Socket
Socket
Sign inDemoInstall

micromatch

Package Overview
Dependencies
20
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    micromatch

Glob matching for javascript/node.js. A faster alternative to minimatch (10-20x faster on avg), with all the features you're used to using in your Grunt and gulp tasks.


Version published
Maintainers
1
Install size
224 kB
Created

Package description

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

Changelog

Source

[1.0.0] - 2016-12-12

Stable release.

Readme

Source

micromatch NPM version

Glob matching for javascript/node.js. A faster and more stable alternative to minimatch (bencharks show micromatch is 10-40x faster on avg).

  • 10-20x faster than minimatch (benchmarks) on average
  • Focus on core Bash 4.3 specification features that are actually used (or can be used) in node.js
  • Supports passing glob patterns as a string or array
  • Extensive unit tests

Features

Supports

All the mainstream glob features you're used to using in your gulp and Grunt tasks:

  • Brace Expansion (foo/bar-{1..5}.md, one/{two,three}/four.md)
  • Globstar matching (**/*, a/b/*.js, etc)
  • Logical OR (foo/bar/(abc|xyz).js)
  • Regex character classes (foo/bar/baz-[1-5].js)

You can combine these features to achieve whatever matching patterns you need.

Does not support

  • Extended glob matching. This might be supported in the future, either in core or as an extension, but it's hard to justify the cost in terms of speed and complexity for features that are rarely used.

Install with npm

npm i micromatch --save

Usage

Works exactly the same as minimatch.

var micromatch = require('micromatch');

micromatch(['a.js', 'b.md', 'c.txt'], '*.{js,txt}');
//=> ['a.js', 'c.txt']

Negation patterns:

micromatch(['a.js', 'b.md', 'c.txt'], '!*.{js,txt}');
//=> ['b.md']

micromatch(['a.md', 'b.js', 'c.txt', 'd.json'], ['*.*', '!*.{js,txt}']);
//=> ['a.md', 'd.json']

Special characters

With the exception of brace expansion ({a,b}, {1..5}, etc), most of the special characters convert directly to regex, so you can expect them to follow the same rules and produce the same results as regex.

Square brackets

Given ['a.js', 'b.js', 'c.js', 'd.js', 'E.js']:

  • [ac].js: matches both a and c, returning ['a.js', 'c.js']
  • [b-d].js: matches from b to d, returning ['b.js', 'c.js', 'd.js']
  • [b-d].js: matches from b to d, returning ['b.js', 'c.js', 'd.js']
  • a/[A-Z].js: matches and uppercase letter, returning ['a/E.md']

Learn about regex character classes.

Parentheses

Given ['a.js', 'b.js', 'c.js', 'd.js', 'E.js']:

  • (a|c).js: would match either a or c, returning ['a.js', 'c.js']
  • (b|d).js: would match either b or d, returning ['b.js', 'd.js']
  • (b|[A-Z]).js: would match either b or an uppercase letter, returning ['b.js', 'E.js']

As with regex, parenthese can be nested, so patterns like ((a|b)|c)/b will work. But it might be easier to achieve your goal using brace expansion.

Brace Expansion

In simple cases, brace expansion appears to work the same way as the logical OR operator. For example, (a|b) will achieve the same result as {a,b}.

Here are some powerful features unique to brace expansion:

  • range expansion: a{1..3}b/*.js expands to: ['a1b/*.js', 'a2b/*.js', 'a3b/*.js']

Learn about brace expansion, or visit braces to ask questions and create an issue related to brace-expansion, or to see the full range of features and options related to brace expansion.

.matchRe

Generate a regular expression for matching file paths based on the given pattern:

micromatch.makeRe('a/?/c.md');
//=> /^a\/.\/c\.md$/

Benchmarks

Run the benchmarks

node benchmark/

image

Run tests

Install dev dependencies

npm i -d && mocha

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Please be sure to run the benchmarks before/after any code changes to judge the impact before you do a PR. thanks!

Author

Jon Schlinkert

License

Copyright (c) 2014 Jon Schlinkert
Released under the MIT license


This file was generated by verb on December 28, 2014.

Keywords

FAQs

Last updated on 28 Dec 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc