• Stars
    star
    388
  • Rank 110,734 (Top 3 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created over 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

anymatch Build Status Coverage Status

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'); // 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', {returnIndex: true}); // 2
anymatch(matchers, 'path/anyjs/foo.js', {returnIndex: true}); // 1

// any picomatc

// using globs to match directories and their children
anymatch('node_modules', 'node_modules'); // true
anymatch('node_modules', 'node_modules/somelib/index.js'); // false
anymatch('node_modules/**', 'node_modules/somelib/index.js'); // true
anymatch('node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // false
anymatch('**/node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // true

const matcher = anymatch(matchers);
['foo.js', 'bar.js'].filter(matcher);  // [ 'foo.js' ]
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'); // true
matcher('path/anyjs/baz.js', true); // 1

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

Changelog

See release notes page on GitHub

License

ISC

More Repositories

1

micromatch

Highly optimized wildcard and glob matching library. Faster, drop-in replacement to minimatch and multimatch. Used by square, webpack, babel core, yarn, jest, ract-native, taro, bulma, browser-sync, stylelint, nyc, ava, and many others! Follow micromatch's author: https://github.com/jonschlinkert
JavaScript
2,781
star
2

picomatch

Blazing fast and accurate glob matcher written JavaScript, with no dependencies and full support for standard and extended Bash glob features, including braces, extglobs, POSIX brackets, and regular expressions. Used by GraphQL, Jest, Astro, Snowpack, Storybook, bulma, Serverless, fdir, Netlify, AWS Amplify, Revogrid, rollup, routify, open-wc, imba, ava, docusaurus, fast-glob, globby, chokidar, anymatch, cloudflare/miniflare, pts, and more than 5 million projects! Please follow picomatch's author: https://github.com/jonschlinkert
JavaScript
744
star
3

braces

Faster brace expansion for node.js. Besides being faster, braces is not subject to DoS attacks like minimatch, is more accurate, and has more complete support for Bash 4.3.
JavaScript
220
star
4

to-regex-range

Pass two numbers, get a regex-compatible source string for matching ranges. Fast compiler, optimized regex, and validated against more than 2.78 million test assertions. Useful for creating regular expressions to validate numbers, ranges, years, etc.
JavaScript
126
star
5

nanomatch

Fast, minimal glob matcher for node.js. Similar to micromatch, minimatch and multimatch, but without support for extended globs (extglobs), posix brackets or braces, and with complete Bash 4.3 wildcard support: ("*", "**", and "?").
JavaScript
95
star
6

is-glob

If you use globs, this will make your code faster. 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 better user experience. 55+ million downloads.
JavaScript
91
star
7

glob-fs

file globbing for node.js. speedy and powerful alternative to node-glob. This library is experimental and does not work on windows!
JavaScript
54
star
8

extglob

Extended globs. Add (almost) the expressive power of regular expressions to glob patterns.
JavaScript
31
star
9

expand-brackets

Expand POSIX bracket expressions (character classes) in glob patterns.
JavaScript
27
star
10

parse-glob

Parse a glob pattern into an object of path parts.
JavaScript
25
star
11

posix-character-classes

POSIX character classes for creating regular expressions.
JavaScript
25
star
12

is-valid-glob

Return true if a value is a valid glob pattern string, or array of glob patterns.
JavaScript
23
star
13

is-extglob

Returns true if a string has an extglob
JavaScript
20
star
14

to-absolute-glob

Make a glob pattern absolute, ensuring that negative globs and patterns with trailing slashes are correctly handled.
JavaScript
18
star
15

glob-base

Returns an object with the base path and the actual pattern from a glob.
JavaScript
17
star
16

bash-glob

Bash-powered globbing for node.js. Alternative to node-glob. Does not work on Windows 9 and lower.
JavaScript
13
star
17

is-posix-bracket

Returns true if the given string is a POSIX bracket expression (POSIX character class)
JavaScript
12
star
18

expand-braces

Wrapper for [braces] to enable brace expansion for arrays of patterns.
JavaScript
7
star
19

has-glob

Returns `true` if an array has a glob pattern.
JavaScript
7
star
20

is-negated-glob

Returns an object with a `negated` boolean and the `!` stripped from negation patterns. Also respects extglobs.
JavaScript
7
star
21

bash-match

Match strings using bash. Does not work on windows, and does not read from the file system. This library requires that Bash 4.3 or higher is installed and is mostly used for checking parity in unit tests.
JavaScript
6
star
22

bash-path

Get the path to the bash binary on your OS.
JavaScript
6
star
23

resolve-glob

Ensures that absolute file paths are always returned from a glob pattern or array of glob patterns.
JavaScript
5
star
24

glob-spec

Specification for glob-matching in JavaScript.
4
star
25

.github

Default files for the .github directory of all micromatch projects.
3
star