Can micromatch be used as a drop-in replacement for minimatch?
For mainstream features, I tried to keep as much parity as possible between micromatch and minimatch. But there are some differences.
- the main minimatch function,
minimatch()
, works likemicromatch.isMatch()
- the main micromatch function,
micromatch()
, works like multimatch, with support for multiple patterns. - micromatch optimizes patterns to generate the leanest possible regex to use for matching without sacrificing accuracy.
Micromatch's optimizations are achieved in a number of different ways.
Brace expansion
It's not uncommon to do this in a gulp or Grunt task:
src('*.{yml,json}');
feature | micromatch | minimatch | notes |
---|---|---|---|
multiple patterns | yes | no | ex: ['*.js', '!foo'] |
# comments in file paths |
no | yes | |
brace expansion | yes | yes | ex: *.{txt,md} |
regex character classes | yes | sort of | ex: [a-c]*.js , match file names starting with a through c |
extglobs | yes | yes | ex: `+(foo |
POSIX bracket expressions | yes | no | (character classes) ex: [[:alpha:][:lower:]] |
regex or string | yes | no | Micromatch will take a regex or a glob pattern to use for matching. |
Support for matching against multiple patterns, like ['*.js', '!foo']
:
- Minimatch: no
- Micromatch: yes
Because of this, there is also a key difference in how the main exported function from each library is used.
Key difference
micromatch()
: the mainmicromatch()
function works like multimatch, and supports matching with multiple patterns (e.g.['*.js', '!foo']
).minimatch()
: the mainminimatch()
function works likemicromatch.isMatch()
, returning true if a single path matches the given pattern.
method | micromatch | minimatch | notes |
---|---|---|---|
matchOne |
no | yes | like match, but only the first file |
makeRe |
yes | yes | create a regular expression from the pattern. |
match |
yes | yes | return an array of matches from a single pattern |
filter |
yes | yes | like match but returns a function that can be passed to Array.filter |
contains |
yes | no | like match, but matches any part of a path, not just the entire path |
expand |
yes | no | returns an object of tokens, which are passed to .makeRe() |
matcher |
yes | no | returns a function to use for matching |
isMatch |
yes | no | returns true if a path matches the given pattern. Works like minimatch() |
matchKeys |
yes | no | match the keys in an object |
option | micromatch | minimatch | description |
---|---|---|---|
flipNegate |
no | yes | |
failglob |
yes | no | throw when no matches are found (bash parity) |
ignore |
yes | no | string or array of patterns to ignore. like negate, but passed on options. |
nocase |
yes | yes | ... |
nonull |
yes | yes | ... |
nullglob |
yes | yes | ... |
nonegate |
yes | yes | ... |
benchmarks
micromatch is faster in every benchmark by a significant margin. One significant case that stands out is matching on arrays with thousands of items (like filepaths in a project). Here are the results from the benchmarks for matching against an array of ~7-8k items:
micromatch.js x 773 ops/sec ±0.62% (98 runs sampled)
minimatch.js x 27.52 ops/sec ±0.66% (49 runs sampled)
Bash 4.3
micromatch has better Bash 4.3 coverage along with extensive, organized unit tests
micromatch isn't a constructor
However, if you're using new Minimatch()
because you need to do some kind of customization to the pre-regex pattern or whatever, then you should be able to achieve the same or similar results with micromatch.
In particular, micromatch.expand()
parses the glob pattern and returns an object. You can then pass that to the .makeRe()
method to generate the regex for matching.
(nothing yet)