Glob matching for javascript/node.js, a replacement and faster alternative to minimatch and multimatch
npx @tessl/cli install tessl/npm-micromatch@4.0.0Micromatch is a high-performance glob matching library for JavaScript/Node.js that serves as a faster, feature-rich alternative to minimatch and multimatch. It provides comprehensive pattern matching capabilities including extended globbing (extglobs), brace expansion, POSIX bracket expressions, and regex character classes.
npm install micromatchconst micromatch = require('micromatch');For specific functions:
const { isMatch, matcher, not, contains } = require('micromatch');const micromatch = require('micromatch');
// Filter files using glob patterns
const files = ['a.js', 'b.txt', 'c.js', 'd.md'];
console.log(micromatch(files, '*.js'));
//=> ['a.js', 'c.js']
// Test if a string matches patterns
console.log(micromatch.isMatch('foo.js', '*.js'));
//=> true
// Use negation patterns
console.log(micromatch(files, ['*', '!*.txt']));
//=> ['a.js', 'c.js', 'd.md']Micromatch is built around several key components:
micromatch() function for filtering arrays of stringsisMatch, contains, some, every, all)parse, scan, makeRe)braces, braceExpand)not, matchKeys, capture)Core functionality for filtering arrays of strings against glob patterns with support for negation, options, and advanced pattern features.
/**
* Returns an array of strings that match one or more glob patterns
* @param {string|string[]} list - List of strings to match
* @param {string|string[]} patterns - One or more glob patterns
* @param {object} options - See available options
* @returns {string[]} Array of matches
*/
function micromatch(list, patterns, options);Functions that return true/false for pattern matching tests, including single string tests and collection-based tests.
/**
* Returns true if any of the given glob patterns match the specified string
* @param {string} str - The string to test
* @param {string|string[]} patterns - One or more glob patterns
* @param {object} options - See available options
* @returns {boolean} Returns true if any patterns match str
*/
function isMatch(str, patterns, options);
/**
* Returns a matcher function from the given glob pattern
* @param {string} pattern - Glob pattern
* @param {object} options - See available options
* @returns {function} Returns a matcher function
*/
function matcher(pattern, options);Advanced pattern manipulation functions for creating regular expressions, parsing patterns, and analyzing pattern structure.
/**
* Create a regular expression from the given glob pattern
* @param {string} pattern - A glob pattern to convert to regex
* @param {object} options - See available options
* @returns {RegExp} Returns a regex created from the given pattern
*/
function makeRe(pattern, options);
/**
* Parse a glob pattern to create the source string for a regular expression
* @param {string|string[]} patterns - Glob patterns to parse
* @param {object} options - See available options
* @returns {object[]} Returns array of parsed pattern objects
*/
function parse(patterns, options);Comprehensive brace pattern processing for expanding and manipulating brace expressions like {a,b,c} and {1..10}.
/**
* Process the given brace pattern
* @param {string} pattern - String with brace pattern to process
* @param {object} options - Any options to change how expansion is performed
* @returns {string[]} Array of processed patterns
*/
function braces(pattern, options);
/**
* Expand braces with expand option enabled
* @param {string} pattern - String with brace pattern to expand
* @param {object} options - Any options to change how expansion is performed
* @returns {string[]} Array of expanded patterns
*/
function braceExpand(pattern, options);Specialized utility functions for specific matching scenarios including negation filtering, object key matching, and capture groups.
/**
* Returns a list of strings that do not match any of the given patterns
* @param {string[]} list - Array of strings to match
* @param {string|string[]} patterns - One or more glob patterns
* @param {object} options - See available options
* @returns {string[]} Returns array of strings that do not match
*/
function not(list, patterns, options);
/**
* Filter the keys of the given object with the given glob pattern
* @param {object} object - The object with keys to filter
* @param {string|string[]} patterns - One or more glob patterns
* @param {object} options - See available options
* @returns {object} Returns object with only keys that match patterns
*/
function matchKeys(object, patterns, options);Micromatch provides extensive configuration options to control matching behavior:
interface MicromatchOptions {
// Matching Behavior
/** Match against basename only */
basename?: boolean;
/** Alias for basename */
matchBase?: boolean;
/** Follow bash matching rules more strictly */
bash?: boolean;
/** Match dotfiles */
dot?: boolean;
/** Case-insensitive matching */
nocase?: boolean;
/** Allow partial string matching */
contains?: boolean;
// Pattern Processing
/** Disable brace matching */
nobrace?: boolean;
/** Disable extglob support (+(a|b)) */
noextglob?: boolean;
/** Alias for noextglob */
noext?: boolean;
/** Disable globstar (**) support */
noglobstar?: boolean;
/** Disable negation (!) support */
nonegate?: boolean;
/** Disable regex brackets */
nobracket?: boolean;
/** Disable regex quantifiers */
noquantifiers?: boolean;
// Path Handling
/** Convert slashes to forward slashes */
posixSlashes?: boolean;
/** Alias for posixSlashes */
unixify?: boolean;
/** Strict slash matching */
strictSlashes?: boolean;
/** Strict bracket/brace/paren matching */
strictBrackets?: boolean;
// Advanced Options
/** One or more glob patterns to ignore */
ignore?: string | string[];
/** Throw error when no matches found */
failglob?: boolean;
/** Return null patterns when no matches (like Bash nullglob) */
nullglob?: boolean;
/** Return original patterns when no matches found */
nonull?: boolean;
/** Return regex matches in supporting methods */
capture?: boolean;
/** Enable fast-path optimization */
fastpaths?: boolean;
/** Support POSIX character classes */
posix?: boolean;
/** Remove escaping backslashes */
unescape?: boolean;
/** Regex flags to use */
flags?: string;
/** Maximum input string length */
maxLength?: number;
// Callback Functions
/** Function called on all items */
onResult?: (state: any) => void;
/** Function called on matched items */
onMatch?: (state: any) => void;
/** Function called on ignored items */
onIgnore?: (state: any) => void;
// Custom Functions
/** Custom function for expanding ranges */
expandRange?: (a: string, b: string) => string;
/** Custom function for formatting output */
format?: (str: string) => string;
}