How to use the Minimatch function from minimatch

Find comprehensive JavaScript minimatch.Minimatch code examples handpicked from public code repositorys.

minimatch.Minimatch is a class that creates an object used to match file paths against a pattern.

196
197
198
199
200
201
202
203
204
205
  delete options.noglobstar
}
if (params.anchored == false) { options.matchBase = true }
if (params.debug == 'minimatch') { options.debug = true }

const mm = new minimatch.Minimatch(params.pattern, options)
if (!mm.makeRe()) { // Bad pattern
  throw Object.assign(
    new Error(`Invalid match pattern "${params.origPattern}"`),
    { code: 'EINVAL', pattern: params.origPattern }
fork icon3
star icon4
watch icon1

+ 4 other calls in file

213
214
215
216
217
218
219
220
221
222
module.exports = glob

var fs = require('fs')
var rp = require('fs.realpath')
var minimatch = require('minimatch')
var Minimatch = minimatch.Minimatch
var inherits = require('inherits')
var EE = require('events').EventEmitter
var path = require('path')
var assert = require('assert')
fork icon2
star icon4
watch icon4

How does minimatch.Minimatch work?

minimatch.Minimatch is a class that represents a compiled glob pattern that can be used to match and filter file paths. It works by parsing the glob pattern string and converting it into a regular expression, which is then used to match against file paths. The resulting Minimatch object can be used to perform multiple matches against different file paths without having to re-parse the glob pattern.

128
129
130
131
132
133
134
135
136
137

this.matchers = [];
if(this.options.pattern) {
  const matchers = Array.isArray(this.options.pattern) ? this.options.pattern : [this.options.pattern];
  this.matchers = matchers.map( m =>
    new Minimatch(m, {
      dot: this.options.dot,
      noglobstar:this.options.noglobstar,
      matchBase:this.options.matchBase,
      nocase:this.options.nocase
fork icon0
star icon1
watch icon1

+ 2 other calls in file

100
101
102
103
104
105
106
107
108
109
110
111


//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------


const Minimatch = minimatch.Minimatch;
const minimatchCache = new Map();
const negatedMinimatchCache = new Map();
const debug = createDebug('@hwc/config-array');

fork icon0
star icon0
watch icon1

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
const minimatch = require("minimatch");

const pattern = new minimatch.Minimatch("**/*.js"); // create a pattern that matches all .js files in all subdirectories
const filename1 = "src/index.js";
const filename2 = "dist/script.min.js";

console.log(pattern.match(filename1)); // true
console.log(pattern.match(filename2)); // true

In this example, minimatch.Minimatch is used to create a pattern that matches all .js files in all subdirectories. The match() method is then called on the pattern object with two different filenames, and it returns true for both since both filenames match the pattern.

8
9
10
11
12
13
14
15
16
17
18


class WildcardMatcher {
  constructor(wildcardPattern, emptyVal) {
    this.emptyVal = emptyVal;
    this.pattern = String(wildcardPattern || '*');
    this.matcher = new _minimatch.Minimatch(this.pattern, {
      noglobstar: true,
      dot: true,
      nocase: true,
      matchBase: true,
fork icon0
star icon0
watch icon0

+ 7 other calls in file

101
102
103
104
105
106
107
108
109
110
  _classCallCheck(this, GithubGlobMatcher);

  this.repo = repo;
  this.ref = ref;
  this.pathCache = Object.create(null);
  this.patterns = new _minimatch.Minimatch(typeof patterns === 'string' ? patterns : '{' + patterns.join(',') + '}').set;
  this.matches = [];
}

_createClass(GithubGlobMatcher, [{
fork icon0
star icon0
watch icon0