Skip to content

Commit

Permalink
Merge pull request #2 from es128/micromatch
Browse files Browse the repository at this point in the history
Switch to micromatch
  • Loading branch information
es128 committed Mar 23, 2015
2 parents 11878ef + 3935ac5 commit a5cc6ea
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- "iojs"
- "0.12"
- "0.11"
- "0.10"
- "0.8"
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ Change Log
----------
[See release notes page on GitHub](https://github.com/es128/anymatch/releases)

NOTE: As of v1.2.0, anymatch uses [micromatch](https://github.com/jonschlinkert/micromatch)
for glob pattern matching. The glob matching behavior should be functionally
equivalent to the commonly used [minimatch](https://github.com/isaacs/minimatch)
library (aside from some fixed bugs and greater performance), so a major
version bump wasn't merited. Issues with glob pattern matching should be
reported directly to the [micromatch issue tracker](https://github.com/jonschlinkert/micromatch/issues).

License
-------
[ISC](https://raw.github.com/es128/anymatch/master/LICENSE)
22 changes: 15 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
'use strict';

var minimatch = require('minimatch');
var micromatch = require('micromatch');
var arrify = require('arrify');

var anymatch = function(criteria, value, returnIndex, startIndex, endIndex) {
if (!Array.isArray(criteria)) { criteria = [criteria]; }
if (arguments.length === 1) { return anymatch.bind(null, criteria); }
var string = Array.isArray(value) ? value[0] : value;
if (!startIndex) { startIndex = 0; }
criteria = arrify(criteria);
value = arrify(value);
if (arguments.length === 1) {
return criteria.length === 1 ?
micromatch.matcher(criteria[0]) :
anymatch.bind(null, criteria.map(function(criterion) {
return micromatch.matcher(criterion);
}));
}
startIndex = startIndex || 0;
var string = value[0];
var matchIndex = -1;
function testCriteria (criterion, index) {
var result;
switch (toString.call(criterion)) {
case '[object String]':
result = string === criterion || minimatch(string, criterion);
result = string === criterion || micromatch.isMatch(string, criterion);
break;
case '[object RegExp]':
result = criterion.test(string);
break;
case '[object Function]':
result = criterion.apply(null, Array.isArray(value) ? value : [value]);
result = criterion.apply(null, value);
break;
default:
result = false;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"test": "istanbul cover _mocha && cat ./coverage/lcov.info | coveralls"
},
"dependencies": {
"minimatch": "~1.0.0"
"arrify": "^1.0.0",
"micromatch": "^2.1.0"
},
"devDependencies": {
"coveralls": "~2.11.2",
Expand Down
24 changes: 17 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ describe('anymatch', function() {
});

describe('curried matching function', function() {
var matchFunc = anymatch(matchers);
var matchFn = anymatch(matchers);
it('should resolve matchers', function() {
assert(anymatch(matchers, 'path/to/file.js'));
assert(anymatch(matchers, 'path/anyjs/baz.js'));
assert(anymatch(matchers, 'path/to/foo.js'));
assert(anymatch(matchers, 'path/to/bar.js'));
assert(!anymatch(matchers, 'bar.js'));
assert(matchFn('path/to/file.js'));
assert(matchFn('path/anyjs/baz.js'));
assert(matchFn('path/to/foo.js'));
assert(matchFn('path/to/bar.js'));
assert(!matchFn('bar.js'));
});
it('should be usable as an Array.prototype.filter callback', function() {
var arr = [
Expand All @@ -76,7 +76,17 @@ describe('anymatch', function() {
];
var expected = arr.slice();
expected.splice(arr.indexOf('bar.js'), 1);
assert.deepEqual(arr.filter(matchFunc), expected);
assert.deepEqual(arr.filter(matchFn), expected);
});
it('should bind individual criterion', function() {
assert(anymatch(matchers[0])('path/to/file.js'));
assert(!anymatch(matchers[0])('path/to/other.js'));
assert(anymatch(matchers[1])('path/anyjs/baz.js'));
assert(!anymatch(matchers[1])('path/to/baz.js'));
assert(anymatch(matchers[2])('path/to/foo.js'));
assert(!anymatch(matchers[2])('path/to/foo.js.bak'));
assert(anymatch(matchers[3])('path/to/bar.js'));
assert(!anymatch(matchers[3])('bar.js'));
});
});

Expand Down

0 comments on commit a5cc6ea

Please sign in to comment.