Skip to content

Commit

Permalink
Merge pull request #34 from bpasero/ben-picooptions
Browse files Browse the repository at this point in the history
allow to specify picomatch options
  • Loading branch information
paulmillr authored Aug 27, 2019
2 parents 657e800 + 17c072e commit 8f96a00
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Usage
npm install anymatch
```

#### anymatch(matchers, testString, [returnIndex])
#### anymatch(matchers, testString, [returnIndex], [options])
* __matchers__: (_Array|String|RegExp|Function_)
String to be directly matched, string with glob patterns, regular expression
test, function that takes the testString as an argument and returns a truthy
Expand All @@ -26,6 +26,7 @@ as the arguments for function matchers.
* __returnIndex__: (_Boolean [optional]_) If true, return the array index of
the first matcher that that testString matched, or -1 if no match, instead of a
boolean result.
* __options__: (_Object_ [optional]_) Any of the [picomatch](https://github.com/micromatch/picomatch#options) options.

```js
const anymatch = require('anymatch');
Expand Down
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ const arrify = (item) => Array.isArray(item) ? item : [item];

/**
* @param {AnymatchPattern} matcher
* @param {object} options
* @returns {AnymatchFn}
*/
const createPattern = (matcher) => {
const createPattern = (matcher, options) => {
if (typeof matcher === 'function') {
return matcher;
}
if (typeof matcher === 'string') {
const glob = picomatch(matcher);
const glob = picomatch(matcher, options);
return (string) => matcher === string || glob(string);
}
if (matcher instanceof RegExp) {
Expand Down Expand Up @@ -62,9 +63,10 @@ const matchPatterns = (patterns, negatedGlobs, path, returnIndex) => {
* @param {AnymatchMatcher} matchers
* @param {Array|string} testString
* @param {boolean=} returnIndex
* @param {object} options
* @returns {boolean|number|Function}
*/
const anymatch = (matchers, testString, returnIndex = false) => {
const anymatch = (matchers, testString, returnIndex = false, options = undefined) => {
if (matchers == null) {
throw new TypeError('anymatch: specify first argument');
}
Expand All @@ -73,8 +75,8 @@ const anymatch = (matchers, testString, returnIndex = false) => {
const negatedGlobs = mtchers
.filter(item => typeof item === 'string' && item.charAt(0) === BANG)
.map(item => item.slice(1))
.map(item => picomatch(item));
const patterns = mtchers.map(createPattern);
.map(item => picomatch(item, options));
const patterns = mtchers.map(matcher => createPattern(matcher, options));

if (testString == null) {
return (testString, ri = false) => {
Expand Down
9 changes: 8 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('anymatch', () => {
assert.throws(() => anymatch());
})
it('should not allow bad testString', () => {
assert.throws(() => anymatch(matchers, {path: 'path/to/bar.js'}));
assert.throws(() => anymatch(matchers, { path: 'path/to/bar.js' }));
});
it('should allow string to be passed as first member of an array', () => {
assert.doesNotThrow(() => anymatch(matchers, ['path/to/bar.js']));
Expand Down Expand Up @@ -183,4 +183,11 @@ describe('anymatch', () => {
assert(!anymatch(matchers)('path/no/no.js'));
});
});

describe('picomatch options', () => {
it('should support picomatch options', () => {
assert.equal(false, anymatch('path/to/?dotfile', 'path/to/.dotfile'));
assert.equal(true, anymatch('path/to/?dotfile', 'path/to/.dotfile', false, { dot: true }));
});
});
});

0 comments on commit 8f96a00

Please sign in to comment.