What is multimatch?
The multimatch npm package allows for matching file paths against specified glob patterns. It is a wrapper around the 'minimatch' library, providing the ability to use multiple patterns at once for more complex matching scenarios. This can be particularly useful in build processes, file selection for operations like copying, deleting, or applying specific processing, and in any context where a list of files needs to be filtered based on flexible criteria.
What are multimatch's main functionalities?
Basic glob matching
This feature demonstrates basic usage of multimatch to filter an array of file paths based on a simple glob pattern. It's useful for selecting files of a specific type.
const multimatch = require('multimatch');
const paths = ['index.html', 'styles/main.css', 'scripts/app.js'];
const matched = multimatch(paths, '*.html');
console.log(matched); // ['index.html']
Multiple pattern matching
This feature shows how to use multimatch with multiple patterns to filter file paths. It's particularly useful when you need to select files that match any of several criteria.
const multimatch = require('multimatch');
const paths = ['index.html', 'styles/main.css', 'scripts/app.js', 'images/logo.png'];
const matched = multimatch(paths, ['*.css', '*.js']);
console.log(matched); // ['styles/main.css', 'scripts/app.js']
Exclusion patterns
This feature illustrates the use of exclusion patterns to filter out files that match a certain pattern. It's useful for excluding specific files or directories from a broader selection.
const multimatch = require('multimatch');
const paths = ['index.html', 'styles/main.css', 'scripts/app.js', 'test/app.test.js'];
const matched = multimatch(paths, ['*.js', '!test/*.js']);
console.log(matched); // ['scripts/app.js']
Other packages similar to multimatch
glob
The 'glob' package provides similar functionality for matching files using glob patterns. Unlike multimatch, glob interacts directly with the filesystem to filter files based on patterns. This makes it more suited for operations that require reading from or writing to the disk, but less convenient for filtering an existing list of file paths.
minimatch
The 'minimatch' package is the core matching library behind multimatch. It offers the fundamental functionality for pattern matching but does so on a one-pattern-at-a-time basis. Multimatch extends this by allowing multiple patterns to be used simultaneously, making it more convenient for complex matching scenarios.
multimatch
Extends minimatch.match()
with support for multiple patterns
Install
npm install multimatch
Usage
import multimatch from 'multimatch';
multimatch(['unicorn', 'cake', 'rainbows'], ['*', '!cake']);
See the tests for more usage examples and expected matches.
API
multimatch(paths, patterns, options?)
Returns an array of matching paths in the order of input paths.
paths
Type: string | string[]
The paths to match against.
patterns
Type: string | string[]
Globbing patterns to use. For example: ['*', '!cake']
. See supported minimatch
patterns.
options
Type: object
See the minimatch
options.
How multiple patterns work
Positive patterns (e.g. foo
or *
) add to the results, while negative patterns (e.g. !foo
) subtract from the results.
Therefore a lone negation (e.g. ['!foo']
) will never match anything. Use ['*', '!foo']
instead.
Globbing patterns
Just a quick overview.
*
matches any number of characters, but not /
?
matches a single character, but not /
**
matches any number of characters, including /
, as long as it's the only thing in a path part{}
allows for a comma-separated list of "or" expressions!
at the beginning of a pattern will negate the match
Related
- globby - Match against the filesystem instead of a list
- matcher - Simple wildcard matching