An extremely fast and lightweight glob-matching library for JavaScript with advanced features
Outmatch takes one or more glob patterns, compiles them into a RegExp and returns a function for matching strings with it.
Glob patterns are strings that contain wildcards such as *
, ?
, [abc]
and others. When such a pattern is compared with another string, these wildcards can replace one or more symbols. For example, src/*
would match both src/foo
and src/bar
.
While globs are usually used to search file paths separated by slashes, with outmatch it is possible to match arbitrary strings, whether separated or not.
Quickstart
npm install outmatch
import outmatch from 'outmatch'
const isMatch = outmatch('src/**/*.{js,ts}')
isMatch('src/components/header/index.js')
isMatch('src/README.md')
isMatch.pattern
isMatch.options
isMatch.regexp
More details are available in the Installation, Usage, Syntax and API sections.
Features
🍃 | Lightweight No dependencies. Just 2.4 KB when minified and gzipped — less than ⅓ of picomatch and ⅕ of micromatch |
💪 | Powerful Supports basic and extended globbing, proper multi-pattern compilation and custom path separators — a feature unique to outmatch |
🎯 | Accurate The only library that can handle negated extglobs correctly. Actually expands braces instead of merely converting them to groups |
🏎 | Fast Compiles and matches patterns faster than minimatch, micromatch and picomatch |
⚒ | Reliable Written in TypeScript. Covered by thousands of unit tests |
🌞 | Simple The API is a single function. Options can be specified in easy to understand language rather than Linux slang |
🔌 | Compatible Works in any ES5 environment including older versions of Node.js, Deno, Bun, React Native and browsers |
For comparison with the alternatives, see the corresponding section.
Installation
The package is distributed via the npm package registry. It can be installed using one of the compatible package managers or included directly from a CDN.
npm install outmatch
yarn add outmatch
pnpm install outmatch
CDN
When included from a CDN, outmatch is available as the global function outmatch
.
Usage
Outmatch comes built in ESM, CommonJS and UMD formats and includes TypeScript typings. The examples use ESM imports, which can be replaced with the following line for CommonJS: const outmatch = require('outmatch')
.
The default export is a function of two arguments, first of which can be either a single glob string or an array of such patterns. The second argument is optional and can be either an options object or a separator (which will be the value of the separator
option). Outmatch compiles them into a regular expression and returns a function (usually called isMatch
in the examples) that tests strings against the pattern. The pattern, options and the compiled RegExp object are available as properties on the returned function:
import outmatch from 'outmatch'
const isMatch = outmatch('src/[bc]ar', { '{}': false })
isMatch('src/bar')
isMatch('src/car')
isMatch('src/tar')
isMatch.pattern
isMatch.options
isMatch.regexp
The returned function can be invoked immediately if there is no need to match a pattern more than once:
outmatch('src/**/*.js')('src/components/body/index.js')
Compiling a pattern is much slower than comparing a string to it, so it is recommended to always reuse the returned function when possible.
File Paths and 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, outmatch is able to work with arbitrary strings by accepting a custom separator via the second argument:
const matchDomain = outmatch('*.example.com', { separator: '.' })
matchDomain('subdomain.example.com')
const matchLike = outmatch('wh?t like**like mean', 'like')
matchLike('what like do like you like mean')
The only limitation is that backslashes \
cannot be used as separators in patterns because
outmatch 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:
const isMatchA = outmatch('foo\\bar')
const isMatchB = outmatch('foo/bar')
isMatchB('foo/bar')
isMatchB('foo\\bar')
const isMatchC = outmatch('foo/bar', '/')
isMatchC('foo/bar')
isMatchC('foo\\bar')
A thing to note is that most matching features work with a segment rather than a whole pattern. For example, foo/b*
will match foo/bar
but not foo/b/ar
. The two exceptions to this are brace expansion and pattern negation, both of which work with whole patterns:
outmatch('src/{foo/bar,baz}')('src/foo/bar')
outmatch('src/@(foo/bar|baz)')('src/foo/bar')
Any string that contains a segment starting with a dot (a dotfile) is excluded unless the dot is specified explicitly in the pattern.
This behavior can be disabled by setting excludeDot
to false
, in which case leading dots are treated like any other symbol:
outmatch('project/*')('project/.git')
outmatch('project/.*')('project/.git')
outmatch('project/*', { excludeDot: false })('project/.git')
Segmentation can be turned off completely by passing false
as the separator, which makes outmatch treat whole patterns as a single segment.
Slashes become regular symbols, **
works identically to *
and leading dots get excluded only when they are the very first character of a pattern:
const isMatch = outmatch('foo?ba*', false)
isMatch('foo/bar/.qux')
A single separator in a pattern will match any number of separators in a sample string:
outmatch('foo/bar/baz')('foo/bar///baz')
When a pattern has an explicit separator at its end, samples also require one or more trailing separators:
const isMatch = outmatch('foo/bar/')
isMatch('foo/bar')
isMatch('foo/bar/')
isMatch('foo/bar///')
However, if there is no trailing separator in a pattern, strings will match even if they have separators at the end:
const isMatch = outmatch('foo/bar')
isMatch('foo/bar')
isMatch('foo/bar/')
isMatch('foo/bar///')
Multiple Patterns
Outmatch 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:
const isMatch = outmatch(['src/*', 'tests/*'])
isMatch('src/utils.js')
isMatch('tests/utils.js')
If a negated pattern is given among positive patterns, it will work as an ignore filter for strings that match the positive patterns:
const isMatch = outmatch(['src/*', '!src/foo', '!src/bar'])
isMatch('src/foo')
isMatch('src/bar')
isMatch('src/baz')
Matching Arrays of Strings
The returned function can work with arrays of strings when used as the predicate of the native array methods:
const isMatch = outmatch(['src/**/*.js', '!**/body.js'])
const paths = ['readme.md', 'src/index.js', 'src/components/body.js']
paths.map(isMatch)
paths.filter(isMatch)
paths.some(isMatch)
paths.every(isMatch)
paths.find(isMatch)
paths.findIndex(isMatch)
Syntax
Pattern | Description |
---|
Basic Wildcards |
? | Matches exactly one arbitrary character excluding separators |
* | Matches zero or more arbitrary characters excluding separators |
Globstar |
** | Matches zero or more segments when used as a whole segment in a separated pattern (e.g. /**/ if / is the separator) |
Character Classes (Brackets) |
[abc1_] | Matches a single character from the specified list of characters |
[a-z]
[0-9] | Matches a single character from the specified range of characters |
[!abc]
[!f-k] | Matches a single character not in the specified list or range |
[[:alnum:]]
[[:alpha:]]
[[:lower:]] | POSIX classes are not supported. Other libraries claim to have support, but in reality it's just a gimmick.
Their original purpose was to match symbols from the current locale rather than just English. For example, [[:alpha:]] would match the russian letter ы while [a-zA-Z] would not. However, making this work in JS is non-trivial, and most libraries that list POSIX classes as supported merely have them as aliases to regular character ranges, so [[:alpha:]] works identically to [a-zA-Z] |
Extglobs (Parens) |
@(bar|baz) | Matches one of the given subpatterns repeated exactly one time |
?(foo)
?(bar|baz) | Matches one of the given subpatterns repeated zero or one time |
*(foo)
*(bar|baz) | Matches one of the given subpatterns repeated zero or more times |
+(foo)
+(bar|baz) | Matches one of the given subpatterns repeated one or more times |
!(foo)
!(bar|baz) | Matches anything except the given subpatterns. Cannot be nested inside another negated glob |
Braces |
{bar,baz} | Expands the pattern to an array of patterns, so outmatch('src/{foo,bar}/baz') is equivalent to outmatch(['src/foo/baz', 'src/bar/baz'])
While braces are similar to extglobs, they work differently. Braces are expanded before anything else, so, unlike extglobs, they can handle subpatterns that contain separators.
Braces can be nested: src/{foo,bar/{baz,qux}} expands to src/foo , src/bar/baz and src/bar/qux |
{1..5} | Matches any character in the specified range
|
{01..300}
{1..9..2} | Like most other libraries, outmatch doesn't support zero-padded and stepped ranges. The amount of code it would take to implement them is simply not justified by how rarely they are used |
Negation |
! | Negates a pattern when put at the start of it. If repeated multiple times, each ! will invert the effect, so !!foo/bar is the same as foo/bar and !!!baz/qux is the same as !baz/qux .
A negated pattern matches any string that doesn't match the part after the ! . When put in an array among positive patterns, negated patterns effectively work as ignores |
Escaping |
\ | Escapes the following character making it be treated literally |
API
outmatch(patterns, options?): isMatch
outmatch(patterns, separator?): isMatch
Takes a single pattern string or an array of patterns and compiles them into a regular expression. Returns an isMatch function that takes a sample string as its only argument and returns true if the string matches the pattern(s).
isMatch(sample): boolean
Tests if a sample string matches the patterns that were used to compile the regular expression and create this function.
isMatch.regexp
The compiled regular expression.
isMatch.pattern
The original pattern or array of patterns that was used to compile the regular expression and create the isMatch function.
isMatch.options
The options object that was used to compile the regular expression and create the isMatch function.
Options
Option | Type | Default Value | Description |
---|
separator | string | boolean | true | Separator to be used to split patterns and samples into segments:true — / in patterns match both / and \ in samplesfalse — don't split- any string — custom separator
|
flags | string | undefined | Flags to pass to the RegExp. For example, setting this option to 'i' will make the matching case-insensitive |
excludeDot | boolean | true | Toggles whether to exclude strings that contain segments starting with a dot |
! | boolean | true | Toggles pattern negation |
? | boolean | true | Toggles single-char wildcards |
* | boolean | true | Toggles multi-char wildcards |
** | boolean | true | Toggles globstars |
[] | boolean | true | Toggles character classes |
() | boolean | true | Toggles extglobs |
{} | boolean | true | Toggles brace expansion |
Comparison
Pattern: src/test/**/*.?s
Sample: src/test/foo/bar.js
Compilation
outmatch 650,098 ops/sec
picomatch 136,927 ops/sec
Matching
outmatch 34,524,204 ops/sec
picomatch 10,223,739 ops/sec