Home

Awesome

<h1 align="center">Picomatch</h1> <p align="center"> <a href="https://npmjs.org/package/picomatch"> <img src="https://img.shields.io/npm/v/picomatch.svg" alt="version"> </a> <a href="https://github.com/micromatch/picomatch/actions?workflow=Tests"> <img src="https://github.com/micromatch/picomatch/workflows/Tests/badge.svg" alt="test status"> </a> <a href="https://coveralls.io/github/micromatch/picomatch"> <img src="https://img.shields.io/coveralls/github/micromatch/picomatch/master.svg" alt="coverage status"> </a> <a href="https://npmjs.org/package/picomatch"> <img src="https://img.shields.io/npm/dm/picomatch.svg" alt="downloads"> </a> </p> <br> <br> <p align="center"> <strong>Blazing fast and accurate glob matcher written in JavaScript.</strong></br> <em>No dependencies and full support for standard and extended Bash glob features, including braces, extglobs, POSIX brackets, and regular expressions.</em> </p> <br> <br>

Why picomatch?

See the library comparison to other libraries.

<br> <br>

Table of Contents

<details><summary> Click to expand </summary>

(TOC generated by verb using markdown-toc)

</details> <br> <br>

Install

Install with npm:

npm install --save picomatch
<br>

Usage

The main export is a function that takes a glob pattern and an options object and returns a function for matching strings.

const pm = require('picomatch');
const isMatch = pm('*.js');

console.log(isMatch('abcd')); //=> false
console.log(isMatch('a.js')); //=> true
console.log(isMatch('a.md')); //=> false
console.log(isMatch('a/b.js')); //=> false
<br>

API

picomatch

Creates a matcher function from one or more glob patterns. The returned function takes a string to match as its first argument, and returns true if the string is a match. The returned matcher function also takes a boolean as the second argument that, when true, returns an object with additional information.

Params

Example

const picomatch = require('picomatch');
// picomatch(glob[, options]);

const isMatch = picomatch('*.!(*a)');
console.log(isMatch('a.a')); //=> false
console.log(isMatch('a.b')); //=> true

Example without node.js

For environments without node.js, picomatch/posix provides you a dependency-free matcher, without automatic OS detection.

const picomatch = require('picomatch/posix');
// the same API, defaulting to posix paths
const isMatch = picomatch('a/*');
console.log(isMatch('a\\b')); //=> false
console.log(isMatch('a/b')); //=> true

// you can still configure the matcher function to accept windows paths
const isMatch = picomatch('a/*', { options: windows });
console.log(isMatch('a\\b')); //=> true
console.log(isMatch('a/b')); //=> true

.test

Test input with the given regex. This is used by the main picomatch() function to test the input string.

Params

Example

const picomatch = require('picomatch');
// picomatch.test(input, regex[, options]);

console.log(picomatch.test('foo/bar', /^(?:([^/]*?)\/([^/]*?))$/));
// { isMatch: true, match: [ 'foo/', 'foo', 'bar' ], output: 'foo/bar' }

.matchBase

Match the basename of a filepath.

Params

Example

