Socket
Socket
Sign inDemoInstall

anymatch

Package Overview
Dependencies
3
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    anymatch

Matches strings against configurable strings, globs, regular expressions, and/or functions


Version published
Maintainers
1
Install size
116 kB
Created

Package description

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

Readme

Source

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.

Usage

npm install anymatch --save

anymatch (matchers, testString, [returnIndex], [startIndex], [endIndex])
  • 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) The string to test against the matchers.
  • 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.
  • startIndex, endIndex: (Integer [optional]) Can be used to define a subset out of the array of provided matchers to test against. Can be useful with bound matcher functions (see below). When used with returnIndex = true preserves original indexing. Behaves the same as Array.prototype.slice (i.e. includes array members up to, but not including endIndex).
var anymatch = require('anymatch');

var matchers = [
	'path/to/file.js',
	'path/anyjs/**/*.js',
	/foo.js$/,
	function (string) {
		return string.indexOf('bar') !== -1 && string.length > 10
	}
];

anymatch(matchers, 'path/to/file.js'); // true
anymatch(matchers, 'path/anyjs/baz.js'); // true
anymatch(matchers, 'path/to/foo.js'); // true
anymatch(matchers, 'path/to/bar.js'); // true
anymatch(matchers, 'bar.js'); // false

// returnIndex = true
anymatch(matchers, 'foo.js', true); // 2
anymatch(matchers, 'path/anyjs/foo.js', true); // 1

// skip matchers
anymatch(matchers, 'path/to/file.js', false, 1); // false
anymatch(matchers, 'path/anyjs/foo.js', true, 2, 3); // 2
anymatch(matchers, 'path/to/bar.js', true, 0, 3); // -1
anymatch.matcher (matchers)

You can also use the matcher method to get a function that has already been bound to your matcher(s). This can be used as an Array.prototype.filter callback.

var matcher = anymatch.matcher(matchers);

matcher('path/to/file.js'); // true
matcher('path/anyjs/baz.js', true); // 1
matcher('path/anyjs/baz.js', true, 2); // -1

['foo.js', 'bar.js'].filter(matcher); // ['foo.js']

License

MIT

Keywords

FAQs

Last updated on 19 Feb 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc