What is anymatch?
The anymatch npm package is a utility that allows you to match strings against a variety of patterns including strings, regexes, and functions. It is commonly used to test whether a given string matches any of a set of criteria, which is useful for tasks like filtering file paths or determining if an event should trigger a particular action.
What are anymatch's main functionalities?
String pattern matching
Match a string against a single glob pattern.
"foo.js".match(anymatch('*.js'))
Array of patterns matching
Match a string against an array of glob patterns.
anymatch(['*.js', '*.css'], 'foo.js')
Function as a custom matcher
Use a function as a custom matcher in the array of patterns.
anymatch([/\.js$/, (string) => string.includes('foo')], 'foo.js')
Negated patterns
Use negated patterns to exclude matches.
anymatch(['*.js', '!foo.js'], 'foo.js')
Partial application
Create a partially applied matcher function for reuse.
const isJsFile = anymatch('*.js');
isJsFile('foo.js');
Other packages similar to anymatch
micromatch
Micromatch is a glob matching library that offers a variety of powerful features and optimizations. It is more extensive than anymatch, providing more fine-grained control over pattern matching and glob expansion.
minimatch
Minimatch is a minimal matching utility that implements glob matching in JavaScript. It is the matcher used by npm itself and is similar to anymatch but with a simpler API and fewer features.
multimatch
Multimatch extends minimatch to allow multiple patterns to be specified. It is similar to anymatch in that it can match against multiple patterns, but it is built on top of minimatch.
picomatch
Picomatch is a small, fast, and powerful glob matcher with a simple API. It is similar to anymatch but focuses on performance and is suitable for runtime usage.
anymatch
Javascript module to match a string against a regular expression, glob, string,
or function that takes the string as an argument and returns a truthy or falsy
value. The matcher can also be an array of any or all of these. Useful for
allowing a very flexible user-defined config to define things like file paths.
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.
Usage
npm install anymatch
anymatch(matchers, testString, [returnIndex], [options])
- matchers: (Array|String|RegExp|Function)
String to be directly matched, string with glob patterns, regular expression
test, function that takes the testString as an argument and returns a truthy
value if it should be matched, or an array of any number and mix of these types.
- testString: (String|Array) The string to test against the matchers. If
passed as an array, the first element of the array will be used as the
testString
for non-function matchers, while the entire array will be applied
as the arguments for function matchers. - options: (Object [optional]_) Any of the picomatch options.
- returnIndex: (Boolean [optional]) If true, return the array index of
the first matcher that that testString matched, or -1 if no match, instead of a
boolean result.
const anymatch = require('anymatch');
const matchers = [ 'path/to/file.js', 'path/anyjs/**/*.js', /foo.js$/, string => string.includes('bar') && string.length > 10 ] ;
anymatch(matchers, 'path/to/file.js');
anymatch(matchers, 'path/anyjs/baz.js');
anymatch(matchers, 'path/to/foo.js');
anymatch(matchers, 'path/to/bar.js');
anymatch(matchers, 'bar.js');
anymatch(matchers, 'foo.js', {returnIndex: true});
anymatch(matchers, 'path/anyjs/foo.js', {returnIndex: true});
anymatch('node_modules', 'node_modules');
anymatch('node_modules', 'node_modules/somelib/index.js');
anymatch('node_modules/**', 'node_modules/somelib/index.js');
anymatch('node_modules/**', '/absolute/path/to/node_modules/somelib/index.js');
anymatch('**/node_modules/**', '/absolute/path/to/node_modules/somelib/index.js');
const matcher = anymatch(matchers);
['foo.js', 'bar.js'].filter(matcher);
anymatch master* ❯
anymatch(matchers)
You can also pass in only your matcher(s) to get a curried function that has
already been bound to the provided matching criteria. This can be used as an
Array#filter
callback.
var matcher = anymatch(matchers);
matcher('path/to/file.js');
matcher('path/anyjs/baz.js', true);
['foo.js', 'bar.js'].filter(matcher);
Changelog
See release notes page on GitHub
License
ISC