Initial commit
[yaffs-website] / node_modules / micromatch / index.js
1 /*!
2  * micromatch <https://github.com/jonschlinkert/micromatch>
3  *
4  * Copyright (c) 2014-2015, Jon Schlinkert.
5  * Licensed under the MIT License.
6  */
7
8 'use strict';
9
10 var expand = require('./lib/expand');
11 var utils = require('./lib/utils');
12
13 /**
14  * The main function. Pass an array of filepaths,
15  * and a string or array of glob patterns
16  *
17  * @param  {Array|String} `files`
18  * @param  {Array|String} `patterns`
19  * @param  {Object} `opts`
20  * @return {Array} Array of matches
21  */
22
23 function micromatch(files, patterns, opts) {
24   if (!files || !patterns) return [];
25   opts = opts || {};
26
27   if (typeof opts.cache === 'undefined') {
28     opts.cache = true;
29   }
30
31   if (!Array.isArray(patterns)) {
32     return match(files, patterns, opts);
33   }
34
35   var len = patterns.length, i = 0;
36   var omit = [], keep = [];
37
38   while (len--) {
39     var glob = patterns[i++];
40     if (typeof glob === 'string' && glob.charCodeAt(0) === 33 /* ! */) {
41       omit.push.apply(omit, match(files, glob.slice(1), opts));
42     } else {
43       keep.push.apply(keep, match(files, glob, opts));
44     }
45   }
46   return utils.diff(keep, omit);
47 }
48
49 /**
50  * Return an array of files that match the given glob pattern.
51  *
52  * This function is called by the main `micromatch` function If you only
53  * need to pass a single pattern you might get very minor speed improvements
54  * using this function.
55  *
56  * @param  {Array} `files`
57  * @param  {String} `pattern`
58  * @param  {Object} `options`
59  * @return {Array}
60  */
61
62 function match(files, pattern, opts) {
63   if (utils.typeOf(files) !== 'string' && !Array.isArray(files)) {
64     throw new Error(msg('match', 'files', 'a string or array'));
65   }
66
67   files = utils.arrayify(files);
68   opts = opts || {};
69
70   var negate = opts.negate || false;
71   var orig = pattern;
72
73   if (typeof pattern === 'string') {
74     negate = pattern.charAt(0) === '!';
75     if (negate) {
76       pattern = pattern.slice(1);
77     }
78
79     // we need to remove the character regardless,
80     // so the above logic is still needed
81     if (opts.nonegate === true) {
82       negate = false;
83     }
84   }
85
86   var _isMatch = matcher(pattern, opts);
87   var len = files.length, i = 0;
88   var res = [];
89
90   while (i < len) {
91     var file = files[i++];
92     var fp = utils.unixify(file, opts);
93
94     if (!_isMatch(fp)) { continue; }
95     res.push(fp);
96   }
97
98   if (res.length === 0) {
99     if (opts.failglob === true) {
100       throw new Error('micromatch.match() found no matches for: "' + orig + '".');
101     }
102
103     if (opts.nonull || opts.nullglob) {
104       res.push(utils.unescapeGlob(orig));
105     }
106   }
107
108   // if `negate` was defined, diff negated files
109   if (negate) { res = utils.diff(files, res); }
110
111   // if `ignore` was defined, diff ignored filed
112   if (opts.ignore && opts.ignore.length) {
113     pattern = opts.ignore;
114     opts = utils.omit(opts, ['ignore']);
115     res = utils.diff(res, micromatch(res, pattern, opts));
116   }
117
118   if (opts.nodupes) {
119     return utils.unique(res);
120   }
121   return res;
122 }
123
124 /**
125  * Returns a function that takes a glob pattern or array of glob patterns
126  * to be used with `Array#filter()`. (Internally this function generates
127  * the matching function using the [matcher] method).
128  *
129  * ```js
130  * var fn = mm.filter('[a-c]');
131  * ['a', 'b', 'c', 'd', 'e'].filter(fn);
132  * //=> ['a', 'b', 'c']
133  * ```
134  * @param  {String|Array} `patterns` Can be a glob or array of globs.
135  * @param  {Options} `opts` Options to pass to the [matcher] method.
136  * @return {Function} Filter function to be passed to `Array#filter()`.
137  */
138
139 function filter(patterns, opts) {
140   if (!Array.isArray(patterns) && typeof patterns !== 'string') {
141     throw new TypeError(msg('filter', 'patterns', 'a string or array'));
142   }
143
144   patterns = utils.arrayify(patterns);
145   var len = patterns.length, i = 0;
146   var patternMatchers = Array(len);
147   while (i < len) {
148     patternMatchers[i] = matcher(patterns[i++], opts);
149   }
150
151   return function(fp) {
152     if (fp == null) return [];
153     var len = patternMatchers.length, i = 0;
154     var res = true;
155
156     fp = utils.unixify(fp, opts);
157     while (i < len) {
158       var fn = patternMatchers[i++];
159       if (!fn(fp)) {
160         res = false;
161         break;
162       }
163     }
164     return res;
165   };
166 }
167
168 /**
169  * Returns true if the filepath contains the given
170  * pattern. Can also return a function for matching.
171  *
172  * ```js
173  * isMatch('foo.md', '*.md', {});
174  * //=> true
175  *
176  * isMatch('*.md', {})('foo.md')
177  * //=> true
178  * ```
179  * @param  {String} `fp`
180  * @param  {String} `pattern`
181  * @param  {Object} `opts`
182  * @return {Boolean}
183  */
184
185 function isMatch(fp, pattern, opts) {
186   if (typeof fp !== 'string') {
187     throw new TypeError(msg('isMatch', 'filepath', 'a string'));
188   }
189
190   fp = utils.unixify(fp, opts);
191   if (utils.typeOf(pattern) === 'object') {
192     return matcher(fp, pattern);
193   }
194   return matcher(pattern, opts)(fp);
195 }
196
197 /**
198  * Returns true if the filepath matches the
199  * given pattern.
200  */
201
202 function contains(fp, pattern, opts) {
203   if (typeof fp !== 'string') {
204     throw new TypeError(msg('contains', 'pattern', 'a string'));
205   }
206
207   opts = opts || {};
208   opts.contains = (pattern !== '');
209   fp = utils.unixify(fp, opts);
210
211   if (opts.contains && !utils.isGlob(pattern)) {
212     return fp.indexOf(pattern) !== -1;
213   }
214   return matcher(pattern, opts)(fp);
215 }
216
217 /**
218  * Returns true if a file path matches any of the
219  * given patterns.
220  *
221  * @param  {String} `fp` The filepath to test.
222  * @param  {String|Array} `patterns` Glob patterns to use.
223  * @param  {Object} `opts` Options to pass to the `matcher()` function.
224  * @return {String}
225  */
226
227 function any(fp, patterns, opts) {
228   if (!Array.isArray(patterns) && typeof patterns !== 'string') {
229     throw new TypeError(msg('any', 'patterns', 'a string or array'));
230   }
231
232   patterns = utils.arrayify(patterns);
233   var len = patterns.length;
234
235   fp = utils.unixify(fp, opts);
236   while (len--) {
237     var isMatch = matcher(patterns[len], opts);
238     if (isMatch(fp)) {
239       return true;
240     }
241   }
242   return false;
243 }
244
245 /**
246  * Filter the keys of an object with the given `glob` pattern
247  * and `options`
248  *
249  * @param  {Object} `object`
250  * @param  {Pattern} `object`
251  * @return {Array}
252  */
253
254 function matchKeys(obj, glob, options) {
255   if (utils.typeOf(obj) !== 'object') {
256     throw new TypeError(msg('matchKeys', 'first argument', 'an object'));
257   }
258
259   var fn = matcher(glob, options);
260   var res = {};
261
262   for (var key in obj) {
263     if (obj.hasOwnProperty(key) && fn(key)) {
264       res[key] = obj[key];
265     }
266   }
267   return res;
268 }
269
270 /**
271  * Return a function for matching based on the
272  * given `pattern` and `options`.
273  *
274  * @param  {String} `pattern`
275  * @param  {Object} `options`
276  * @return {Function}
277  */
278
279 function matcher(pattern, opts) {
280   // pattern is a function
281   if (typeof pattern === 'function') {
282     return pattern;
283   }
284   // pattern is a regex
285   if (pattern instanceof RegExp) {
286     return function(fp) {
287       return pattern.test(fp);
288     };
289   }
290
291   if (typeof pattern !== 'string') {
292     throw new TypeError(msg('matcher', 'pattern', 'a string, regex, or function'));
293   }
294
295   // strings, all the way down...
296   pattern = utils.unixify(pattern, opts);
297
298   // pattern is a non-glob string
299   if (!utils.isGlob(pattern)) {
300     return utils.matchPath(pattern, opts);
301   }
302   // pattern is a glob string
303   var re = makeRe(pattern, opts);
304
305   // `matchBase` is defined
306   if (opts && opts.matchBase) {
307     return utils.hasFilename(re, opts);
308   }
309   // `matchBase` is not defined
310   return function(fp) {
311     fp = utils.unixify(fp, opts);
312     return re.test(fp);
313   };
314 }
315
316 /**
317  * Create and cache a regular expression for matching
318  * file paths.
319  *
320  * If the leading character in the `glob` is `!`, a negation
321  * regex is returned.
322  *
323  * @param  {String} `glob`
324  * @param  {Object} `options`
325  * @return {RegExp}
326  */
327
328 function toRegex(glob, options) {
329   // clone options to prevent  mutating the original object
330   var opts = Object.create(options || {});
331   var flags = opts.flags || '';
332   if (opts.nocase && flags.indexOf('i') === -1) {
333     flags += 'i';
334   }
335
336   var parsed = expand(glob, opts);
337
338   // pass in tokens to avoid parsing more than once
339   opts.negated = opts.negated || parsed.negated;
340   opts.negate = opts.negated;
341   glob = wrapGlob(parsed.pattern, opts);
342   var re;
343
344   try {
345     re = new RegExp(glob, flags);
346     return re;
347   } catch (err) {
348     err.reason = 'micromatch invalid regex: (' + re + ')';
349     if (opts.strict) throw new SyntaxError(err);
350   }
351
352   // we're only here if a bad pattern was used and the user
353   // passed `options.silent`, so match nothing
354   return /$^/;
355 }
356
357 /**
358  * Create the regex to do the matching. If the leading
359  * character in the `glob` is `!` a negation regex is returned.
360  *
361  * @param {String} `glob`
362  * @param {Boolean} `negate`
363  */
364
365 function wrapGlob(glob, opts) {
366   var prefix = (opts && !opts.contains) ? '^' : '';
367   var after = (opts && !opts.contains) ? '$' : '';
368   glob = ('(?:' + glob + ')' + after);
369   if (opts && opts.negate) {
370     return prefix + ('(?!^' + glob + ').*$');
371   }
372   return prefix + glob;
373 }
374
375 /**
376  * Create and cache a regular expression for matching file paths.
377  * If the leading character in the `glob` is `!`, a negation
378  * regex is returned.
379  *
380  * @param  {String} `glob`
381  * @param  {Object} `options`
382  * @return {RegExp}
383  */
384
385 function makeRe(glob, opts) {
386   if (utils.typeOf(glob) !== 'string') {
387     throw new Error(msg('makeRe', 'glob', 'a string'));
388   }
389   return utils.cache(toRegex, glob, opts);
390 }
391
392 /**
393  * Make error messages consistent. Follows this format:
394  *
395  * ```js
396  * msg(methodName, argNumber, nativeType);
397  * // example:
398  * msg('matchKeys', 'first', 'an object');
399  * ```
400  *
401  * @param  {String} `method`
402  * @param  {String} `num`
403  * @param  {String} `type`
404  * @return {String}
405  */
406
407 function msg(method, what, type) {
408   return 'micromatch.' + method + '(): ' + what + ' should be ' + type + '.';
409 }
410
411 /**
412  * Public methods
413  */
414
415 /* eslint no-multi-spaces: 0 */
416 micromatch.any       = any;
417 micromatch.braces    = micromatch.braceExpand = utils.braces;
418 micromatch.contains  = contains;
419 micromatch.expand    = expand;
420 micromatch.filter    = filter;
421 micromatch.isMatch   = isMatch;
422 micromatch.makeRe    = makeRe;
423 micromatch.match     = match;
424 micromatch.matcher   = matcher;
425 micromatch.matchKeys = matchKeys;
426
427 /**
428  * Expose `micromatch`
429  */
430
431 module.exports = micromatch;