micromatch
Advanced tools
Comparing version 1.5.0 to 1.6.0
183
index.js
@@ -11,2 +11,3 @@ /*! | ||
var diff = require('arr-diff'); | ||
var debug = require('debug')('micromatch'); | ||
var typeOf = require('kind-of'); | ||
@@ -30,6 +31,3 @@ var omit = require('object.omit'); | ||
function micromatch(files, patterns, opts) { | ||
if (!files || !patterns) { | ||
return []; | ||
} | ||
if (!files || !patterns) return []; | ||
opts = opts || {}; | ||
@@ -41,3 +39,3 @@ | ||
if (typeof patterns === 'string') { | ||
if (!Array.isArray(patterns)) { | ||
return match(files, patterns, opts); | ||
@@ -57,3 +55,2 @@ } | ||
} | ||
return diff(keep, omit); | ||
@@ -76,4 +73,4 @@ } | ||
function match(files, pattern, opts) { | ||
if (typeof files !== 'string' && !Array.isArray(files)) { | ||
throw new Error('micromatch.match() expects a string or array.'); | ||
if (typeOf(files) !== 'string' && !Array.isArray(files)) { | ||
throw new Error(msg('match', 'files', 'a string or array')); | ||
} | ||
@@ -94,3 +91,3 @@ | ||
var isMatch = matcher(pattern, opts); | ||
var _isMatch = matcher(pattern, opts); | ||
var len = files.length, i = 0; | ||
@@ -103,3 +100,3 @@ var res = []; | ||
if (!isMatch(fp)) { continue; } | ||
if (!_isMatch(fp)) { continue; } | ||
res.push(fp); | ||
@@ -110,3 +107,3 @@ } | ||
if (opts.failglob === true) { | ||
throw new Error('micromatch found no matches for: "' + orig + '".'); | ||
throw new Error('micromatch.match() found no matches for: "' + orig + '".'); | ||
} | ||
@@ -132,31 +129,30 @@ | ||
/** | ||
* Return a function for matching based on the | ||
* given `pattern` and `options`. | ||
* Filter files with the given pattern. | ||
* | ||
* @param {String} `pattern` | ||
* @param {Object} `options` | ||
* @return {Function} | ||
* @param {String|Array} `pattern` | ||
* @param {Array} `files` | ||
* @param {Options} `opts` | ||
* @return {Array} | ||
*/ | ||
function matcher(pattern, opts) { | ||
// pattern is a function | ||
if (typeof pattern === 'function') { | ||
return pattern; | ||
function filter(pattern, opts) { | ||
if (typeof pattern !== 'string') { | ||
throw new TypeError(msg('filter', 'pattern', 'a string')); | ||
} | ||
// pattern is a string, make a regex | ||
if (!(pattern instanceof RegExp)) { | ||
if (!isGlob(pattern)) { | ||
return utils.matchPath(pattern, opts); | ||
var fn = matcher(pattern, opts); | ||
return function (files) { | ||
if (!Array.isArray(files)) { | ||
return fn(files); | ||
} | ||
var re = makeRe(pattern, opts); | ||
if (opts && opts.matchBase) { | ||
return utils.hasFilename(re, opts); | ||
var res = files.slice(); | ||
var len = files.length; | ||
while (len--) { | ||
if (!fn(files[len])) { | ||
res.splice(len, 1); | ||
} | ||
} | ||
return function(fp) { | ||
return re.test(fp); | ||
}; | ||
} | ||
// pattern is already a regex | ||
return function(fp) { | ||
return pattern.test(fp); | ||
return res; | ||
}; | ||
@@ -184,2 +180,6 @@ } | ||
function isMatch(fp, pattern, opts) { | ||
if (typeof fp !== 'string') { | ||
throw new TypeError(msg('isMatch', 'filepath', 'a string')); | ||
} | ||
if (typeOf(pattern) === 'object') { | ||
@@ -217,3 +217,3 @@ return matcher(fp, pattern); | ||
if (!Array.isArray(patterns) && typeof patterns !== 'string') { | ||
throw new TypeError('micromatch.any() expects a string or array.'); | ||
throw new TypeError(msg('any', 'patterns', 'a string or array')); | ||
} | ||
@@ -240,14 +240,13 @@ | ||
function matchKeys(pattern, obj, options) { | ||
var re = !(pattern instanceof RegExp) | ||
? makeRe(pattern, options) | ||
: pattern; | ||
function matchKeys(obj, pattern, options) { | ||
if (typeOf(obj) !== 'object') { | ||
throw new TypeError(msg('matchKeys', 'first argument', 'an object')); | ||
} | ||
var fn = matcher(pattern, options); | ||
var res = {}; | ||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
if (re.test(key)) { | ||
res[key] = obj[key]; | ||
} | ||
if (obj.hasOwnProperty(key) && fn(key)) { | ||
res[key] = obj[key]; | ||
} | ||
@@ -259,33 +258,35 @@ } | ||
/** | ||
* Filter files with the given pattern. | ||
* Return a function for matching based on the | ||
* given `pattern` and `options`. | ||
* | ||
* @param {String|Array} `pattern` | ||
* @param {Array} `files` | ||
* @param {Options} `opts` | ||
* @return {Array} | ||
* @param {String} `pattern` | ||
* @param {Object} `options` | ||
* @return {Function} | ||
*/ | ||
function filter(pattern, opts) { | ||
opts = opts || {}; | ||
function matcher(pattern, opts) { | ||
// pattern is a function | ||
if (typeof pattern === 'function') { | ||
return pattern; | ||
} | ||
// pattern is a regex | ||
if (pattern instanceof RegExp) { | ||
return function(fp) { | ||
return pattern.test(fp); | ||
}; | ||
} | ||
// pattern is a non-glob string | ||
if (!isGlob(pattern)) { | ||
return utils.matchPath(pattern, opts); | ||
} | ||
// pattern is a glob string | ||
var re = makeRe(pattern, opts); | ||
pattern = !(pattern instanceof RegExp) | ||
? makeRe(pattern, opts) | ||
: pattern; | ||
return function (files) { | ||
if (typeof files === 'string') { | ||
return isMatch(files, pattern, opts); | ||
} | ||
var res = files.slice(); | ||
var len = files.length; | ||
while (len--) { | ||
var match = isMatch(files[len], pattern, opts); | ||
if (match) { | ||
continue; | ||
} | ||
res.splice(len, 1); | ||
} | ||
return res; | ||
// `matchBase` is defined | ||
if (opts && opts.matchBase) { | ||
return utils.hasFilename(re, opts); | ||
} | ||
// `matchBase` is not defined | ||
return function(fp) { | ||
return re.test(fp); | ||
}; | ||
@@ -307,5 +308,8 @@ } | ||
function toRegex(glob, options) { | ||
// clone options to prevent mutating upstream values | ||
if (typeOf(glob) !== 'string') { | ||
throw new Error(msg('toRegex', 'glob', 'a string')); | ||
} | ||
// clone options to prevent mutating the original object | ||
var opts = Object.create(options || {}); | ||
var flags = opts.flags || ''; | ||
@@ -325,4 +329,6 @@ if (opts.nocase && flags.indexOf('i') === -1) { | ||
return new RegExp(glob, flags); | ||
} catch (err) {} | ||
return /^$/; | ||
} catch (err) { | ||
debug('toRegex', err); | ||
} | ||
return /$^/; | ||
} | ||
@@ -359,15 +365,34 @@ | ||
/** | ||
* Make error messages consistent. Follows this format: | ||
* | ||
* ```js | ||
* msg(methodName, argNumber, nativeType); | ||
* // example: | ||
* msg('matchKeys', 'first', 'an object'); | ||
* ``` | ||
* | ||
* @param {String} `method` | ||
* @param {String} `num` | ||
* @param {String} `type` | ||
* @return {String} | ||
*/ | ||
function msg(method, what, type) { | ||
return 'micromatch.' + method + '(): ' + what + ' should be ' + type + '.'; | ||
} | ||
/** | ||
* Public methods | ||
*/ | ||
micromatch.any = any; | ||
micromatch.braces = micromatch.braceExpand = require('braces'); | ||
micromatch.contains = contains; | ||
micromatch.expand = expand; | ||
micromatch.filter = filter; | ||
micromatch.isMatch = isMatch; | ||
micromatch.makeRe = makeRe; | ||
micromatch.contains = contains; | ||
micromatch.any = any; | ||
micromatch.isMatch = isMatch; | ||
micromatch.filter = filter; | ||
micromatch.matchKeys = matchKeys; | ||
micromatch.match = match; | ||
micromatch.matcher = matcher; | ||
micromatch.matchKeys = matchKeys; | ||
@@ -374,0 +399,0 @@ /** |
{ | ||
"name": "micromatch", | ||
"description": "Glob matching for javascript/node.js. A faster alternative to minimatch (10-45x faster on avg), with all the features you're used to using in your Grunt and gulp tasks.", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"homepage": "https://github.com/jonschlinkert/micromatch", | ||
@@ -31,3 +31,3 @@ "author": { | ||
"test": "mocha", | ||
"benchmark": "node benchmark" | ||
"prepublish": "browserify -o browser.js -e index.js" | ||
}, | ||
@@ -37,2 +37,3 @@ "dependencies": { | ||
"braces": "^1.7.0", | ||
"debug": "^2.1.2", | ||
"expand-brackets": "^0.1.1", | ||
@@ -49,2 +50,3 @@ "extglob": "^0.2.0", | ||
"benchmarked": "^0.1.3", | ||
"browserify": "^9.0.3", | ||
"chalk": "^1.0.0", | ||
@@ -51,0 +53,0 @@ "helper-reflinks": "^0.4.0", |
@@ -349,3 +349,3 @@ # micromatch [![NPM version](https://badge.fury.io/js/micromatch.svg)](http://badge.fury.io/js/micromatch) [![Build Status](https://travis-ci.org/jonschlinkert/micromatch.svg)](https://travis-ci.org/jonschlinkert/micromatch) | ||
As of March 04, 2015: | ||
As of March 06, 2015: | ||
@@ -433,3 +433,3 @@ ```bash | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on March 04, 2015._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on March 06, 2015._ | ||
@@ -436,0 +436,0 @@ [brace expansion]: https://github.com/jonschlinkert/braces |
37994
959
11
10
+ Addeddebug@^2.1.2
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)