Initial commit
[yaffs-website] / node_modules / micromatch / README.md
1 # micromatch [![NPM version](https://img.shields.io/npm/v/micromatch.svg?style=flat)](https://www.npmjs.com/package/micromatch) [![NPM downloads](https://img.shields.io/npm/dm/micromatch.svg?style=flat)](https://npmjs.org/package/micromatch) [![Build Status](https://img.shields.io/travis/jonschlinkert/micromatch.svg?style=flat)](https://travis-ci.org/jonschlinkert/micromatch)
2
3 > Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.
4
5 Micromatch supports all of the same matching features as [minimatch](https://github.com/isaacs/minimatch) and [multimatch](https://github.com/sindresorhus/multimatch).
6
7 * [mm()](#usage) is the same as [multimatch()](https://github.com/sindresorhus/multimatch)
8 * [mm.match()](#match) is the same as [minimatch.match()](https://github.com/isaacs/minimatch)
9 * use [mm.isMatch()](#ismatch) instead of [minimatch()](https://github.com/isaacs/minimatch)
10
11 ## Install
12
13 Install with [npm](https://www.npmjs.com/):
14
15 ```sh
16 $ npm install --save micromatch
17 ```
18
19 ## Start matching!
20
21 ```js
22 var mm = require('micromatch');
23 console.log(mm(['']))
24 ```
25
26 ***
27
28 ### Features
29
30 * [Drop-in replacement](#switch-from-minimatch) for [minimatch](https://github.com/isaacs/minimatch) and [multimatch](https://github.com/sindresorhus/multimatch)
31 * Built-in support for multiple glob patterns, like `['foo/*.js', '!bar.js']`
32 * [Brace Expansion](https://github.com/jonschlinkert/braces) (`foo/bar-{1..5}.md`, `one/{two,three}/four.md`)
33 * Typical glob patterns, like `**/*`, `a/b/*.js`, or `['foo/*.js', '!bar.js']`
34 * Methods like `.isMatch()`, `.contains()` and `.any()`
35
36 **Extended globbing features:**
37
38 * Logical `OR` (`foo/bar/(abc|xyz).js`)
39 * Regex character classes (`foo/bar/baz-[1-5].js`)
40 * POSIX [bracket expressions](https://github.com/jonschlinkert/expand-brackets) (`**/[[:alpha:][:digit:]]/`)
41 * [extglobs](https://github.com/jonschlinkert/extglob) (`**/+(x|y)`, `!(a|b)`, etc).
42
43 You can combine these to create whatever matching patterns you need.
44
45 **Example**
46
47 ```js
48 // double-negation!
49 mm(['fa', 'fb', 'f', 'fo'], '!(f!(o))');
50 //=> ['fo']
51 ```
52
53 ## Why switch to micromatch?
54
55 * Native support for multiple glob patterns, no need for wrappers like [multimatch](https://github.com/sindresorhus/multimatch)
56 * [10-55x faster](#benchmarks) and more performant than [minimatch](https://github.com/isaacs/minimatch) and [multimatch](https://github.com/sindresorhus/multimatch). This is achieved through a combination of caching and regex optimization strategies, a fundamentally different approach than minimatch.
57 * More extensive support for the Bash 4.3 specification
58 * More complete extglob support
59 * Extensive [unit tests](./test) (approx. 1,300 tests). Minimatch fails many of the tests.
60
61 ### Switch from minimatch
62
63 Use `mm.isMatch()` instead of `minimatch()`:
64
65 ```js
66 mm.isMatch('foo', 'b*');
67 //=> false
68 ```
69
70 Use `mm.match()` instead of `minimatch.match()`:
71
72 ```js
73 mm.match(['foo', 'bar'], 'b*');
74 //=> 'bar'
75 ```
76
77 ### Switch from multimatch
78
79 Same signature:
80
81 ```js
82 mm(['foo', 'bar', 'baz'], ['f*', '*z']);
83 //=> ['foo', 'baz']
84 ```
85
86 ***
87
88 ## Usage
89
90 Add micromatch to your node.js project:
91
92 ```js
93 var mm = require('micromatch');
94 ```
95
96 **Signature**
97
98 ```js
99 mm(array_of_strings, glob_patterns[, options]);
100 ```
101
102 **Example**
103
104 ```js
105 mm(['foo', 'bar', 'baz'], 'b*');
106 //=> ['bar', 'baz']
107 ```
108
109 ### Usage examples
110
111 **Brace expansion**
112
113 Match files with `.js` or `.txt` extensions.
114
115 ```js
116 mm(['a.js', 'b.md', 'c.txt'], '*.{js,txt}');
117 //=> ['a.js', 'c.txt']
118 ```
119
120 **Extglobs**
121
122 Match anything except for files with the `.md` extension.
123
124 ```js
125 mm(files, '**/*.!(md)');
126
127 //=> ['a.js', 'c.txt']
128 ```
129
130 **Multiple patterns**
131
132 Match using an array of patterns.
133
134 ```js
135 mm(['a.md', 'b.js', 'c.txt', 'd.json'], ['*.md', '*.txt']);
136 //=> ['a.md', 'c.txt']
137 ```
138
139 **Negation patterns:**
140
141 Behavior is designed to be what users would expect, based on conventions that are already well-established.
142
143 * [minimatch](https://github.com/isaacs/minimatch) behavior is used when the pattern is a string, so patterns are **inclusive by default**.
144 * [multimatch](https://github.com/sindresorhus/multimatch) behavior is used when an array of patterns is passed, so patterns are **exclusive by default**.
145
146 ```js
147 mm(['a.js', 'b.md', 'c.txt'], '!*.{js,txt}');
148 //=> ['b.md']
149
150 mm(['a.md', 'b.js', 'c.txt', 'd.json'], ['*.*', '!*.{js,txt}']);
151 //=> ['a.md', 'd.json']
152 ```
153
154 ***
155
156 ## API methods
157
158 ```js
159 var mm = require('micromatch');
160 ```
161
162 ### .match
163
164 ```js
165 mm.match(array, globString);
166 ```
167
168 Return an array of files that match the given glob pattern. Useful if you only need to use a single glob pattern.
169
170 **Example**
171
172 ```js
173 mm.match(['ab', 'a/b', 'bb', 'b/c'], '?b');
174 //=> ['ab', 'bb']
175
176 mm.match(['ab', 'a/b', 'bb', 'b/c'], '*/b');
177 //=> ['a/b']
178 ```
179
180 ### .isMatch
181
182 ```js
183 mm.isMatch(filepath, globString);
184 ```
185
186 Returns true if a file path matches the given glob pattern.
187
188 **Example**
189
190 ```js
191 mm.isMatch('.verb.md', '*.md');
192 //=> false
193
194 mm.isMatch('.verb.md', '*.md', {dot: true});
195 //=> true
196 ```
197
198 ### .contains
199
200 Returns true if any part of a file path matches the given glob pattern. Think of this is "has path" versus "is path".
201
202 **Example**
203
204 `.isMatch()` would return false for both of the following:
205
206 ```js
207 mm.contains('a/b/c', 'a/b');
208 //=> true
209
210 mm.contains('a/b/c', 'a/*');
211 //=> true
212 ```
213
214 ### .matcher
215
216 Returns a function for matching using the supplied pattern. e.g. create your own "matcher". The advantage of this method is that the pattern can be compiled outside of a loop.
217
218 **Pattern**
219
220 Can be any of the following:
221
222 * `glob/string`
223 * `regex`
224 * `function`
225
226 **Example**
227
228 ```js
229 var isMatch = mm.matcher('*.md');
230 var files = [];
231
232 ['a.md', 'b.txt', 'c.md'].forEach(function(fp) {
233   if (isMatch(fp)) {
234     files.push(fp);
235   }
236 });
237 ```
238
239 ### .filter
240
241 Returns a function that can be passed to `Array#filter()`.
242
243 **Params**
244
245 * `patterns` **{String|Array}**:
246
247 **Examples**
248
249 Single glob:
250
251 ```js
252 var fn = mm.filter('*.md');
253 ['a.js', 'b.txt', 'c.md'].filter(fn);
254 //=> ['c.md']
255
256 var fn = mm.filter('[a-c]');
257 ['a', 'b', 'c', 'd', 'e'].filter(fn);
258 //=> ['a', 'b', 'c']
259 ```
260
261 Array of glob patterns:
262
263 ```js
264 var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
265
266 var fn = mm.filter(['{1..10}', '![7-9]', '!{3..4}']);
267 arr.filter(fn);
268 //=> [1, 2, 5, 6, 10]
269 ```
270
271 _(Internally this function generates the matching function by using the [matcher](#matcher) method. You can use the [matcher](#matcher) method directly to create your own filter function)_
272
273 ### .any
274
275 Returns true if a file path matches any of the given patterns.
276
277 ```js
278 mm.any(filepath, patterns, options);
279 ```
280
281 **Params**
282
283 * filepath `{String}`: The file path to test.
284 * patterns `{String|Array}`: One or more glob patterns
285 * options: `{Object}`: options to pass to the `.matcher()` method.
286
287 **Example**
288
289 ```js
290 mm.any('abc', ['!*z']);
291 //=> true
292 mm.any('abc', ['a*', 'z*']);
293 //=> true
294 mm.any('abc', 'a*');
295 //=> true
296 mm.any('abc', ['z*']);
297 //=> false
298 ```
299
300 ### .expand
301
302 Returns an object with a regex-compatible string and tokens.
303
304 ```js
305 mm.expand('*.js');
306
307 // when `track` is enabled (for debugging), the `history` array is used
308 // to record each mutation to the glob pattern as it's converted to regex
309 { options: { track: false, dot: undefined, makeRe: true, negated: false },
310   pattern: '(.*\\/|^)bar\\/(?:(?!(?:^|\\/)\\.).)*?',
311   history: [],
312   tokens:
313    { path:
314       { whole: '**/bar/**',
315         dirname: '**/bar/',
316         filename: '**',
317         basename: '**',
318         extname: '',
319         ext: '' },
320      is:
321       { glob: true,
322         negated: false,
323         globstar: true,
324         dotfile: false,
325         dotdir: false },
326      match: {},
327      original: '**/bar/**',
328      pattern: '**/bar/**',
329      base: '' } }
330 ```
331
332 ### .makeRe
333
334 Create a regular expression for matching file paths based on the given pattern:
335
336 ```js
337 mm.makeRe('*.js');
338 //=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/
339 ```
340
341 ## Options
342
343 ### options.unixify
344
345 Normalize slashes in file paths and glob patterns to forward slashes.
346
347 Type: `{Boolean}`
348
349 Default: `undefined` on non-windows, `true` on windows.
350
351 ### options.dot
352
353 Match dotfiles. Same behavior as [minimatch](https://github.com/isaacs/minimatch).
354
355 Type: `{Boolean}`
356
357 Default: `false`
358
359 ### options.unescape
360
361 Unescape slashes in glob patterns. Use cautiously, especially on windows.
362
363 Type: `{Boolean}`
364
365 Default: `undefined`
366
367 **Example**
368
369 ```js
370 mm.isMatch('abc', '\\a\\b\\c', {unescape: true});
371 //=> true
372 ```
373
374 ### options.nodupes
375
376 Remove duplicate elements from the result array.
377
378 Type: `{Boolean}`
379
380 Default: `undefined`
381
382 **Example**
383
384 Example of using the `unescape` and `nodupes` options together:
385
386 ```js
387 mm.match(['abc', '\\a\\b\\c'], '\\a\\b\\c', {unescape: true});
388 //=> ['abc', 'abc']
389
390 mm.match(['abc', '\\a\\b\\c'], '\\a\\b\\c', {unescape: true, nodupes: true});
391 //=> ['abc']
392 ```
393
394 ### options.matchBase
395
396 Allow glob patterns without slashes to match a file path based on its basename. . Same behavior as [minimatch](https://github.com/isaacs/minimatch).
397
398 Type: `{Boolean}`
399
400 Default: `false`
401
402 **Example**
403
404 ```js
405 mm(['a/b.js', 'a/c.md'], '*.js');
406 //=> []
407
408 mm(['a/b.js', 'a/c.md'], '*.js', {matchBase: true});
409 //=> ['a/b.js']
410 ```
411
412 ### options.nobraces
413
414 Don't expand braces in glob patterns. Same behavior as [minimatch](https://github.com/isaacs/minimatch) `nobrace`.
415
416 Type: `{Boolean}`
417
418 Default: `undefined`
419
420 See [braces](https://github.com/jonschlinkert/braces) for more information about extended brace expansion.
421
422 ### options.nobrackets
423
424 Don't expand POSIX bracket expressions.
425
426 Type: `{Boolean}`
427
428 Default: `undefined`
429
430 See [expand-brackets](https://github.com/jonschlinkert/expand-brackets) for more information about extended bracket expressions.
431
432 ### options.noextglob
433
434 Don't expand extended globs.
435
436 Type: `{Boolean}`
437
438 Default: `undefined`
439
440 See [extglob](https://github.com/jonschlinkert/extglob) for more information about extended globs.
441
442 ### options.nocase
443
444 Use a case-insensitive regex for matching files. Same behavior as [minimatch](https://github.com/isaacs/minimatch).
445
446 Type: `{Boolean}`
447
448 Default: `false`
449
450 ### options.nonegate
451
452 Disallow negation (`!`) patterns.
453
454 Type: `{Boolean}`
455
456 Default: `false`
457
458 ### options.nonull
459
460 If `true`, when no matches are found the actual (array-ified) glob pattern is returned instead of an empty array. Same behavior as [minimatch](https://github.com/isaacs/minimatch).
461
462 Type: `{Boolean}`
463
464 Default: `false`
465
466 ### options.cache
467
468 Cache the platform (e.g. `win32`) to prevent this from being looked up for every filepath.
469
470 Type: `{Boolean}`
471
472 Default: `true`
473
474 ***
475
476 ## Other features
477
478 Micromatch also supports the following.
479
480 ### Extended globbing
481
482 #### extglobs
483
484 Extended globbing, as described by the bash man page:
485
486 | **pattern** | **regex equivalent** | **description** | 
487 | --- | --- | --- |
488 | `?(pattern-list)` | `(... | ...)?` | Matches zero or one occurrence of the given patterns |
489 | `*(pattern-list)` | `(... | ...)*` | Matches zero or more occurrences of the given patterns |
490 | `+(pattern-list)` | `(... | ...)+` | Matches one or more occurrences of the given patterns |
491 | `@(pattern-list)` | `(... | ...)` <sup>*</sup> | Matches one of the given patterns |
492 | `!(pattern-list)` | N/A | Matches anything except one of the given patterns |
493
494 <sup><strong>*</strong></sup> `@` isn't a RegEx character.
495
496 Powered by [extglob](https://github.com/jonschlinkert/extglob). Visit that library for the full range of options or to report extglob related issues.
497
498 See [extglob](https://github.com/jonschlinkert/extglob) for more information about extended globs.
499
500 #### brace expansion
501
502 In simple cases, brace expansion appears to work the same way as the logical `OR` operator. For example, `(a|b)` will achieve the same result as `{a,b}`.
503
504 Here are some powerful features unique to brace expansion (versus character classes):
505
506 * range expansion: `a{1..3}b/*.js` expands to: `['a1b/*.js', 'a2b/*.js', 'a3b/*.js']`
507 * nesting: `a{c,{d,e}}b/*.js` expands to: `['acb/*.js', 'adb/*.js', 'aeb/*.js']`
508
509 Visit [braces](https://github.com/jonschlinkert/braces) to ask questions and create an issue related to brace-expansion, or to see the full range of features and options related to brace expansion.
510
511 #### regex character classes
512
513 With the exception of brace expansion (`{a,b}`, `{1..5}`, etc), most of the special characters convert directly to regex, so you can expect them to follow the same rules and produce the same results as regex.
514
515 For example, given the list: `['a.js', 'b.js', 'c.js', 'd.js', 'E.js']`:
516
517 * `[ac].js`: matches both `a` and `c`, returning `['a.js', 'c.js']`
518 * `[b-d].js`: matches from `b` to `d`, returning `['b.js', 'c.js', 'd.js']`
519 * `[b-d].js`: matches from `b` to `d`, returning `['b.js', 'c.js', 'd.js']`
520 * `a/[A-Z].js`: matches and uppercase letter, returning `['a/E.md']`
521
522 Learn about [regex character classes](http://www.regular-expressions.info/charclass.html).
523
524 #### regex groups
525
526 Given `['a.js', 'b.js', 'c.js', 'd.js', 'E.js']`:
527
528 * `(a|c).js`: would match either `a` or `c`, returning `['a.js', 'c.js']`
529 * `(b|d).js`: would match either `b` or `d`, returning `['b.js', 'd.js']`
530 * `(b|[A-Z]).js`: would match either `b` or an uppercase letter, returning `['b.js', 'E.js']`
531
532 As with regex, parenthese can be nested, so patterns like `((a|b)|c)/b` will work. But it might be easier to achieve your goal using brace expansion.
533
534 #### POSIX bracket expressions
535
536 **Example**
537
538 ```js
539 mm.isMatch('a1', '[[:alpha:][:digit:]]');
540 //=> true
541 ```
542
543 See [expand-brackets](https://github.com/jonschlinkert/expand-brackets) for more information about extended bracket expressions.
544
545 ***
546
547 ## Notes
548
549 Whenever possible parsing behavior for patterns is based on globbing specifications in Bash 4.3. Patterns that aren't described by Bash follow wildmatch spec (used by git).
550
551 ## Benchmarks
552
553 Run the [benchmarks](./benchmark):
554
555 ```bash
556 node benchmark
557 ```
558
559 As of July 15, 2016:
560
561 ```bash
562 #1: basename-braces
563   micromatch x 26,420 ops/sec ±0.89% (91 runs sampled)
564   minimatch x 3,507 ops/sec ±0.64% (97 runs sampled)
565
566 #2: basename
567   micromatch x 25,315 ops/sec ±0.82% (93 runs sampled)
568   minimatch x 4,398 ops/sec ±0.86% (94 runs sampled)
569
570 #3: braces-no-glob
571   micromatch x 341,254 ops/sec ±0.78% (93 runs sampled)
572   minimatch x 30,197 ops/sec ±1.12% (91 runs sampled)
573
574 #4: braces
575   micromatch x 54,649 ops/sec ±0.74% (94 runs sampled)
576   minimatch x 3,095 ops/sec ±0.82% (95 runs sampled)
577
578 #5: immediate
579   micromatch x 16,719 ops/sec ±0.79% (95 runs sampled)
580   minimatch x 4,348 ops/sec ±0.86% (96 runs sampled)
581
582 #6: large
583   micromatch x 721 ops/sec ±0.77% (94 runs sampled)
584   minimatch x 17.73 ops/sec ±1.08% (50 runs sampled)
585
586 #7: long
587   micromatch x 5,051 ops/sec ±0.87% (97 runs sampled)
588   minimatch x 628 ops/sec ±0.83% (94 runs sampled)
589
590 #8: mid
591   micromatch x 51,280 ops/sec ±0.80% (95 runs sampled)
592   minimatch x 1,923 ops/sec ±0.84% (95 runs sampled)
593
594 #9: multi-patterns
595   micromatch x 22,440 ops/sec ±0.97% (94 runs sampled)
596   minimatch x 2,481 ops/sec ±1.10% (94 runs sampled)
597
598 #10: no-glob
599   micromatch x 722,823 ops/sec ±1.30% (87 runs sampled)
600   minimatch x 52,967 ops/sec ±1.09% (94 runs sampled)
601
602 #11: range
603   micromatch x 243,471 ops/sec ±0.79% (94 runs sampled)
604   minimatch x 11,736 ops/sec ±0.82% (96 runs sampled)
605
606 #12: shallow
607   micromatch x 190,874 ops/sec ±0.98% (95 runs sampled)
608   minimatch x 21,699 ops/sec ±0.81% (97 runs sampled)
609
610 #13: short
611   micromatch x 496,393 ops/sec ±3.86% (90 runs sampled)
612   minimatch x 53,765 ops/sec ±0.75% (95 runs sampled)
613 ```
614
615 ## Tests
616
617 ### Running tests
618
619 Install dev dependencies:
620
621 ```sh
622 $ npm install -d && npm test
623 ```
624
625 ### Coverage
626
627 As of July 15, 2016:
628
629 ```sh
630 Statements : 100% (441/441)
631 Branches   : 100% (270/270)
632 Functions  : 100% (54/54)
633 Lines      : 100% (429/429)
634 ```
635
636 ## Contributing
637
638 Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
639
640 Please be sure to run the benchmarks before/after any code changes to judge the impact before you do a PR. thanks!
641
642 ## Related
643
644 * [braces](https://www.npmjs.com/package/braces): Fastest brace expansion for node.js, with the most complete support for the Bash 4.3 braces… [more](https://github.com/jonschlinkert/braces) | [homepage](https://github.com/jonschlinkert/braces "Fastest brace expansion for node.js, with the most complete support for the Bash 4.3 braces specification.")
645 * [expand-brackets](https://www.npmjs.com/package/expand-brackets): Expand POSIX bracket expressions (character classes) in glob patterns. | [homepage](https://github.com/jonschlinkert/expand-brackets "Expand POSIX bracket expressions (character classes) in glob patterns.")
646 * [expand-range](https://www.npmjs.com/package/expand-range): Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. See… [more](https://github.com/jonschlinkert/expand-range) | [homepage](https://github.com/jonschlinkert/expand-range "Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. See the benchmarks. Used by micromatch.")
647 * [extglob](https://www.npmjs.com/package/extglob): Convert extended globs to regex-compatible strings. Add (almost) the expressive power of regular expressions to… [more](https://github.com/jonschlinkert/extglob) | [homepage](https://github.com/jonschlinkert/extglob "Convert extended globs to regex-compatible strings. Add (almost) the expressive power of regular expressions to glob patterns.")
648 * [fill-range](https://www.npmjs.com/package/fill-range): Fill in a range of numbers or letters, optionally passing an increment or multiplier to… [more](https://github.com/jonschlinkert/fill-range) | [homepage](https://github.com/jonschlinkert/fill-range "Fill in a range of numbers or letters, optionally passing an increment or multiplier to use.")
649 * [gulp-micromatch](https://www.npmjs.com/package/gulp-micromatch): Filter vinyl files with glob patterns, string, regexp, array, object or matcher function. micromatch stream. | [homepage](https://github.com/tunnckocore/gulp-micromatch#readme "Filter vinyl files with glob patterns, string, regexp, array, object or matcher function. micromatch stream.")
650 * [is-glob](https://www.npmjs.com/package/is-glob): Returns `true` if the given string looks like a glob pattern or an extglob pattern… [more](https://github.com/jonschlinkert/is-glob) | [homepage](https://github.com/jonschlinkert/is-glob "Returns `true` if the given string looks like a glob pattern or an extglob pattern. This makes it easy to create code that only uses external modules like node-glob when necessary, resulting in much faster code execution and initialization time, and a bet")
651 * [parse-glob](https://www.npmjs.com/package/parse-glob): Parse a glob pattern into an object of tokens. | [homepage](https://github.com/jonschlinkert/parse-glob "Parse a glob pattern into an object of tokens.")
652
653 ## Contributing
654
655 Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
656
657 ## Building docs
658
659 _(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_
660
661 To generate the readme and API documentation with [verb](https://github.com/verbose/verb):
662
663 ```sh
664 $ npm install -g verb verb-generate-readme && verb
665 ```
666
667 ## Running tests
668
669 Install dev dependencies:
670
671 ```sh
672 $ npm install -d && npm test
673 ```
674
675 ## Author
676
677 **Jon Schlinkert**
678
679 * [github/jonschlinkert](https://github.com/jonschlinkert)
680 * [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
681
682 ## License
683
684 Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
685 Released under the [MIT license](https://github.com/jonschlinkert/micromatch/blob/master/LICENSE).
686
687 ***
688
689 _This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on July 15, 2016._