Skip to content

Commit

Permalink
Preserve input order for the returned matching paths (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanveerulHuq committed Oct 12, 2020
1 parent 9f0d68b commit 18d778b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Extends [`minimatch.match()`](https://github.com/isaacs/minimatch#minimatchmatch
@param paths - Paths to match against.
@param patterns - Globbing patterns to use. For example: `['*', '!cake']`. See supported [`minimatch` patterns](https://github.com/isaacs/minimatch#usage).
@returns The matching paths.
@returns The matching paths in the order of input paths.
@example
```
Expand Down
19 changes: 12 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ module.exports = (list, patterns, options = {}) => {
return [];
}

return patterns.reduce((result, pattern) => {
let process = arrayUnion;
let result = [];
for (const item of list) {
for (let pattern of patterns) {
let process = arrayUnion;

if (pattern[0] === '!') {
pattern = pattern.slice(1);
process = arrayDiffer;
if (pattern[0] === '!') {
pattern = pattern.slice(1);
process = arrayDiffer;
}

result = process(result, minimatch.match([item], pattern, options));
}
}

return process(result, minimatch.match(list, pattern, options));
}, []);
return result;
};
9 changes: 1 addition & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

> Extends [`minimatch.match()`](https://github.com/isaacs/minimatch#minimatchmatchlist-pattern-options) with support for multiple patterns

## Install

```
$ npm install multimatch
```


## Usage

```js
Expand All @@ -21,12 +19,11 @@ multimatch(['unicorn', 'cake', 'rainbows'], ['*', '!cake']);

See the [tests](https://github.com/sindresorhus/multimatch/tree/master/test) for more usage examples and expected matches.


## API

### multimatch(paths, patterns, options?)

Returns an array of matching paths.
Returns an array of matching paths in the order of input paths.

#### paths

Expand All @@ -49,14 +46,12 @@ Type: `object`

See the [`minimatch` options](https://github.com/isaacs/minimatch#options).


## How multiple patterns work

Positive patterns (e.g. `foo` or `*`) add to the results, while negative patterns (e.g. `!foo`) subtract from the results.

Therefore a lone negation (e.g. `['!foo']`) will never match anything – use `['*', '!foo']` instead.


## Globbing patterns

Just a quick overview.
Expand All @@ -67,13 +62,11 @@ Just a quick overview.
- `{}` allows for a comma-separated list of "or" expressions
- `!` at the beginning of a pattern will negate the match


## Related

- [globby](https://github.com/sindresorhus/globby) - Match against the filesystem instead of a list
- [matcher](https://github.com/sindresorhus/matcher) - Simple wildcard matching


---

<div align="center">
Expand Down
2 changes: 1 addition & 1 deletion test/globule.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test('partial exclusion should partially cancel match', t => {

test('inclusion / exclusion order matters', t => {
t.deepEqual(multimatch(['foo.js', 'bar.js', 'baz.js'], ['*.js', '!*.js', 'b*.js']), ['bar.js', 'baz.js']);
t.deepEqual(multimatch(['foo.js', 'bar.js', 'baz.js'], ['*.js', '!f*.js', '*.js']), ['bar.js', 'baz.js', 'foo.js']);
t.deepEqual(multimatch(['foo.js', 'bar.js', 'baz.js'], ['*.js', '!f*.js', '*.js']), ['foo.js', 'bar.js', 'baz.js']);
});

test('should matchBase (minimatch) when specified.', t => {
Expand Down
12 changes: 7 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ test('return an array of matches', t => {
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', 'bar']), ['foo', 'bar']);
});

test('return matches in the order the patterns were defined', t => {
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['bar', 'f*']), ['bar', 'foo']);
test('return matches in the order the list were defined', t => {
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['bar', 'f*']), ['foo', 'bar']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['f*', '*z']), ['foo', 'baz']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*z', 'f*']), ['baz', 'foo']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*z', 'f*']), ['foo', 'baz']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*z', '*r', 'f*']), ['foo', 'bar']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*a*', '!f*']), ['bar', 'baz']);
});

test('return an array with negations omitted', t => {
Expand Down Expand Up @@ -53,7 +55,7 @@ test('patterns be order sensitive', t => {
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!*{o,r}']), []);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!foo', 'bar']), ['bar']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!bar']), ['foo']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['bar', '!foo', 'foo']), ['bar', 'foo']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['bar', '!foo', 'foo']), ['foo', 'bar']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!foo', 'bar']), ['bar']);
});

Expand All @@ -75,7 +77,7 @@ test('misc', t => {
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*{o,r}', '*']), ['foo', 'bar', 'baz']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*', '!*{o,r}']), ['baz']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!*{o,r}', '*']), ['foo', 'bar', 'baz']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*', '!*{o,r}', 'foo']), ['baz', 'foo']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*', '!*{o,r}', 'foo']), ['foo', 'baz']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*{o,r}', '*', 'foo']), ['foo', 'bar', 'baz']);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!*{o,r}']), []);
t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!*{o,r}', 'foo']), ['foo']);
Expand Down

0 comments on commit 18d778b

Please sign in to comment.