How to use minimatch - 10 common examples

To help you get started, we’ve selected a few minimatch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github micromatch / micromatch / test / minimatch / basic.js View on Github external
it('minimatch tests:', function() {
  // http://www.bashcookbook.com/bashinfo/source/bash-1.14.7/tests/glob-test
  mm.makeRe('a*').should.eql(/^(?:(?=.)a[^/]*?)$/);
  mm.makeRe('X*', {nonull: true}).should.eql(/^(?:(?=.)X[^/]*?)$/);
  mm.makeRe('X*').should.eql(/^(?:(?=.)X[^/]*?)$/);
  mm.makeRe('\\*', {nonull: true}).should.eql(/^(?:\*)$/);
  mm.makeRe('\\**', {nonull: true}).should.eql(/^(?:(?=.)\*[^/]*?)$/);
  mm.makeRe('\\*\\*', {nonull: true}).should.eql(/^(?:\*\*)$/);
  mm.makeRe('b*/').should.eql(/^(?:(?=.)b[^/]*?\/)$/);
  mm.makeRe('c*').should.eql(/^(?:(?=.)c[^/]*?)$/);
  mm.makeRe('**').should.eql(/^(?:(?:(?!(?:\/|^)\.).)*?)$/);
  mm.makeRe('\\.\\./*/').should.eql(/^(?:\.\.\/(?!\.)(?=.)[^/]*?\/)$/);
  mm.makeRe('s/\\..*//').should.eql(/^(?:s\/(?=.)\.\.[^/]*?\/)$/);

  // legendary larry crashes bashes
  mm.makeRe('/^root:/{s/^[^:]*:[^:]*:([^:]*).*$/\\1/').should.eql(/^(?:\/\^root:\/\{s\/(?=.)\^[^:][^/]*?:[^:][^/]*?:\([^:]\)[^/]*?\.[^/]*?\$\/1\/)$/);
  mm.makeRe('/^root:/{s/^[^:]*:[^:]*:([^:]*).*$/\\1/').should.eql(/^(?:\/\^root:\/\{s\/(?=.)\^[^:][^/]*?:[^:][^/]*?:\([^:]\)[^/]*?\.[^/]*?\$\/1\/)$/);

  // character classes
  mm.makeRe('[a-c]b*').should.eql(/^(?:(?!\.)(?=.)[a-c]b[^/]*?)$/);
  mm.makeRe('[a-y]*[^c]').should.eql(/^(?:(?!\.)(?=.)[a-y][^/]*?[^c])$/);
  mm.makeRe('a*[^c]').should.eql(/^(?:(?=.)a[^/]*?[^c])$/);
  mm.makeRe('a[X-]b').should.eql(/^(?:(?=.)a[X-]b)$/);
  mm.makeRe('[^a-c]*').should.eql(/^(?:(?!\.)(?=.)[^a-c][^/]*?)$/);
  mm.makeRe('a\\*b/*').should.eql(/^(?:a\*b\/(?!\.)(?=.)[^/]*?)$/);
  mm.makeRe('a\\*?/*').should.eql(/^(?:(?=.)a\*[^/]\/(?!\.)(?=.)[^/]*?)$/);
github micromatch / micromatch / test / minimatch / temp.js View on Github external
mm.makeRe('a*[^c]').should.eql(/^(?:a(?!\.)(?=.)[^/]*?(?!\.)(?=.)[^c])$/);
  mm.makeRe('a[X-]b').should.eql(/^(?:a(?!\.)(?=.)[X-]b)$/);
  mm.makeRe('[^a-c]*').should.eql(/^(?:(?!\.)(?=.)[^a-c][^/]*?)$/);
  mm.makeRe('a\\*b/*').should.eql(/^(?:a\*b\/(?!\.)(?=.)[^/]*?)$/);
  mm.makeRe('a\\*?/*').should.eql(/^(?:a\%\/(?!\.)(?=.)[^/]\/(?!\.)(?=.)[^/]*?)$/);
  mm.makeRe('*\\\\!*').should.eql(/^(?:(?!\.)(?=.)[^/]*?\\![^/]*?)$/);
  mm.makeRe('*\\!*').should.eql(/^(?:(?!\.)(?=.)[^/]*?\![^/]*?)$/);
  mm.makeRe('*.\\*').should.eql(/^(?:(?!\.)(?=.)[^/]*?.\*)$/);
  mm.makeRe('a[b]c').should.eql(/^(?:a(?!\.)(?=.)[b]c)$/);
  mm.makeRe('a[\\b]c').should.eql(/^(?:a(?!\.)(?=.)[\b]c)$/);
  mm.makeRe('a?c').should.eql(/^(?:\/(?!\.)(?=.)[^/]c)$/);
  mm.makeRe('a\\*c').should.eql(/^(?:a\*c)$/);
  mm.makeRe('').should.eql(/^(?:)$/);

  // http://www.opensource.apple.com/source/bash/bash-23/bash/tests/glob-test
  mm.makeRe('*/man*/bash.*').should.eql(/^(?:(?!\.)(?=.)[^/]*?\/man(?!\.)(?=.)[^/]*?\/bash.[^/]*?)$/);
  mm.makeRe('man/man1/bash.1').should.eql(/^(?:man\/man1\/bash\.1)$/);
  mm.makeRe('a***c').should.eql(/^(?:a.*(?!\.)(?=.)[^/]*?c)$/);
  mm.makeRe('a*****?c').should.eql(/^(?:a.*.*\/(?!\.)(?=.)[^/]c)$/);
  // console.log(mm.makeRe('?*****??'));
  mm.makeRe('*****??').should.eql(/^(?:.*.*\/(?!\.)(?=.)[^/][^/])$/);
// console.log(mm.makeRe('?*****?c'));
// console.log(mm.makeRe('?***?****c'));
// console.log(mm.makeRe('?***?****?'));
// console.log(mm.makeRe('?***?****'));
// // console.log(mm.makeRe('*******c'));
// // console.log(mm.makeRe('*******?'));
// // console.log(mm.makeRe('a*cd**?**??k'));
// // console.log(mm.makeRe('a**?**cd**?**??k'));
// // console.log(mm.makeRe('a**?**cd**?**??k***'));
// // console.log(mm.makeRe('a**?**cd**?**??***k'));
// // console.log(mm.makeRe('a**?**cd**?**??***k**'));
github micromatch / micromatch / test / minimatch / basic.js View on Github external
mm.makeRe('a\\*?/*').should.eql(/^(?:(?=.)a\*[^/]\/(?!\.)(?=.)[^/]*?)$/);
  mm.makeRe('*\\\\!*').should.eql(/^(?:(?!\.)(?=.)[^/]*?\\\![^/]*?)$/);
  mm.makeRe('*\\!*').should.eql(/^(?:(?!\.)(?=.)[^/]*?\![^/]*?)$/);
  mm.makeRe('*.\\*').should.eql(/^(?:(?!\.)(?=.)[^/]*?\.\*)$/);
  mm.makeRe('a[b]c').should.eql(/^(?:(?=.)a[b]c)$/);
  mm.makeRe('a[\\b]c').should.eql(/^(?:(?=.)a[b]c)$/);
  mm.makeRe('a?c').should.eql(/^(?:(?=.)a[^/]c)$/);
  mm.makeRe('a\\*c').should.eql(/^(?:a\*c)$/);
  mm.makeRe('').should.be.false();

  // http://www.opensource.apple.com/source/bash/bash-23/bash/tests/glob-test
  mm.makeRe('*/man*/bash.*').should.eql(/^(?:(?!\.)(?=.)[^/]*?\/(?=.)man[^/]*?\/(?=.)bash\.[^/]*?)$/);
  mm.makeRe('man/man1/bash.1').should.eql(/^(?:man\/man1\/bash\.1)$/);
  mm.makeRe('a***c').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/]*?c)$/);
  mm.makeRe('a*****?c').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]c)$/);
  mm.makeRe('?*****??').should.eql(/^(?:(?!\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/])$/);
  mm.makeRe('*****??').should.eql(/^(?:(?!\.)(?=.)[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/])$/);
  mm.makeRe('?*****?c').should.eql(/^(?:(?!\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]c)$/);
  mm.makeRe('?***?****c').should.eql(/^(?:(?!\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?c)$/);
  mm.makeRe('?***?****?').should.eql(/^(?:(?!\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/])$/);
  mm.makeRe('?***?****').should.eql(/^(?:(?!\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?)$/);
  mm.makeRe('*******c').should.eql(/^(?:(?!\.)(?=.)[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?c)$/);
  mm.makeRe('*******?').should.eql(/^(?:(?!\.)(?=.)[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/])$/);
  mm.makeRe('a*cd**?**??k').should.eql(/^(?:(?=.)a[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/]k)$/);
  mm.makeRe('a**?**cd**?**??k').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/][^/]*?[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/]k)$/);
  mm.makeRe('a**?**cd**?**??k***').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/][^/]*?[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/]k[^/]*?[^/]*?[^/]*?)$/);
  mm.makeRe('a**?**cd**?**??***k').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/][^/]*?[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/][^/]*?[^/]*?[^/]*?k)$/);
  mm.makeRe('a**?**cd**?**??***k**').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/][^/]*?[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/][^/]*?[^/]*?[^/]*?k[^/]*?[^/]*?)$/);
  mm.makeRe('a****c**?**??*****').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/]*?[^/]*?c[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?)$/);
  mm.makeRe('[-abc]').should.eql(/^(?:(?!\.)(?=.)[-abc])$/);
  mm.makeRe('[abc-]').should.eql(/^(?:(?!\.)(?=.)[abc-])$/);
  mm.makeRe('\\').should.eql(/^(?:\\)$/);
github micromatch / micromatch / test / minimatch / basic.js View on Github external
mm.makeRe('a*[^c]').should.eql(/^(?:(?=.)a[^/]*?[^c])$/);
  mm.makeRe('a[X-]b').should.eql(/^(?:(?=.)a[X-]b)$/);
  mm.makeRe('[^a-c]*').should.eql(/^(?:(?!\.)(?=.)[^a-c][^/]*?)$/);
  mm.makeRe('a\\*b/*').should.eql(/^(?:a\*b\/(?!\.)(?=.)[^/]*?)$/);
  mm.makeRe('a\\*?/*').should.eql(/^(?:(?=.)a\*[^/]\/(?!\.)(?=.)[^/]*?)$/);
  mm.makeRe('*\\\\!*').should.eql(/^(?:(?!\.)(?=.)[^/]*?\\\![^/]*?)$/);
  mm.makeRe('*\\!*').should.eql(/^(?:(?!\.)(?=.)[^/]*?\![^/]*?)$/);
  mm.makeRe('*.\\*').should.eql(/^(?:(?!\.)(?=.)[^/]*?\.\*)$/);
  mm.makeRe('a[b]c').should.eql(/^(?:(?=.)a[b]c)$/);
  mm.makeRe('a[\\b]c').should.eql(/^(?:(?=.)a[b]c)$/);
  mm.makeRe('a?c').should.eql(/^(?:(?=.)a[^/]c)$/);
  mm.makeRe('a\\*c').should.eql(/^(?:a\*c)$/);
  mm.makeRe('').should.be.false();

  // http://www.opensource.apple.com/source/bash/bash-23/bash/tests/glob-test
  mm.makeRe('*/man*/bash.*').should.eql(/^(?:(?!\.)(?=.)[^/]*?\/(?=.)man[^/]*?\/(?=.)bash\.[^/]*?)$/);
  mm.makeRe('man/man1/bash.1').should.eql(/^(?:man\/man1\/bash\.1)$/);
  mm.makeRe('a***c').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/]*?c)$/);
  mm.makeRe('a*****?c').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]c)$/);
  mm.makeRe('?*****??').should.eql(/^(?:(?!\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/])$/);
  mm.makeRe('*****??').should.eql(/^(?:(?!\.)(?=.)[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/])$/);
  mm.makeRe('?*****?c').should.eql(/^(?:(?!\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]c)$/);
  mm.makeRe('?***?****c').should.eql(/^(?:(?!\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?c)$/);
  mm.makeRe('?***?****?').should.eql(/^(?:(?!\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/])$/);
  mm.makeRe('?***?****').should.eql(/^(?:(?!\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?)$/);
  mm.makeRe('*******c').should.eql(/^(?:(?!\.)(?=.)[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?c)$/);
  mm.makeRe('*******?').should.eql(/^(?:(?!\.)(?=.)[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/])$/);
  mm.makeRe('a*cd**?**??k').should.eql(/^(?:(?=.)a[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/]k)$/);
  mm.makeRe('a**?**cd**?**??k').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/][^/]*?[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/]k)$/);
  mm.makeRe('a**?**cd**?**??k***').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/][^/]*?[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/]k[^/]*?[^/]*?[^/]*?)$/);
  mm.makeRe('a**?**cd**?**??***k').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/][^/]*?[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/][^/]*?[^/]*?[^/]*?k)$/);
  mm.makeRe('a**?**cd**?**??***k**').should.eql(/^(?:(?=.)a[^/]*?[^/]*?[^/][^/]*?[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/][^/]*?[^/]*?[^/]*?k[^/]*?[^/]*?)$/);
github micromatch / micromatch / test / minimatch / basic.js View on Github external
// nocase tests
  mm.makeRe('XYZ', { nocase: true, null: true }).should.eql(/^(?:(?=.)XYZ)$/i);
  mm.makeRe('ab*').should.eql(/^(?:(?=.)ab[^/]*?)$/);
  mm.makeRe('ab*', { nocase: true, null: true }).should.eql(/^(?:(?=.)ab[^/]*?)$/i);
  mm.makeRe('[ia]?[ck]').should.eql(/^(?:(?!\.)(?=.)[ia][^/][ck])$/);

  // onestar/twostar
  mm.makeRe('{/*,*}').should.eql(/^(?:\/(?!\.)(?=.)[^/]*?|(?!\.)(?=.)[^/]*?)$/);
  mm.makeRe('{/?,*}').should.eql(/^(?:\/(?!\.)(?=.)[^/]|(?!\.)(?=.)[^/]*?)$/);

  // dots should not match unless requested
  mm.makeRe('**').should.eql(/^(?:(?:(?!(?:\/|^)\.).)*?)$/);
  mm.makeRe('a/*/b').should.eql(/^(?:a\/(?!\.)(?=.)[^/]*?\/b)$/);
  mm.makeRe('a/*/b', {dot: true}).should.eql(/^(?:a\/(?!(?:^|\/)\.{1,2}(?:$|\/))(?=.)[^/]*?\/b)$/);
  mm.makeRe('a/.*/b').should.eql(/^(?:a\/(?=.)\.[^/]*?\/b)$/);
  mm.makeRe('a/.*/b', {dot: true}).should.eql(/^(?:a\/(?=.)\.[^/]*?\/b)$/);
  mm.makeRe('a/*/b').should.eql(/^(?:a\/(?!\.)(?=.)[^/]*?\/b)$/);
  mm.makeRe('a/*/b', {dot: true}).should.eql(/^(?:a\/(?!(?:^|\/)\.{1,2}(?:$|\/))(?=.)[^/]*?\/b)$/);
  mm.makeRe('a/.*/b').should.eql(/^(?:a\/(?=.)\.[^/]*?\/b)$/);
  mm.makeRe('a/.*/b', {dot: true}).should.eql(/^(?:a\/(?=.)\.[^/]*?\/b)$/);
  mm.makeRe('**').should.eql(/^(?:(?:(?!(?:\/|^)\.).)*?)$/);
  mm.makeRe('**', {dot: true}).should.eql(/^(?:(?:(?!(?:\/|^)(?:\.{1,2})($|\/)).)*?)$/);

  // paren sets cannot contain slashes
  mm.makeRe('*(a/b)').should.eql(/^(?:(?!\.)(?=.)[^/]*?\(a\/b\))$/);
  mm.makeRe('*(a|{b),c)}').should.eql(/^(?:(?!\.)(?=.)(?:a|b)*|(?!\.)(?=.)(?:a|c)*)$/);
  mm.makeRe('[!a*').should.eql(/^(?:(?=.)\[(?=.)\!a[^/]*?)$/);
  mm.makeRe('[#a*').should.eql(/^(?:(?=.)\[(?=.)#a[^/]*?)$/);
  mm.makeRe('+(a|*\\|c\\\\|d\\\\\\|e\\\\\\\\|f\\\\\\\\\\|g').should.eql(/^(?:(?=.)\+\(a\|[^/]*?\|c\\\\\|d\\\\\|e\\\\\\\\\|f\\\\\\\\\|g)$/);
  mm.makeRe('*(a|{b,c})').should.eql(/^(?:(?!\.)(?=.)(?:a|b)*|(?!\.)(?=.)(?:a|c)*)$/);
  mm.makeRe('{a,*(b|c,d)}').should.eql(/^(?:a|(?!\.)(?=.)[^/]*?\(b\|c|d\))$/);
github micromatch / braces / test / support / generate.js View on Github external
'use strict';

var braces = require('../..');
var mm = require('minimatch');
var text = require('text-table');
var Time = require('time-diff');
var time = new Time();

var table = [
  ['**Pattern**', '**braces**', '**minimatch**'],
  ['---', '---', '---']
];

// warm up both libs
mm.braceExpand('{a,b}');
braces('{a,b}');

function generate(pattern) {
  time.start('braces');
  var bval = braces(pattern, {rangeLimit: false}).join('|');
  var b = [wrap(format(bval.length)), '(' + time.end('braces', 'μs') + ')'].join(' ');

  time.start('minimatch');
  var mval = mm.braceExpand(pattern).join('|');
  var m = [wrap(format(mval.length)), '(' + time.end('minimatch', 'μs') + ')'].join(' ');

  table.push([wrap(pattern), b, m]);
  return table;
}

function wrap(str) {
github kamilkisiela / graphql-config / src / utils.ts View on Github external
return (globs || []).some(glob => {
    try {
      const globStat = lstatSync(join(configDir, glob));
      const globToMatch = globStat.isDirectory() ? `${glob}/**` : glob;
      return minimatch(filePath, globToMatch, {matchBase: true});
    } catch (error) {
      // Out of errors that lstat provides, EACCES and ENOENT are the
      // most likely. For both cases, run the match with the raw glob
      // and return the result.
      return minimatch(filePath, glob, {matchBase: true});
    }
  });
}
github electron-userland / electron-builder / packages / app-builder-lib / src / fileMatcher.ts View on Github external
computeParsedPatterns(result: Array<Minimatch>, fromDir?: string): void {
    const relativeFrom = fromDir == null ? null : path.relative(fromDir, this.from)

    if (this.patterns.length === 0 && relativeFrom != null) {
      // file mappings, from here is a file
      result.push(new Minimatch(relativeFrom, minimatchOptions))
      return
    }

    for (let pattern of this.patterns) {
      if (relativeFrom != null) {
        pattern = path.join(relativeFrom, pattern)
      }

      const parsedPattern = new Minimatch(pattern, minimatchOptions)
      result.push(parsedPattern)

      // do not add if contains dot (possibly file if has extension)
      if (!pattern.includes(".") && !hasMagic(parsedPattern)) {
        // https://github.com/electron-userland/electron-builder/issues/545
        // add **/*
        result.push(new Minimatch(`${pattern}/**/*`, minimatchOptions))
github angular / dgeni-packages / base / processors / read-files.js View on Github external
function getSourceFiles(sourceInfo) {

  var excludeMatcher = sourceInfo.exclude && new Minimatch(sourceInfo.exclude);

  var filesPromise = Q.nfcall(glob, sourceInfo.include);

  return filesPromise.then(function(files) {

    // Filter the files on whether they match the `exclude` property and whether they are files
    var filteredFilePromises = files.map(function(file) {

      if ( excludeMatcher && excludeMatcher.match(file) ) {
        // Return a promise for `null` if the path is excluded
        // Doing this first - it is synchronous - saves us even making the isFile call if not needed
        return Q(null);
      } else {
        // Return a promise for the file if path is a file, otherwise return a promise for `null`
        return qfs.isFile(file).then(function(isFile) { return isFile ? file : null; });
      }
github electron-userland / electron-builder / packages / app-builder-lib / src / fileMatcher.ts View on Github external
computeParsedPatterns(result: Array<Minimatch>, fromDir?: string): void {
    const relativeFrom = fromDir == null ? null : path.relative(fromDir, this.from)

    if (this.patterns.length === 0 && relativeFrom != null) {
      // file mappings, from here is a file
      result.push(new Minimatch(relativeFrom, minimatchOptions))
      return
    }

    for (let pattern of this.patterns) {
      if (relativeFrom != null) {
        pattern = path.join(relativeFrom, pattern)
      }

      const parsedPattern = new Minimatch(pattern, minimatchOptions)
      result.push(parsedPattern)

      // do not add if contains dot (possibly file if has extension)
      if (!pattern.includes(".") && !hasMagic(parsedPattern)) {
        // https://github.com/electron-userland/electron-builder/issues/545
        // add **/*
        result.push(new Minimatch(`${pattern}/**/*`, minimatchOptions))
      }
    }
  }