A tiny and extremely fast JavaScript library for compiling and matching basic glob patterns
| 🍃 | Lightweight No dependencies. Less than 1 KB when minified and gzipped |
| 🏎 | Fast Compiles and matches patterns faster than any other known library |
| 🌞 | Simple The API is a single function |
| ⚒ | Reliable Written in TypeScript. Covered by hundreds of unit tests |
| 🔌 | Compatible Works in any ES5+ environment including older versions of Node.js, Bun, Deno, React Native and browsers |
/\*\*/ if / is the separator)
- `\` escapes the following character making it be treated literally
More features are available in the [outmatch](https://github.com/axtgr/outmatch) library.
### Separators
Globs are most often used to search file paths, which are, essentially, strings split into segments by slashes. While other libraries are usually restricted to this use-case, wildcard-match is able to work with _arbitrary_ strings by accepting a custom separator in the second parameter:
```js
const matchDomain = wcmatch('*.example.com', { separator: '.' })
matchDomain('subdomain.example.com') //=> true
// Here, the second parameter is a shorthand for `{ separator: ',' }`
const matchLike = wcmatch('one,**,f?ur', ',')
matchLike('one,two,three,four') //=> true
```
The only limitation is that backslashes `\` cannot be used as separators in patterns because
wildcard-match uses them for character escaping. However, when `separator` is `undefined`
or `true`, `/` in patterns will match both `/` and `\`, so a single pattern with forward
slashes can match both Unix and Windows paths:
```js
const isMatchA = outmatch('foo\\bar') // throws an error
const isMatchB = outmatch('foo/bar') // same as passing `true` as the separator
isMatchB('foo/bar') //=> true
isMatchB('foo\\bar') //=> true
const isMatchC = outmatch('foo/bar', '/')
isMatchC('foo/bar') //=> true
isMatchC('foo\\bar') //=> false
```
The matching features work with a _segment_ rather than a whole pattern:
```js
const isMatch = wcmatch('foo/b*')
isMatch('foo/bar') //=> true
isMatch('foo/b/ar') //=> false
```
Segmentation can be turned off completely by passing `false` as the separator, which makes wildcard-match treat whole patterns as a single segment. Slashes become regular symbols and `*` matches _anything_:
```js
const isMatch = wcmatch('foo?ba*', false)
isMatch('foo/bar/qux') //=> true
```
A single separator in a pattern will match _one or more_ separators in a sample string:
```js
wcmatch('foo/bar/baz')('foo/bar///baz') //=> true
```
When a pattern has an explicit separator at its end, samples also require one or more trailing separators:
```js
const isMatch = wcmatch('foo/bar/')
isMatch('foo/bar') //=> false
isMatch('foo/bar/') //=> true
isMatch('foo/bar///') //=> true
```
However, if there is no trailing separator in a pattern, strings will match even if they have separators at the end:
```js
const isMatch = wcmatch('foo/bar')
isMatch('foo/bar') //=> true
isMatch('foo/bar/') //=> true
isMatch('foo/bar///') //=> true
```
### Multiple Patterns
Wildcard-match can take an array of glob patterns as the first argument instead of a single pattern. In that case a string will be considered a match if it matches _any_ of the given patterns:
```js
const isMatch = wcmatch(['src/*', 'tests/*'])
isMatch('src/utils.js') //=> true
isMatch('tests/utils.js') //=> true
```
### Matching Arrays of Strings
The returned function can work with arrays of strings when used as the predicate of the native array methods:
```js
const isMatch = wcmatch('src/*.js')
const paths = ['readme.md', 'src/index.js', 'src/components/body.js']
paths.map(isMatch) //=> [ false, true, false ]
paths.filter(isMatch) //=> [ 'src/index.js' ]
paths.some(isMatch) //=> true
paths.every(isMatch) //=> false
paths.find(isMatch) //=> 'src/index.js'
paths.findIndex(isMatch) //=> 1
```
## API
### wcmatch(patterns, options?): isMatch