1 | anymatch [![Build Status](https://travis-ci.org/micromatch/anymatch.svg?branch=master)](https://travis-ci.org/micromatch/anymatch) [![Coverage Status](https://img.shields.io/coveralls/micromatch/anymatch.svg?branch=master)](https://coveralls.io/r/micromatch/anymatch?branch=master)
|
---|
2 | ======
|
---|
3 | Javascript module to match a string against a regular expression, glob, string,
|
---|
4 | or function that takes the string as an argument and returns a truthy or falsy
|
---|
5 | value. The matcher can also be an array of any or all of these. Useful for
|
---|
6 | allowing a very flexible user-defined config to define things like file paths.
|
---|
7 |
|
---|
8 | __Note: This module has Bash-parity, please be aware that Windows-style backslashes are not supported as separators. See https://github.com/micromatch/micromatch#backslashes for more information.__
|
---|
9 |
|
---|
10 |
|
---|
11 | Usage
|
---|
12 | -----
|
---|
13 | ```sh
|
---|
14 | npm install anymatch
|
---|
15 | ```
|
---|
16 |
|
---|
17 | #### anymatch(matchers, testString, [returnIndex], [options])
|
---|
18 | * __matchers__: (_Array|String|RegExp|Function_)
|
---|
19 | String to be directly matched, string with glob patterns, regular expression
|
---|
20 | test, function that takes the testString as an argument and returns a truthy
|
---|
21 | value if it should be matched, or an array of any number and mix of these types.
|
---|
22 | * __testString__: (_String|Array_) The string to test against the matchers. If
|
---|
23 | passed as an array, the first element of the array will be used as the
|
---|
24 | `testString` for non-function matchers, while the entire array will be applied
|
---|
25 | as the arguments for function matchers.
|
---|
26 | * __options__: (_Object_ [optional]_) Any of the [picomatch](https://github.com/micromatch/picomatch#options) options.
|
---|
27 | * __returnIndex__: (_Boolean [optional]_) If true, return the array index of
|
---|
28 | the first matcher that that testString matched, or -1 if no match, instead of a
|
---|
29 | boolean result.
|
---|
30 |
|
---|
31 | ```js
|
---|
32 | const anymatch = require('anymatch');
|
---|
33 |
|
---|
34 | const matchers = [ 'path/to/file.js', 'path/anyjs/**/*.js', /foo.js$/, string => string.includes('bar') && string.length > 10 ] ;
|
---|
35 |
|
---|
36 | anymatch(matchers, 'path/to/file.js'); // true
|
---|
37 | anymatch(matchers, 'path/anyjs/baz.js'); // true
|
---|
38 | anymatch(matchers, 'path/to/foo.js'); // true
|
---|
39 | anymatch(matchers, 'path/to/bar.js'); // true
|
---|
40 | anymatch(matchers, 'bar.js'); // false
|
---|
41 |
|
---|
42 | // returnIndex = true
|
---|
43 | anymatch(matchers, 'foo.js', {returnIndex: true}); // 2
|
---|
44 | anymatch(matchers, 'path/anyjs/foo.js', {returnIndex: true}); // 1
|
---|
45 |
|
---|
46 | // any picomatc
|
---|
47 |
|
---|
48 | // using globs to match directories and their children
|
---|
49 | anymatch('node_modules', 'node_modules'); // true
|
---|
50 | anymatch('node_modules', 'node_modules/somelib/index.js'); // false
|
---|
51 | anymatch('node_modules/**', 'node_modules/somelib/index.js'); // true
|
---|
52 | anymatch('node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // false
|
---|
53 | anymatch('**/node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // true
|
---|
54 |
|
---|
55 | const matcher = anymatch(matchers);
|
---|
56 | ['foo.js', 'bar.js'].filter(matcher); // [ 'foo.js' ]
|
---|
57 | anymatch master* ❯
|
---|
58 |
|
---|
59 | ```
|
---|
60 |
|
---|
61 | #### anymatch(matchers)
|
---|
62 | You can also pass in only your matcher(s) to get a curried function that has
|
---|
63 | already been bound to the provided matching criteria. This can be used as an
|
---|
64 | `Array#filter` callback.
|
---|
65 |
|
---|
66 | ```js
|
---|
67 | var matcher = anymatch(matchers);
|
---|
68 |
|
---|
69 | matcher('path/to/file.js'); // true
|
---|
70 | matcher('path/anyjs/baz.js', true); // 1
|
---|
71 |
|
---|
72 | ['foo.js', 'bar.js'].filter(matcher); // ['foo.js']
|
---|
73 | ```
|
---|
74 |
|
---|
75 | Changelog
|
---|
76 | ----------
|
---|
77 | [See release notes page on GitHub](https://github.com/micromatch/anymatch/releases)
|
---|
78 |
|
---|
79 | - **v3.0:** Removed `startIndex` and `endIndex` arguments. Node 8.x-only.
|
---|
80 | - **v2.0:** [micromatch](https://github.com/jonschlinkert/micromatch) moves away from minimatch-parity and inline with Bash. This includes handling backslashes differently (see https://github.com/micromatch/micromatch#backslashes for more information).
|
---|
81 | - **v1.2:** anymatch uses [micromatch](https://github.com/jonschlinkert/micromatch)
|
---|
82 | for glob pattern matching. Issues with glob pattern matching should be
|
---|
83 | reported directly to the [micromatch issue tracker](https://github.com/jonschlinkert/micromatch/issues).
|
---|
84 |
|
---|
85 | License
|
---|
86 | -------
|
---|
87 | [ISC](https://raw.github.com/micromatch/anymatch/master/LICENSE)
|
---|