const picomatch = require('picomatch');
// picomatch.matchBase(input, glob[, options]);
console.log(picomatch.matchBase('foo/bar.js', '*.js'); // true

.isMatch

Returns true if any of the given glob patterns match the specified string.

Params

Example

const picomatch = require('picomatch');
// picomatch.isMatch(string, patterns[, options]);

console.log(picomatch.isMatch('a.a', ['b.*', '*.a'])); //=> true
console.log(picomatch.isMatch('a.a', 'b.*')); //=> false

.parse

Parse a glob pattern to create the source string for a regular expression.

Params

Example

const picomatch = require('picomatch');
const result = picomatch.parse(pattern[, options]);

.scan

Scan a glob pattern to separate the pattern into segments.

Params

Example

const picomatch = require('picomatch');
// picomatch.scan(input[, options]);

const result = picomatch.scan('!./foo/*.js');
console.log(result);
{ prefix: '!./',
  input: '!./foo/*.js',
  start: 3,
  base: 'foo',
  glob: '*.js',
  isBrace: false,
  isBracket: false,
  isGlob: true,
  isExtglob: false,
  isGlobstar: false,
  negated: true }

.compileRe

Compile a regular expression from the state object returned by the parse() method.

Params

.makeRe

Create a regular expression from a parsed glob pattern.

Params

Example

const picomatch = require('picomatch');
const state = picomatch.parse('*.js');
// picomatch.compileRe(state[, options]);

console.log(picomatch.compileRe(state));
//=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/

.toRegex

Create a regular expression from the given regex source string.

Params

Example

const picomatch = require('picomatch');
// picomatch.toRegex(source[, options]);

const { output } = picomatch.parse('*.js');
console.log(picomatch.toRegex(output));
//=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/
<br>

Options

Picomatch options

The following options may be used with the main picomatch() function or any of the methods on the picomatch API.

OptionTypeDefault valueDescription
basenamebooleanfalseIf set, then patterns without slashes will be matched against the basename of the path if it contains slashes. For example, a?b would match the path /xyz/123/acb, but not /xyz/acb/123.
bashbooleanfalseFollow bash matching rules more strictly - disallows backslashes as escape characters, and treats single stars as globstars (**).
capturebooleanundefinedReturn regex matches in supporting methods.
containsbooleanundefinedAllows glob to match any part of the given string(s).
cwdstringprocess.cwd()Current working directory. Used by picomatch.split()
debugbooleanundefinedDebug regular expressions when an error is thrown.
dotbooleanfalseEnable dotfile matching. By default, dotfiles are ignored unless a . is explicitly defined in the pattern, or options.dot is true
expandRangefunctionundefinedCustom function for expanding ranges in brace patterns, such as {a..z}. The function receives the range values as two arguments, and it must return a string to be used in the generated regex. It's recommended that returned strings be wrapped in parentheses.
failglobbooleanfalseThrows an error if no matches are found. Based on the bash option of the same name.
fastpathsbooleantrueTo speed up processing, full parsing is skipped for a handful common glob patterns. Disable this behavior by setting this option to false.
flagsstringundefinedRegex flags to use in the generated regex. If defined, the nocase option will be overridden.
formatfunctionundefinedCustom function for formatting the returned string. This is useful for removing leading slashes, converting Windows paths to Posix paths, etc.
ignorearray|stringundefinedOne or more glob patterns for excluding strings that should not be matched from the result.
keepQuotesbooleanfalseRetain quotes in the generated regex, since quotes may also be used as an alternative to backslashes.
literalBracketsbooleanundefinedWhen true, brackets in the glob pattern will be escaped so that only literal brackets will be matched.
matchBasebooleanfalseAlias for basename
maxLengthboolean65536Limit the max length of the input string. An error is thrown if the input string is longer than this value.
nobracebooleanfalseDisable brace matching, so that {a,b} and {1..3} would be treated as literal characters.
nobracketbooleanundefinedDisable matching with regex brackets.
nocasebooleanfalseMake matching case-insensitive. Equivalent to the regex i flag. Note that this option is overridden by the flags option.
nodupesbooleantrueDeprecated, use nounique instead. This option will be removed in a future major release. By default duplicates are removed. Disable uniquification by setting this option to false.
noextbooleanfalseAlias for noextglob
noextglobbooleanfalseDisable support for matching with extglobs (like +(a|b))
noglobstarbooleanfalseDisable support for matching nested directories with globstars (**)
nonegatebooleanfalseDisable support for negating with leading !
noquantifiersbooleanfalseDisable support for regex quantifiers (like a{1,2}) and treat them as brace patterns to be expanded.
onIgnorefunctionundefinedFunction to be called on ignored items.
onMatchfunctionundefinedFunction to be called on matched items.
onResultfunctionundefinedFunction to be called on all items, regardless of whether or not they are matched or ignored.
posixbooleanfalseSupport POSIX character classes ("posix brackets").
posixSlashesbooleanundefinedConvert all slashes in file paths to forward slashes. This does not convert slashes in the glob pattern itself
prependbooleanundefinedString to prepend to the generated regex used for matching.
regexbooleanfalseUse regular expression rules for + (instead of matching literal +), and for stars that follow closing parentheses or brackets (as in )* and ]*).
strictBracketsbooleanundefinedThrow an error if brackets, braces, or parens are imbalanced.
strictSlashesbooleanundefinedWhen true, picomatch won't match trailing slashes with single stars.
unescapebooleanundefinedRemove backslashes preceding escaped characters in the glob pattern. By default, backslashes are retained.
unixifybooleanundefinedAlias for posixSlashes, for backwards compatibility.
windowsbooleanfalseAlso accept backslashes as the path separator.

Scan Options

In addition to the main picomatch options, the following options may also be used with the .scan method.

OptionTypeDefault valueDescription
tokensbooleanfalseWhen true, the returned object will include an array of tokens (objects), representing each path "segment" in the scanned glob pattern
partsbooleanfalseWhen true, the returned object will include an array of strings representing each path "segment" in the scanned glob pattern. This is automatically enabled when options.tokens is true

Example

