What is micromatch?
The micromatch npm package is a fast, minimal glob utility for node.js and JavaScript. It is used to parse, match, and expand glob patterns against strings to filter, validate, or manipulate file paths, names, and other string lists.
What are micromatch's main functionalities?
Matching glob patterns
Match an array of strings to a glob pattern. In this example, it filters the list to include only files ending with '.js'.
const micromatch = require('micromatch');
const result = micromatch(['foo.js', 'bar.js'], '*.js');
console.log(result); // ['foo.js', 'bar.js']
Negating glob patterns
Use negation patterns to exclude matches. This example matches all '.js' files but excludes 'foo.js'.
const micromatch = require('micromatch');
const result = micromatch(['foo.js', 'bar.js', 'baz.txt'], ['*.js', '!foo.js']);
console.log(result); // ['bar.js']
Testing a filepath
Test a single filepath against a glob pattern to see if it matches. In this case, 'foobar.js' is a match for '*.js'.
const micromatch = require('micromatch');
const isMatch = micromatch.isMatch('foobar.js', '*.js');
console.log(isMatch); // true
Expanding braces
Expand braces in glob patterns to match multiple patterns. This example matches 'foo.js' and 'foo1.js' with a single pattern.
const micromatch = require('micromatch');
const result = micromatch(['foo.js', 'foo1.js'], 'foo{,1}.js');
console.log(result); // ['foo.js', 'foo1.js']
Other packages similar to micromatch
minimatch
Minimatch is a minimal matching utility that works with glob patterns. It is the matcher used internally by npm (at least at the time of writing), but it is slower and less feature-rich compared to micromatch.
globby
Globby is a wrapper for fast-glob and supports multiple patterns. It provides a higher-level API for matching against file paths and is built on top of micromatch, offering additional features like file system operations.
fast-glob
Fast-glob is a fast implementation of glob functionality with a focus on performance. It provides a similar API to micromatch but includes additional features like returning file stats along with matched paths.
node-glob
Node-glob is an older glob implementation for Node.js. It is callback-based and not as performant or modern as micromatch, but it has been widely used in the Node.js ecosystem.