Skip to content

Commit

Permalink
Switch to picomatch. Update deps.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Apr 7, 2019
1 parent cece732 commit 5ece3fd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 87 deletions.
34 changes: 21 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

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

Expand All @@ -18,17 +18,22 @@ const arrify = (item) => Array.isArray(item) ? item : [item];
* @param {AnymatchPattern} matcher
* @returns {AnymatchStrBoolFn}
*/
const createPattern = (matcher) => (string) => {
if (typeof matcher === 'function') {
return matcher(string);
}
if (typeof matcher === 'string') {
return matcher === string || micromatch.isMatch(string, matcher);
}
if (matcher instanceof RegExp) {
return matcher.test(string);
const createPattern = (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;
}
return false;
};

/**
Expand Down Expand Up @@ -61,8 +66,11 @@ const anymatch = (matchers, testString, returnIndex = false) => {
// console.log('anymatch', {matchers, testString, containsNegatedGlob, negatedGlobs});

if (negatedGlobs.length > 0) {
if (micromatch.some(unixified, negatedGlobs)) {
return returnIndex ? -1 : false;
for (var i = 0; i < negatedGlobs.length; i++) {
const nglob = negatedGlobs[i];
if (picomatch(nglob)(unixified)) {
return returnIndex ? -1 : false;
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
"function"
],
"scripts": {
"test": "istanbul cover _mocha && cat ./coverage/lcov.info | coveralls"
"test": "nyc mocha",
"mocha": "mocha"
},
"dependencies": {
"micromatch": "micromatch/micromatch#dev",
"normalize-path": "^3.0.0"
"normalize-path": "^3.0.0",
"picomatch": "github:micromatch/picomatch#dev"
},
"devDependencies": {
"coveralls": "^2.7.0",
"istanbul": "^0.4.5",
"mocha": "^6.0.2"
"nyc": "^13.3.0",
"mocha": "^6.1.1"
}
}
101 changes: 33 additions & 68 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,70 @@ var anymatch = require('./');
var assert = require('assert');
var path = require('path');

describe('anymatch', function() {
describe('anymatch', () => {
var matchers = [
'path/to/file.js',
'path/anyjs/**/*.js',
/foo.js$/,
function(string) {
return string.indexOf('/bar') !== -1 && string.length > 10;
}
(string => string.indexOf('/bar') !== -1 && string.length > 10)
];
it('should resolve string matchers', function() {
it('should resolve string matchers', () => {
assert(anymatch(matchers, 'path/to/file.js'));
assert(anymatch(matchers[0], 'path/to/file.js'));
assert(!anymatch(matchers[0], 'bar.js'));
});
it('should resolve glob matchers', function() {
it('should resolve glob matchers', () => {
assert.equal(true, anymatch(matchers, 'path/anyjs/baz.js'));
assert.equal(true, anymatch(matchers[1], 'path/anyjs/baz.js'));
assert.equal(false, anymatch(matchers[1], 'bar.js'));
});
it('should resolve regexp matchers', function() {
it('should resolve regexp matchers', () => {
assert.equal(true, anymatch(matchers, 'path/to/foo.js'));
assert.equal(true, anymatch(matchers[2], 'path/to/foo.js'));
assert.equal(false, anymatch(matchers[2], 'bar.js'));
});
it('should resolve function matchers', function() {
it('should resolve function matchers', () => {
assert.equal(true, anymatch(matchers, 'path/to/bar.js'));
assert.equal(true, anymatch(matchers[3], 'path/to/bar.js'));
assert.equal(false, anymatch(matchers[3], 'bar.js'));
});
it('should return false for unmatched strings', function() {
it('should return false for unmatched strings', () => {
assert.equal(false, anymatch(matchers, 'bar.js'));
});
it('should ignore improperly typed matchers', function() {
it('should ignore improperly typed matchers', () => {
var emptyObj = {};
assert.equal(false, anymatch(emptyObj, ''));
assert.equal(false, anymatch(Infinity, ''));
});

describe('with returnIndex = true', function() {
it('should return the array index of first positive matcher', function() {
describe('with returnIndex = true', () => {
it('should return the array index of first positive matcher', () => {
var result = anymatch(matchers, 'foo.js', true);
assert.equal(result, 2);
});
it('should return 0 if provided non-array matcher', function() {
it('should return 0 if provided non-array matcher', () => {
var result = anymatch(matchers[2], 'foo.js', true);
assert.equal(result, 0);
});
it('should return -1 if no match', function() {
it('should return -1 if no match', () => {
var result = anymatch(matchers, 'bar.js', true);
assert.equal(result, -1);
});
});

describe('curried matching function', function() {
describe('curried matching function', () => {
var matchFn;
before(() => {
matchFn = anymatch(matchers);
});
it('should resolve matchers', function() {
it('should resolve matchers', () => {
assert.equal(true, matchFn('path/to/file.js'));
assert.equal(true, matchFn('path/anyjs/baz.js'));
assert.equal(true, matchFn('path/to/foo.js'));
assert.equal(true, matchFn('path/to/bar.js'));
assert.equal(false, matchFn('bar.js'));
});
it('should be usable as an Array.prototype.filter callback', function() {
it('should be usable as an Array.prototype.filter callback', () => {
var arr = [
'path/to/file.js',
'path/anyjs/baz.js',
Expand All @@ -82,7 +80,7 @@ describe('anymatch', function() {
expected.splice(arr.indexOf('bar.js'), 1);
assert.deepEqual(arr.filter(matchFn), expected);
});
it('should bind individual criterion', function() {
it('should bind individual criterion', () => {
assert(anymatch(matchers[0])('path/to/file.js'));
assert(!anymatch(matchers[0])('path/to/other.js'));
assert(anymatch(matchers[1])('path/anyjs/baz.js'));
Expand All @@ -94,96 +92,63 @@ describe('anymatch', function() {
});
});

// describe('using matcher subsets', function() {
// it('should skip matchers before the startIndex', function() {
// assert(anymatch(matchers, 'path/to/file.js', false));
// assert(!anymatch(matchers, 'path/to/file.js', false, 1));
// });
// it('should skip matchers after and including the endIndex', function() {
// assert(anymatch(matchers, 'path/to/bars.js', false));
// assert(!anymatch(matchers, 'path/to/bars.js', false, 0, 3));
// assert(!anymatch(matchers, 'foo.js', false, 0, 1));
// });
// });

describe('extra args', function() {
it('should not allow string to be passed as first member of an array', function() {
describe('extra args', () => {
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 pass extra args to function matchers', function() {
// matchers.push(function(string, arg1, arg2) { return 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();
// });
});

describe('glob negation', function() {
describe('glob negation', () => {
after(matchers.splice.bind(matchers, 4, 2));

it('should respect negated globs included in a matcher array', function() {
it('should respect negated globs included in a matcher array', () => {
assert(anymatch(matchers, 'path/anyjs/no/no.js'), 'matches existing glob');
matchers.push('!path/anyjs/no/*.js');
assert(!anymatch(matchers, 'path/anyjs/no/no.js'), 'should be negated');
assert(!anymatch(matchers)('path/anyjs/no/no.js'), 'should be negated (curried)');
});
it('should not break returnIndex option', function() {
it('should not break returnIndex option', () => {
assert.equal(anymatch(matchers, 'path/anyjs/yes.js', true), 1);
assert.equal(anymatch(matchers)('path/anyjs/yes.js', true), 1);
assert.equal(anymatch(matchers, 'path/anyjs/no/no.js', true), -1);
assert.equal(anymatch(matchers)('path/anyjs/no/no.js', true), -1);
});
it('should allow negated globs to negate non-glob matchers', function() {
it('should allow negated globs to negate non-glob matchers', () => {
assert.equal(anymatch(matchers, 'path/to/bar.js', true), 3);
matchers.push('!path/to/bar.*');
assert(!anymatch(matchers, 'path/to/bar.js'));
});
});

describe('windows paths', function() {
describe('windows paths', () => {
var origSep = path.sep;
before(function() {
before(() => {
path.sep = '\\';
});
after(function() {
after(() => {
path.sep = origSep;
});

it('should resolve backslashes against string matchers', function() {
it('should resolve backslashes against string matchers', () => {
assert(anymatch(matchers, 'path\\to\\file.js'));
assert(anymatch(matchers)('path\\to\\file.js'));
});
it('should resolve backslashes against glob matchers', function() {
it('should resolve backslashes against glob matchers', () => {
assert(anymatch(matchers, 'path\\anyjs\\file.js'));
assert(anymatch(matchers)('path\\anyjs\\file.js'));
});
it('should resolve backslashes against regex matchers', function() {
it('should resolve backslashes against regex matchers', () => {
assert(anymatch(/path\/to\/file\.js/, 'path\\to\\file.js'));
assert(anymatch(/path\/to\/file\.js/)('path\\to\\file.js'));
});
it('should resolve backslashes against function matchers', function() {
it('should resolve backslashes against function matchers', () => {
assert(anymatch(matchers, 'path\\to\\bar.js'));
assert(anymatch(matchers)('path\\to\\bar.js'));
});
it('should still correctly handle forward-slash paths', function() {
it('should still correctly handle forward-slash paths', () => {
assert(anymatch(matchers, 'path/to/file.js'));
assert(anymatch(matchers)('path/to/file.js'));
assert(!anymatch(matchers, 'path/no/no.js'));
Expand Down

0 comments on commit 5ece3fd

Please sign in to comment.