const picomatch = require('picomatch');
const result = picomatch.scan('!./foo/*.js', { tokens: true });
console.log(result);
// {
//   prefix: '!./',
//   input: '!./foo/*.js',
//   start: 3,
//   base: 'foo',
//   glob: '*.js',
//   isBrace: false,
//   isBracket: false,
//   isGlob: true,
//   isExtglob: false,
//   isGlobstar: false,
//   negated: true,
//   maxDepth: 2,
//   tokens: [
//     { value: '!./', depth: 0, isGlob: false, negated: true, isPrefix: true },
//     { value: 'foo', depth: 1, isGlob: false },
//     { value: '*.js', depth: 1, isGlob: true }
//   ],
//   slashes: [ 2, 6 ],
//   parts: [ 'foo', '*.js' ]
// }
<br>

Options Examples

options.expandRange

Type: function

Default: undefined

Custom function for expanding ranges in brace patterns. The fill-range library is ideal for this purpose, or you can use custom code to do whatever you need.

Example

The following example shows how to create a glob that matches a folder

const fill = require('fill-range');
const regex = pm.makeRe('foo/{01..25}/bar', {
  expandRange(a, b) {
    return `(${fill(a, b, { toRegex: true })})`;
  }
});

console.log(regex);
//=> /^(?:foo\/((?:0[1-9]|1[0-9]|2[0-5]))\/bar)$/

console.log(regex.test('foo/00/bar'))  // false
console.log(regex.test('foo/01/bar'))  // true
console.log(regex.test('foo/10/bar')) // true
console.log(regex.test('foo/22/bar')) // true
console.log(regex.test('foo/25/bar')) // true
console.log(regex.test('foo/26/bar')) // false

options.format

Type: function

Default: undefined

Custom function for formatting strings before they're matched.

Example

