What is minimatch?
The minimatch npm package is a minimal matching utility that implements the string pattern matching functionality, commonly known as 'globbing'. It is used to match text strings with wildcard characters, such as '*' for multiple characters or '?' for a single character. It is often used in file path matching and filtering operations.
What are minimatch's main functionalities?
Basic string matching
This feature allows for basic pattern matching where a string is tested against a pattern. Wildcards like '*' and '?' can be used to match multiple or single characters respectively.
"use strict";
const minimatch = require("minimatch");
// Match a literal string
console.log(minimatch("foo.js", "foo.js")); // true
// Match with a single wildcard
console.log(minimatch("foo.js", "*.js")); // true
// Match with a single character wildcard
console.log(minimatch("foo.js", "f?o.js")); // true
Negation
This feature allows patterns to be negated so that they match strings that do not match the given pattern.
"use strict";
const minimatch = require("minimatch");
// Negate the match
console.log(minimatch("foo.js", "!foo.js")); // false
console.log(minimatch("bar.js", "!foo.js")); // true
Match options
Minimatch allows for additional options to be set, such as case-insensitivity, to customize the matching behavior.
"use strict";
const minimatch = require("minimatch");
// Match with options
const options = {nocase: true};
console.log(minimatch("FOO.JS", "*.js", options)); // true
Other packages similar to minimatch
micromatch
Micromatch is a faster and more efficient globbing library with a broader feature set compared to minimatch. It offers advanced pattern matching with support for multiple patterns and extended globbing features.
glob
Glob is a package that provides pattern matching and file system operations. It is more focused on file system globbing rather than string pattern matching, but it uses minimatch under the hood for its matching capabilities.
multimatch
Multimatch extends minimatch to allow for multiple patterns to be specified at once. It is useful when you need to match against an array of patterns rather than a single pattern.
anymatch
Anymatch is a package that allows for matching strings against not just patterns, but also against regular expressions and functions. It provides a more flexible matching mechanism compared to minimatch.
minimatch
A minimal matching utility.
This is the matching library used internally by npm.
Eventually, it will replace the C binding in node-glob.
It works by converting glob expressions into JavaScript RegExp
objects.
Usage
var minimatch = require("minimatch")
minimatch("bar.foo", "*.foo")
minimatch("bar.foo", "*.bar")
Features
Supports all glob features.
See:
man sh
man fnmatch
man 5 gitignore
Departures from zsh/bash/ksh/sh
If the pattern starts with a !
character, then it is negated.
If a pattern starts with #
, then it is treated as a comment, and
will not match anything. (Use \#
to match a literal #
at the
start of a line.)
The double-star **
is always supported, instead of requiring a special
flag.
If an escaped pattern has no matches, and the null
flag is not set,
then minimatch.match returns the pattern as-provided, rather than
interpreting the character escapes. For example,
minimatch.match([], "\\*a\\?")
will return "\\*a\\?"
rather than
"*a?"
.
Functions
minimatch(path, pattern, options)
Main export. Tests a path against
the pattern using the options.
minimatch.filter(pattern, options)
Returns a function that tests its
supplied argument, suitable for use with Array.filter
.
minimatch.match(list, pattern, options)
Match against the list of
files, in the style of fnmatch or glob. If nothing is matched, then
return the pattern (unless { null: true }
in the options.)
minimatch.makeRe(pattern, options)
Make a regular expression object
from the pattern.
Options
All options are false
by default.
debug
Dump a ton of stuff to stderr.
null
Return an empty list from minimatch.match, instead of a list
containing the pattern itself.
nocase
Perform a case-insensitive match.
cache
An LRU cache with .get(k)
and .set(k,v)
methods. By
default, an instance of node-lru-cache
is used, with 1000 max
entries.
slash
If set, then a/*
will match a/
as well as a/b
.
matchBase
If set, then patterns without slashes will be matched
against the basename of the path if it contains slashes. For example,
a?b
would match xyz/123/acb
.
partial
Internal. Used by minimatch.makeRe
.
dot
Allow patterns to match paths starting with a period, even if
the pattern does not explicitly start with a period.