jonschlinkert / fill-range

Fill in a range of numbers or letters, positive or negative, optionally passing an increment or multiplier to use.
MIT License
54 stars 15 forks source link

Refactor #4

Closed jonschlinkert closed 8 years ago

jonschlinkert commented 8 years ago

The main goal of this refactor was to improve conversions to regex-compatible strings. For example:

fill-range v2.2.x (current)

This is how ranges currently expand in fill-range (as would typically be expected):

var fill = require('fill-range');
console.log(fill(1, 100))
//=> [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100' ]

// or when "options.makeRe" is enabled
var fill = require('fill-range');
console.log(fill(1, 100, {makeRe: true}))
//=> [ '(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|100)' ]

fill-range is used in micromatch, here is how minimatch and micromatch handle these ranges currently:

var minimatch = require('minimatch');
console.log(minimatch.makeRe('a/{1..100}/b'));
//=> /^(?:a\/1\/b|a\/2\/b|a\/3\/b|a\/4\/b|a\/5\/b|a\/6\/b|a\/7\/b|a\/8\/b|a\/9\/b|a\/10\/b|a\/11\/b|a\/12\/b|a\/13\/b|a\/14\/b|a\/15\/b|a\/16\/b|a\/17\/b|a\/18\/b|a\/19\/b|a\/20\/b|a\/21\/b|a\/22\/b|a\/23\/b|a\/24\/b|a\/25\/b|a\/26\/b|a\/27\/b|a\/28\/b|a\/29\/b|a\/30\/b|a\/31\/b|a\/32\/b|a\/33\/b|a\/34\/b|a\/35\/b|a\/36\/b|a\/37\/b|a\/38\/b|a\/39\/b|a\/40\/b|a\/41\/b|a\/42\/b|a\/43\/b|a\/44\/b|a\/45\/b|a\/46\/b|a\/47\/b|a\/48\/b|a\/49\/b|a\/50\/b|a\/51\/b|a\/52\/b|a\/53\/b|a\/54\/b|a\/55\/b|a\/56\/b|a\/57\/b|a\/58\/b|a\/59\/b|a\/60\/b|a\/61\/b|a\/62\/b|a\/63\/b|a\/64\/b|a\/65\/b|a\/66\/b|a\/67\/b|a\/68\/b|a\/69\/b|a\/70\/b|a\/71\/b|a\/72\/b|a\/73\/b|a\/74\/b|a\/75\/b|a\/76\/b|a\/77\/b|a\/78\/b|a\/79\/b|a\/80\/b|a\/81\/b|a\/82\/b|a\/83\/b|a\/84\/b|a\/85\/b|a\/86\/b|a\/87\/b|a\/88\/b|a\/89\/b|a\/90\/b|a\/91\/b|a\/92\/b|a\/93\/b|a\/94\/b|a\/95\/b|a\/96\/b|a\/97\/b|a\/98\/b|a\/99\/b|a\/100\/b)$/

var mm = require('micromatch');
console.log(mm.makeRe('a/{1..100}/b'));
//=> /^(?:a\/(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|100)\/b)$/

fill-range v3.0.0

Here is how fill-range handles ranges in 3.0 when options.makeRe is enabled:

console.log(fill(1, 100, {makeRe: true}));
//=> '[1-9]|[1-9][0-9]|100'

Which will result in more speed improvements in micromatch by reducing ranges to the following:

var mm = require('micromatch');
console.log(mm.makeRe('a/{1..100}/b'));
//=> /^(?:a\/(?:[1-9]|[1-9][0-9]|100)\/b)$/

Breaking changes

Support for special step characters has been removed. The same thing can be accomplished by passing a function as the last argument.

cc @doowb @es128 @paulmillr I'll leave this for a day or so in case anyone wants to review or provide feedback before I merge. thx

paulmillr commented 8 years ago

Looks great — esp since all tests pass.

es128 commented 8 years ago

Didn't really review the diff, but your description of the new behavior looks awesome - especially compared with the earlier brute-force approaches you illustrated.

I assume that when not using makeRe it still expands to the full array of values like before, right?

jonschlinkert commented 8 years ago

I assume that when not using makeRe it still expands to the full array of values like before, right?

exactly, it will still create the full arrays. thanks for the feedback!

jonschlinkert commented 8 years ago

855,000 generated tests pass in <300 ms lol. (fwiw minimatch freezes on most of those)

screen shot 2016-09-15 at 3 47 02 am