Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved support for numeric brace ranges #170

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 30 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const micromatch = (list, patterns, options) => {
};

for (let i = 0; i < patterns.length; i++) {
let isMatch = picomatch(String(patterns[i]), { ...options, onResult }, true);
let isMatch = picomatch(String(patterns[i]), { ...options, onResult, expandRange }, true);
let negated = isMatch.state.negated || isMatch.state.negatedExtglob;
if (negated) negatives++;

Expand Down Expand Up @@ -120,7 +120,9 @@ micromatch.matcher = (pattern, options) => picomatch(pattern, options);
* @api public
*/

micromatch.isMatch = (str, patterns, options) => picomatch(patterns, options)(str);
micromatch.isMatch = (str, patterns, options) => {
return picomatch(patterns, { ...options, expandRange })(str);
}

/**
* Backwards compatibility
Expand Down Expand Up @@ -360,7 +362,7 @@ micromatch.all = (str, patterns, options) => {

micromatch.capture = (glob, input, options) => {
let posix = utils.isWindows(options);
let regex = picomatch.makeRe(String(glob), { ...options, capture: true });
let regex = micromatch.makeRe(String(glob), { ...options, capture: true });
let match = regex.exec(posix ? utils.toPosixSlashes(input) : input);

if (match) {
Expand All @@ -384,7 +386,10 @@ micromatch.capture = (glob, input, options) => {
* @api public
*/

micromatch.makeRe = (...args) => picomatch.makeRe(...args);
micromatch.makeRe = ( pattern, options ) => {
let result = picomatch.makeRe( pattern, { ...options, expandRange } );
return result
}

/**
* Scan a glob pattern to separate the pattern into segments. Used
Expand Down Expand Up @@ -460,6 +465,27 @@ micromatch.braceExpand = (pattern, options) => {
return micromatch.braces(pattern, { ...options, expand: true });
};

/**
* Helpers
*/

// Replacement for picomatch.parse.expandRange
function expandRange( start, end, incr, options ) {
if ( arguments.length == 3 ) {
options = incr;
incr = '';
} else {
incr = '..'+incr;
}
let source = braces.compile( `{${start}..${end}${incr}}`, options );

// Convert to non-capturing group, since capture groups are added downstream.
source = '(?:' + source.substr( 1 );

return source;
}


/**
* Expose micromatch
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "micromatch",
"description": "Glob matching for javascript/node.js. A replacement and faster alternative to minimatch and multimatch.",
"version": "4.0.2",
"version": "4.0.3",
"homepage": "https://github.com/micromatch/micromatch",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"contributors": [
Expand Down
8 changes: 8 additions & 0 deletions test/api.capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ describe('.capture()', () => {
assert.deepEqual(capture('test/!(a|b)/x.js', 'test/x/x.js'), ['x']);
assert.deepEqual(capture('test/!(a|b)/x.js', 'test/y/x.js'), ['y']);
});

it('should capture brace range expressions', () => {
assert.deepEqual(capture('test/{10..90}/x.js', 'test/25/x.js'), ['25']);
});

it('should capture multiple brace ranges', () => {
assert.deepEqual(capture('test/{10..90}/x/{100..200}/x.js', 'test/20/x/150/x.js'), ['20','150']);
});
});
11 changes: 11 additions & 0 deletions test/api.makeRe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ describe('.makeRe()', () => {
it('should create a regex for a glob pattern', () => {
assert(mm.makeRe('*') instanceof RegExp);
});

describe('differences from picomatch', () => {
it('should produce good regex for braces', () => {
let regex = mm.makeRe('{2..10..3}', { capture: false });
assert(regex instanceof RegExp);
assert.equal( regex.source, '^(?:(?:2|5|8))$');

regex = mm.makeRe('{2..10..3}', { capture: true });
assert.equal( regex.source, '^(?:(?:(2|5|8)))$');
});
});
});
29 changes: 27 additions & 2 deletions test/braces.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,35 @@ describe('braces', () => {
assert(isMatch('a/c', 'a/{a,b,c}'));
});

it('should match with brace ranges', () => {
it('should match with alphabetic brace ranges', () => {
assert(isMatch('a/a', 'a/{a..c}'));
assert(isMatch('a/b', 'a/{a..c}'));
assert(isMatch('a/c', 'a/{a..c}'));
assert(!isMatch('a/d', 'a/{a..c}'));
assert(!isMatch('a/0', 'a/{a..c}'));
});

it('should match with single-digit numeric brace ranges', () => {
assert(isMatch('a/1', 'a/{0..8}'));
assert(!isMatch('a/9', 'a/{0..8}'));
});

it('should match with multi-digit numeric brace ranges', () => {
assert(!isMatch('a/4', 'a/{5..250}'));
assert(isMatch('a/5', 'a/{5..250}'));
assert(isMatch('a/250', 'a/{5..250}'));
assert(!isMatch('a/251', 'a/{5..250}'));
});

xit('should match with zero-padded numeric brace ranges', () => {
assert(!isMatch('a/', 'a/{000..600}'));
assert(isMatch('a/001', 'a/{000..600}'));
assert(!isMatch('a/50', 'a/{000..600}'));
assert(isMatch('a/050', 'a/{000..600}'));
assert(isMatch('a/500', 'a/{000..600}'));
assert(!isMatch('a/700', 'a/{000..600}'));
assert(!isMatch('a/5000', 'a/{000..600}'));
assert(!isMatch('a/251', 'a/{000..600}'));
});

it('should not convert braces inside brackets', () => {
Expand All @@ -61,7 +86,7 @@ describe('braces', () => {
assert(isMatch('a.txt', 'a{,b}.txt'));
assert(isMatch('a.txt', 'a{b,}.txt'));
assert(isMatch('aa.txt', 'a{a,b,}.txt'));
assert(isMatch('aa.txt', 'a{a,b,}.txt'));
assert(!isMatch('ac.txt', 'a{a,b,}.txt'));
assert(isMatch('ab.txt', 'a{,b}.txt'));
assert(isMatch('ab.txt', 'a{b,}.txt'));
});
Expand Down
8 changes: 8 additions & 0 deletions test/micromatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ describe('micromatch', () => {
assert.deepEqual(mm(['a [bc]'], 'a \\[bc\\]*'), ['a [bc]']);
assert.deepEqual(mm(['a [b]', 'a [b].js'], 'a \\[b\\].*'), ['a [b].js']);
});

it('should match numeric brace expressions', () => {
let fixtures = ['0', '1', '10', '100','110','010'];
// assert.deepEqual(mm(fixtures, ['{000..999}']), ['100','010']);
assert.deepEqual(mm(fixtures, ['{0..9}']), ['0','1']);
assert.deepEqual(mm(fixtures, ['{11..120}']), ['100','110']);
});

});

describe('windows paths', () => {
Expand Down