How to use micromatch - 10 common examples

To help you get started, we’ve selected a few micromatch 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 thumbsup / thumbsup / test / components / index / glob.spec.js View on Github external
it('bootstraps micromatch', () => {
    // This is a workaround for https://github.com/tschaub/mock-fs/issues/213
    // Because micromatch() loads packages dynamically which doesn't work if the filesystem is being mocked
    // So we need to pre-load them now
    require('micromatch').match('file.txt', '**/**')
  })
github yarnpkg / yarn / src / util / filter.js View on Github external
export function matchesFilter(filter: IgnoreFilter, basename: string, loc: string): boolean {
  let filterByBasename = true;
  if (filter.base && filter.base !== '.') {
    loc = path.relative(filter.base, loc);
    filterByBasename = false;
  }
  // the micromatch regex expects unix path separators
  loc = loc.replace('\\', '/');

  return (
    filter.regex.test(loc) ||
    filter.regex.test(`/${loc}`) ||
    (filterByBasename && filter.regex.test(basename)) ||
    mm.isMatch(loc, filter.pattern)
  );
}
github babel / babel / packages / babel-core / src / config / build-config-chain.js View on Github external
if (res.some(re => re.test(context.filename))) return true;
  if (fns.some(fn => fn(filename))) return true;

  if (strings.length > 0) {
    const possibleDirs = getPossibleDirs(context);

    const absolutePatterns = strings.map(pattern => {
      // Preserve the "!" prefix so that micromatch can use it for negation.
      const negate = pattern[0] === "!";
      if (negate) pattern = pattern.slice(1);

      return (negate ? "!" : "") + path.resolve(dirname, pattern);
    });

    if (
      micromatch(possibleDirs, absolutePatterns, { nocase: true }).length > 0
    ) {
      return true;
    }
  }

  return false;
}
github parcel-bundler / parcel / packages / resolvers / default / src / DefaultResolver.js View on Github external
lookupAlias(aliases, filename, dir) {
    // First, try looking up the exact filename
    let alias = aliases[filename];
    if (alias == null) {
      // Otherwise, try replacing glob keys
      for (let key in aliases) {
        let val = aliases[key];
        if (typeof val === 'string' && isGlob(key)) {
          let re = micromatch.makeRe(key, {capture: true});
          if (re.test(filename)) {
            alias = filename.replace(re, val);
            break;
          }
        }
      }
    }

    if (typeof alias === 'string') {
      return this.resolveFilename(alias, dir);
    }

    return typeof alias === 'string' ? alias : null;
  }
github parcel-bundler / parcel / packages / resolvers / default / src / DefaultResolver.js View on Github external
lookupAlias(aliases, filename, dir) {
    // First, try looking up the exact filename
    let alias = aliases[filename];
    if (alias == null) {
      // Otherwise, try replacing glob keys
      for (let key in aliases) {
        let val = aliases[key];
        if (typeof val === 'string' && isGlob(key)) {
          let re = micromatch.makeRe(key, {capture: true});
          if (re.test(filename)) {
            alias = filename.replace(re, val);
            break;
          }
        }
      }
    }

    if (typeof alias === 'string') {
      return this.resolveFilename(alias, dir);
    } else if (alias === false) {
      return false;
    }

    return null;
  }
github file-icons / atom / lib / service / strategies / linguist-strategy.js View on Github external
// Only acknowledge languages with icons
				if(!languageIcon)
					return null;

				// Lazily require the micromatch dependency due to its weight.
				const Micromatch = require("micromatch");
				
				pattern = path.dirname(filePath) + "/" + (/^\//.test(pattern) ? "" : "**") + "/" + pattern;
				pattern = path.resolve(pattern);
				
				// Normalise path separators on Windows
				if("win32" === process.platform)
					pattern = pattern.replace(/\\/g, "/");
				
				pattern = Micromatch.makeRe(pattern, {
					unixify: false,
					nonegate: true,
					dot: true,
				});
				return pattern
					? [pattern, languageIcon]
					: null;
			})
			.filter(a => a);
github electerious / Rosid / src / copy.js View on Github external
const filter = (filePath) => {

		// The filePath is absolute, but should be relative as micromatch will
		// match against the relative path of the routes. Matching against
		// an absolute path requires the use of path.join which causes issues on Windows:
		// https://github.com/micromatch/micromatch/issues/95
		filePath = path.relative(srcPath, filePath)

		const fileName = path.parse(filePath).base

		const isIgnored = mm.any(filePath, ignoredFiles)
		const isJunk = junk.is(fileName)

		// Copy file when it's not ignored or not junk
		const copy = isIgnored === false && isJunk === false

		if (opts.verbose === true) {

			if (copy === false) log(`{cyan:Skipping file: {grey:${ filePath }`)
			if (copy === true) log(`{cyan:Copying file: {grey:${ filePath }`)

		}

		// Return true to include, false to exclude
		return copy

	}
github rollup / plugins / packages / pluginutils / src / createFilter.ts View on Github external
test: (what: string) => {
            // this refactor is a tad overly verbose but makes for easy debugging
            const pattern = getMatcherString(id, resolutionBase);
            const fn = mm.matcher(pattern, { dot: true });
            const result = fn(what);

            return result;
          }
        };
github isiahmeadows / thallium / lib / cli / iterate-glob.js View on Github external
exports.create = function (globs) {
    if (!Array.isArray(globs)) globs = [globs]

    var positives = []
    var negatives = []
    var accumulator = []

    for (var i = 0; i < globs.length; i++) {
        var glob = globs[i]

        if (typeof glob === "string" && glob[0] === "!") {
            // Create Minimatch instances for negative glob patterns
            accumulator.push(micromatch.matcher(resolveGlob(glob)))
        } else if (glob instanceof RegExp) {
            accumulator.push(glob)
        } else {
            positives.push(glob)
            negatives.push(accumulator.slice())
        }
    }

    if (positives.length === 0) {
        throw new Error("Missing positive glob")
    }

    var opts = {
        nodir: true,
        symlinks: {},
        statCache: {},
github yvele / poosh / packages / poosh-core / src / helpers / GlobMatcher.js View on Github external
matchSingle(fileName, pattern) {
    if (!pattern) {
      return false;
    }

    // Cache the micropatch "pattern parsing"
    let isMatch = this.cache[pattern];
    if (!isMatch) {
      isMatch = micromatch.matcher(pattern);
      this.cache[pattern] = isMatch;
    }

    return isMatch(fileName);
  }

micromatch

Glob matching for javascript/node.js. A replacement and faster alternative to minimatch and multimatch.

MIT
Latest version published 2 years ago

Package Health Score

87 / 100
Full package analysis