// strip leading './' from strings
const format = str => str.replace(/^\.\//, '');
const isMatch = picomatch('foo/*.js', { format });
console.log(isMatch('./foo/bar.js')); //=> true

options.onMatch

const onMatch = ({ glob, regex, input, output }) => {
  console.log({ glob, regex, input, output });
};

const isMatch = picomatch('*', { onMatch });
isMatch('foo');
isMatch('bar');
isMatch('baz');

options.onIgnore

const onIgnore = ({ glob, regex, input, output }) => {
  console.log({ glob, regex, input, output });
};

const isMatch = picomatch('*', { onIgnore, ignore: 'f*' });
isMatch('foo');
isMatch('bar');
isMatch('baz');

options.onResult

const onResult = ({ glob, regex, input, output }) => {
  console.log({ glob, regex, input, output });
};

const isMatch = picomatch('*', { onResult, ignore: 'f*' });
isMatch('foo');
isMatch('bar');
isMatch('baz');
<br> <br>

Globbing features

Basic globbing

CharacterDescription
*Matches any character zero or more times, excluding path separators. Does not match path separators or hidden files or directories ("dotfiles"), unless explicitly enabled by setting the dot option to true.
**Matches any character zero or more times, including path separators. Note that ** will only match path separators (/, and \\ with the windows option) when they are the only characters in a path segment. Thus, foo**/bar is equivalent to foo*/bar, and foo/a**b/bar is equivalent to foo/a*b/bar, and more than two consecutive stars in a glob path segment are regarded as a single star. Thus, foo/***/bar is equivalent to foo/*/bar.
?Matches any character excluding path separators one time. Does not match path separators or leading dots.
[abc]Matches any characters inside the brackets. For example, [abc] would match the characters a, b or c, and nothing else.

Matching behavior vs. Bash

Picomatch's matching features and expected results in unit tests are based on Bash's unit tests and the Bash 4.3 specification, with the following exceptions:

<br>

Advanced globbing

Extglobs

PatternDescription
@(pattern)Match only one consecutive occurrence of pattern
*(pattern)Match zero or more consecutive occurrences of pattern
+(pattern)Match one or more consecutive occurrences of pattern
?(pattern)Match zero or one consecutive occurrences of pattern
!(pattern)Match anything but pattern

Examples

const pm = require('picomatch');

// *(pattern) matches ZERO or more of "pattern"
console.log(pm.isMatch('a', 'a*(z)')); // true
console.log(pm.isMatch('az', 'a*(z)')); // true
console.log(pm.isMatch('azzz', 'a*(z)')); // true

// +(pattern) matches ONE or more of "pattern"
console.log(pm.isMatch('a', 'a+(z)')); // false
console.log(pm.isMatch('az', 'a+(z)')); // true
console.log(pm.isMatch('azzz', 'a+(z)')); // true

// supports multiple extglobs
console.log(pm.isMatch('foo.bar', '!(foo).!(bar)')); // false

// supports nested extglobs
console.log(pm.isMatch('foo.bar', '!(!(foo)).!(!(bar))')); // true

POSIX brackets

POSIX classes are disabled by default. Enable this feature by setting the posix option to true.

Enable POSIX bracket support

console.log(pm.makeRe('[[:word:]]+', { posix: true }));
//=> /^(?:(?=.)[A-Za-z0-9_]+\/?)$/

Supported POSIX classes

The following named POSIX bracket expressions are supported:

See the Bash Reference Manual for more information.

Braces

Picomatch does not do brace expansion. For brace expansion and advanced matching with braces, use micromatch instead. Picomatch has very basic support for braces.

Matching special characters as literals

If you wish to match the following special characters in a filepath, and you want to use these characters in your glob pattern, they must be escaped with backslashes or quotes:

Special Characters

Some characters that are used for matching in regular expressions are also regarded as valid file path characters on some platforms.

To match any of the following characters as literals: `$^*+?()[]

Examples:

console.log(pm.makeRe('foo/bar \\(1\\)'));
console.log(pm.makeRe('foo/bar \\(1\\)'));
<br> <br>

Library Comparisons

The following table shows which features are supported by minimatch, micromatch, picomatch, nanomatch, extglob, braces, and expand-brackets.

Featureminimatchmicromatchpicomatchnanomatchextglobbracesexpand-brackets
Wildcard matching (*?+)---
Advancing globbing----
Brace matching---
Brace expansion----
Extglobspartial---
Posix brackets----
Regular expression syntax--
File system operations-------
<br> <br>

Benchmarks

Performance comparison of picomatch and minimatch.

(Pay special attention to the last three benchmarks. Minimatch freezes on long ranges.)

# .makeRe star (*)
  picomatch x 4,449,159 ops/sec ±0.24% (97 runs sampled)
  minimatch x 632,772 ops/sec ±0.14% (98 runs sampled)

# .makeRe star; dot=true (*)
  picomatch x 3,500,079 ops/sec ±0.26% (99 runs sampled)
  minimatch x 564,916 ops/sec ±0.23% (96 runs sampled)

# .makeRe globstar (**)
  picomatch x 3,261,000 ops/sec ±0.27% (98 runs sampled)
  minimatch x 1,664,766 ops/sec ±0.20% (100 runs sampled)

# .makeRe globstars (**/**/**)
  picomatch x 3,284,469 ops/sec ±0.18% (97 runs sampled)
  minimatch x 1,435,880 ops/sec ±0.34% (95 runs sampled)

# .makeRe with leading star (*.txt)
  picomatch x 3,100,197 ops/sec ±0.35% (99 runs sampled)
  minimatch x 428,347 ops/sec ±0.42% (94 runs sampled)

# .makeRe - basic braces ({a,b,c}*.txt)
  picomatch x 443,578 ops/sec ±1.33% (89 runs sampled)
  minimatch x 107,143 ops/sec ±0.35% (94 runs sampled)

# .makeRe - short ranges ({a..z}*.txt)
  picomatch x 415,484 ops/sec ±0.76% (96 runs sampled)
  minimatch x 14,299 ops/sec ±0.26% (96 runs sampled)

# .makeRe - medium ranges ({1..100000}*.txt)
  picomatch x 395,020 ops/sec ±0.87% (89 runs sampled)
  minimatch x 2 ops/sec ±4.59% (10 runs sampled)

# .makeRe - long ranges ({1..10000000}*.txt)
  picomatch x 400,036 ops/sec ±0.83% (90 runs sampled)
  minimatch (FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory)
<br> <br>

Philosophies

The goal of this library is to be blazing fast, without compromising on accuracy.

Accuracy

The number one of goal of this library is accuracy. However, it's not unusual for different glob implementations to have different rules for matching behavior, even with simple wildcard matching. It gets increasingly more complicated when combinations of different features are combined, like when extglobs are combined with globstars, braces, slashes, and so on: !(**/{a,b,*/c}).

Thus, given that there is no canonical glob specification to use as a single source of truth when differences of opinion arise regarding behavior, sometimes we have to implement our best judgement and rely on feedback from users to make improvements.

Performance

Although this library performs well in benchmarks, and in most cases it's faster than other popular libraries we benchmarked against, we will always choose accuracy over performance. It's not helpful to anyone if our library is faster at returning the wrong answer.

<br> <br>

About

<details> <summary><strong>Contributing</strong></summary>

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Please read the contributing guide for advice on opening issues, pull requests, and coding standards.

</details> <details> <summary><strong>Running Tests</strong></summary>

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

npm install && npm test
</details> <details> <summary><strong>Building docs</strong></summary>

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

npm install -g verbose/verb#dev verb-generate-readme && verb
</details>

Author

Jon Schlinkert

License

Copyright © 2017-present, Jon Schlinkert. Released under the MIT License.