Skip to content

Commit

Permalink
Bring back arg passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Apr 8, 2019
1 parent 13343b0 commit 57e0193
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 32 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ npm install anymatchx
String to be directly matched, string with glob patterns, regular expression
test, function that takes the testString as an argument and returns a truthy
value if it should be matched, or an array of any number and mix of these types.
* __testString__: (_String) The string to test against the matchers.
* __testString__: (_String|Array_) The string to test against the matchers. If
passed as an array, the first element of the array will be used as the
`testString` for non-function matchers, while the entire array will be applied
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.
Expand All @@ -44,9 +47,6 @@ anymatch(matchers, 'bar.js'); // false
anymatch(matchers, 'foo.js', true); // 2
anymatch(matchers, 'path/anyjs/foo.js', true); // 1

// skip matchers
anymatch(matchers, 'path/to/file.js', false, 1); // false

// using globs to match directories and their children
anymatch('node_modules', 'node_modules'); // true
anymatch('node_modules', 'node_modules/somelib/index.js'); // false
Expand All @@ -73,7 +73,7 @@ Change Log
----------
[See release notes page on GitHub](https://github.com/micromatch/anymatch/releases)

- **v3.0:**: `testString` can no longer be an Array. Removed `startIndex` and `endIndex` arguments.
- **v3.0:**: Removed `startIndex` and `endIndex` arguments.
- **v2.0:** [micromatch](https://github.com/jonschlinkert/micromatch) moves away from minimatch-parity and inline with Bash. This includes handling backslashes differently (see https://github.com/micromatch/micromatch#backslashes for more information).
- **v1.2:** anymatch uses [micromatch](https://github.com/jonschlinkert/micromatch)
for glob pattern matching. Issues with glob pattern matching should be
Expand Down
46 changes: 24 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const picomatch = require('picomatch');
const normalizePath = require('normalize-path');
const {sep} = require('path'); // required for tests.

/**
* @typedef {(string:String) => boolean} AnymatchFn
Expand All @@ -14,36 +13,33 @@ const BANG = '!';
const arrify = (item) => Array.isArray(item) ? item : [item];

/**
*
* @param {AnymatchPattern} matcher
* @returns {AnymatchFn}
* @returns {Function}
*/
const createPattern = (matcher) => {
if (typeof matcher === 'function') {
return matcher;
}
const isString = typeof matcher === 'string';
let glob;
if (isString) glob = picomatch(matcher);
return (string) => {
if (typeof matcher === 'function') {
return matcher(string);
}
if (isString) {
return matcher === string || glob(string);
}
if (matcher instanceof RegExp) {
return matcher.test(string);
}
return false;
if (typeof matcher === 'string') {
const glob = picomatch(matcher);
return (string) => matcher === string || glob(string);
}
if (matcher instanceof RegExp) {
return (string) => matcher.test(string);
}
return (string) => false;
};

/**
* @param {Array<AnymatchFn>} patterns
* @param {Array<AnymatchFn>} negatedGlobs
* @param {String} path
* @param {String|Array} path
* @param {Boolean} returnIndex
*/
const matchPatterns = (patterns, negatedGlobs, path, returnIndex) => {
const upath = normalizePath(path);
const additionalArgs = Array.isArray(path);
const upath = normalizePath(additionalArgs ? path[0] : path);
if (negatedGlobs.length > 0) {
for (let index = 0; index < negatedGlobs.length; index++) {
const nglob = negatedGlobs[index];
Expand All @@ -54,8 +50,14 @@ const matchPatterns = (patterns, negatedGlobs, path, returnIndex) => {
}
for (let index = 0; index < patterns.length; index++) {
const pattern = patterns[index];
if (pattern(upath)) {
return returnIndex ? index : true;
if (additionalArgs) {
if (pattern(...path)) {
return returnIndex ? index : true;
}
} else {
if (pattern(upath)) {
return returnIndex ? index : true;
}
}
}

Expand All @@ -64,7 +66,7 @@ const matchPatterns = (patterns, negatedGlobs, path, returnIndex) => {

/**
* @param {AnymatchMatcher} matchers
* @param {String} testString
* @param {String|Array<String>} testString
* @param {Boolean=} returnIndex
* @returns {boolean|Number|Function}
*/
Expand All @@ -86,7 +88,7 @@ const anymatch = (matchers, testString, returnIndex = false) => {
return matchPatterns(patterns, negatedGlobs, testString, returnIndex);
}
}
if (typeof testString !== 'string') {
if (!Array.isArray(testString) && typeof testString !== 'string') {
throw new TypeError('anymatch: second argument must be a string: got ' +
Object.prototype.toString.call(testString))
}
Expand Down
38 changes: 33 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var anymatch = require('./');
var assert = require('assert');
var path = require('path');
let anymatch = require('./');
let assert = require('assert');
let path = require('path');

describe('anymatch', () => {
var matchers = [
Expand Down Expand Up @@ -96,8 +96,36 @@ describe('anymatch', () => {
it('should not allow no args', () => {
assert.throws(() => anymatch());
})
it('should not allow string to be passed as first member of an array', () => {
assert.throws(() => anymatch(matchers, ['path/to/bar.js']));
it('should not allow bad testString', () => {
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']));
});

it('should pass extra args to function matchers', () => {
matchers.push((string, arg1, arg2) => arg1 || arg2);
assert(!anymatch(matchers, 'bar.js'), 1);
assert(!anymatch(matchers, ['bar.js', 0]), 2);
assert(anymatch(matchers, ['bar.js', true]), 3);
assert(anymatch(matchers, ['bar.js', 0, true]), 4);
// with returnIndex
assert.equal(anymatch(matchers, ['bar.js', 1], true), 4, 5);
// curried versions
var matchFn1 = anymatch(matchers);
var matchFn2 = anymatch(matchers[4]);
assert(!matchFn1(['bar.js', 0]), 6);
assert(!matchFn2(['bar.js', 0]), 7);
assert(matchFn1(['bar.js', true]), 8);
assert(matchFn2(['bar.js', true]), 9);
assert(matchFn1(['bar.js', 0, true]), 10);
assert(matchFn2(['bar.js', 0, true]), 11);
// curried with returnIndex
assert.equal(matchFn1(['bar.js', 1], true), 4, 12);
assert.equal(matchFn2(['bar.js', 1], true), 0, 13);
assert.equal(matchFn1(['bar.js', 0], true), -1, 14);
assert.equal(matchFn2(['bar.js', 0], true), -1, 15);
matchers.pop();
});
});

Expand Down

0 comments on commit 57e0193

Please sign in to comment.