3 const util = require('util');
4 const braces = require('braces');
5 const picomatch = require('picomatch');
6 const utils = require('picomatch/lib/utils');
7 const isEmptyString = val => val === '' || val === './';
10 * Returns an array of strings that match one or more glob patterns.
13 * const mm = require('micromatch');
14 * // mm(list, patterns[, options]);
16 * console.log(mm(['a.js', 'a.txt'], ['*.js']));
19 * @param {String|Array<string>} `list` List of strings to match.
20 * @param {String|Array<string>} `patterns` One or more glob patterns to use for matching.
21 * @param {Object} `options` See available [options](#options)
22 * @return {Array} Returns an array of matches
27 const micromatch = (list, patterns, options) => {
28 patterns = [].concat(patterns);
29 list = [].concat(list);
33 let items = new Set();
36 let onResult = state => {
37 items.add(state.output);
38 if (options && options.onResult) {
39 options.onResult(state);
43 for (let i = 0; i < patterns.length; i++) {
44 let isMatch = picomatch(String(patterns[i]), { ...options, onResult }, true);
45 let negated = isMatch.state.negated || isMatch.state.negatedExtglob;
46 if (negated) negatives++;
48 for (let item of list) {
49 let matched = isMatch(item, true);
51 let match = negated ? !matched.isMatch : matched.isMatch;
55 omit.add(matched.output);
57 omit.delete(matched.output);
58 keep.add(matched.output);
63 let result = negatives === patterns.length ? [...items] : [...keep];
64 let matches = result.filter(item => !omit.has(item));
66 if (options && matches.length === 0) {
67 if (options.failglob === true) {
68 throw new Error(`No matches found for "${patterns.join(', ')}"`);
71 if (options.nonull === true || options.nullglob === true) {
72 return options.unescape ? patterns.map(p => p.replace(/\\/g, '')) : patterns;
80 * Backwards compatibility
83 micromatch.match = micromatch;
86 * Returns a matcher function from the given glob `pattern` and `options`.
87 * The returned function takes a string to match as its only argument and returns
88 * true if the string is a match.
91 * const mm = require('micromatch');
92 * // mm.matcher(pattern[, options]);
94 * const isMatch = mm.matcher('*.!(*a)');
95 * console.log(isMatch('a.a')); //=> false
96 * console.log(isMatch('a.b')); //=> true
98 * @param {String} `pattern` Glob pattern
99 * @param {Object} `options`
100 * @return {Function} Returns a matcher function.
104 micromatch.matcher = (pattern, options) => picomatch(pattern, options);
107 * Returns true if **any** of the given glob `patterns` match the specified `string`.
110 * const mm = require('micromatch');
111 * // mm.isMatch(string, patterns[, options]);
113 * console.log(mm.isMatch('a.a', ['b.*', '*.a'])); //=> true
114 * console.log(mm.isMatch('a.a', 'b.*')); //=> false
116 * @param {String} `str` The string to test.
117 * @param {String|Array} `patterns` One or more glob patterns to use for matching.
118 * @param {Object} `[options]` See available [options](#options).
119 * @return {Boolean} Returns true if any patterns match `str`
123 micromatch.isMatch = (str, patterns, options) => picomatch(patterns, options)(str);
126 * Backwards compatibility
129 micromatch.any = micromatch.isMatch;
132 * Returns a list of strings that _**do not match any**_ of the given `patterns`.
135 * const mm = require('micromatch');
136 * // mm.not(list, patterns[, options]);
138 * console.log(mm.not(['a.a', 'b.b', 'c.c'], '*.a'));
139 * //=> ['b.b', 'c.c']
141 * @param {Array} `list` Array of strings to match.
142 * @param {String|Array} `patterns` One or more glob pattern to use for matching.
143 * @param {Object} `options` See available [options](#options) for changing how matches are performed
144 * @return {Array} Returns an array of strings that **do not match** the given patterns.
148 micromatch.not = (list, patterns, options = {}) => {
149 patterns = [].concat(patterns).map(String);
150 let result = new Set();
153 let onResult = state => {
154 if (options.onResult) options.onResult(state);
155 items.push(state.output);
158 let matches = new Set(micromatch(list, patterns, { ...options, onResult }));
160 for (let item of items) {
161 if (!matches.has(item)) {
169 * Returns true if the given `string` contains the given pattern. Similar
170 * to [.isMatch](#isMatch) but the pattern can match any part of the string.
173 * var mm = require('micromatch');
174 * // mm.contains(string, pattern[, options]);
176 * console.log(mm.contains('aa/bb/cc', '*b'));
178 * console.log(mm.contains('aa/bb/cc', '*d'));
181 * @param {String} `str` The string to match.
182 * @param {String|Array} `patterns` Glob pattern to use for matching.
183 * @param {Object} `options` See available [options](#options) for changing how matches are performed
184 * @return {Boolean} Returns true if any of the patterns matches any part of `str`.
188 micromatch.contains = (str, pattern, options) => {
189 if (typeof str !== 'string') {
190 throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
193 if (Array.isArray(pattern)) {
194 return pattern.some(p => micromatch.contains(str, p, options));
197 if (typeof pattern === 'string') {
198 if (isEmptyString(str) || isEmptyString(pattern)) {
202 if (str.includes(pattern) || (str.startsWith('./') && str.slice(2).includes(pattern))) {
207 return micromatch.isMatch(str, pattern, { ...options, contains: true });
211 * Filter the keys of the given object with the given `glob` pattern
212 * and `options`. Does not attempt to match nested keys. If you need this feature,
213 * use [glob-object][] instead.
216 * const mm = require('micromatch');
217 * // mm.matchKeys(object, patterns[, options]);
219 * const obj = { aa: 'a', ab: 'b', ac: 'c' };
220 * console.log(mm.matchKeys(obj, '*b'));
223 * @param {Object} `object` The object with keys to filter.
224 * @param {String|Array} `patterns` One or more glob patterns to use for matching.
225 * @param {Object} `options` See available [options](#options) for changing how matches are performed
226 * @return {Object} Returns an object with only keys that match the given patterns.
230 micromatch.matchKeys = (obj, patterns, options) => {
231 if (!utils.isObject(obj)) {
232 throw new TypeError('Expected the first argument to be an object');
234 let keys = micromatch(Object.keys(obj), patterns, options);
236 for (let key of keys) res[key] = obj[key];
241 * Returns true if some of the strings in the given `list` match any of the given glob `patterns`.
244 * const mm = require('micromatch');
245 * // mm.some(list, patterns[, options]);
247 * console.log(mm.some(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
249 * console.log(mm.some(['foo.js'], ['*.js', '!foo.js']));
252 * @param {String|Array} `list` The string or array of strings to test. Returns as soon as the first match is found.
253 * @param {String|Array} `patterns` One or more glob patterns to use for matching.
254 * @param {Object} `options` See available [options](#options) for changing how matches are performed
255 * @return {Boolean} Returns true if any `patterns` matches any of the strings in `list`
259 micromatch.some = (list, patterns, options) => {
260 let items = [].concat(list);
262 for (let pattern of [].concat(patterns)) {
263 let isMatch = picomatch(String(pattern), options);
264 if (items.some(item => isMatch(item))) {
272 * Returns true if every string in the given `list` matches
273 * any of the given glob `patterns`.
276 * const mm = require('micromatch');
277 * // mm.every(list, patterns[, options]);
279 * console.log(mm.every('foo.js', ['foo.js']));
281 * console.log(mm.every(['foo.js', 'bar.js'], ['*.js']));
283 * console.log(mm.every(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
285 * console.log(mm.every(['foo.js'], ['*.js', '!foo.js']));
288 * @param {String|Array} `list` The string or array of strings to test.
289 * @param {String|Array} `patterns` One or more glob patterns to use for matching.
290 * @param {Object} `options` See available [options](#options) for changing how matches are performed
291 * @return {Boolean} Returns true if all `patterns` matches all of the strings in `list`
295 micromatch.every = (list, patterns, options) => {
296 let items = [].concat(list);
298 for (let pattern of [].concat(patterns)) {
299 let isMatch = picomatch(String(pattern), options);
300 if (!items.every(item => isMatch(item))) {
308 * Returns true if **all** of the given `patterns` match
309 * the specified string.
312 * const mm = require('micromatch');
313 * // mm.all(string, patterns[, options]);
315 * console.log(mm.all('foo.js', ['foo.js']));
318 * console.log(mm.all('foo.js', ['*.js', '!foo.js']));
321 * console.log(mm.all('foo.js', ['*.js', 'foo.js']));
324 * console.log(mm.all('foo.js', ['*.js', 'f*', '*o*', '*o.js']));
327 * @param {String|Array} `str` The string to test.
328 * @param {String|Array} `patterns` One or more glob patterns to use for matching.
329 * @param {Object} `options` See available [options](#options) for changing how matches are performed
330 * @return {Boolean} Returns true if any patterns match `str`
334 micromatch.all = (str, patterns, options) => {
335 if (typeof str !== 'string') {
336 throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
339 return [].concat(patterns).every(p => picomatch(p, options)(str));
343 * Returns an array of matches captured by `pattern` in `string, or `null` if the pattern did not match.
346 * const mm = require('micromatch');
347 * // mm.capture(pattern, string[, options]);
349 * console.log(mm.capture('test/*.js', 'test/foo.js'));
351 * console.log(mm.capture('test/*.js', 'foo/bar.css'));
354 * @param {String} `glob` Glob pattern to use for matching.
355 * @param {String} `input` String to match
356 * @param {Object} `options` See available [options](#options) for changing how matches are performed
357 * @return {Array|null} Returns an array of captures if the input matches the glob pattern, otherwise `null`.
361 micromatch.capture = (glob, input, options) => {
362 let posix = utils.isWindows(options);
363 let regex = picomatch.makeRe(String(glob), { ...options, capture: true });
364 let match = regex.exec(posix ? utils.toPosixSlashes(input) : input);
367 return match.slice(1).map(v => v === void 0 ? '' : v);
372 * Create a regular expression from the given glob `pattern`.
375 * const mm = require('micromatch');
376 * // mm.makeRe(pattern[, options]);
378 * console.log(mm.makeRe('*.js'));
379 * //=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/
381 * @param {String} `pattern` A glob pattern to convert to regex.
382 * @param {Object} `options`
383 * @return {RegExp} Returns a regex created from the given pattern.
387 micromatch.makeRe = (...args) => picomatch.makeRe(...args);
390 * Scan a glob pattern to separate the pattern into segments. Used
391 * by the [split](#split) method.
394 * const mm = require('micromatch');
395 * const state = mm.scan(pattern[, options]);
397 * @param {String} `pattern`
398 * @param {Object} `options`
399 * @return {Object} Returns an object with
403 micromatch.scan = (...args) => picomatch.scan(...args);
406 * Parse a glob pattern to create the source string for a regular
410 * const mm = require('micromatch');
411 * const state = mm.parse(pattern[, options]);
413 * @param {String} `glob`
414 * @param {Object} `options`
415 * @return {Object} Returns an object with useful properties and output to be used as regex source string.
419 micromatch.parse = (patterns, options) => {
421 for (let pattern of [].concat(patterns || [])) {
422 for (let str of braces(String(pattern), options)) {
423 res.push(picomatch.parse(str, options));
430 * Process the given brace `pattern`.
433 * const { braces } = require('micromatch');
434 * console.log(braces('foo/{a,b,c}/bar'));
435 * //=> [ 'foo/(a|b|c)/bar' ]
437 * console.log(braces('foo/{a,b,c}/bar', { expand: true }));
438 * //=> [ 'foo/a/bar', 'foo/b/bar', 'foo/c/bar' ]
440 * @param {String} `pattern` String with brace pattern to process.
441 * @param {Object} `options` Any [options](#options) to change how expansion is performed. See the [braces][] library for all available options.
446 micromatch.braces = (pattern, options) => {
447 if (typeof pattern !== 'string') throw new TypeError('Expected a string');
448 if ((options && options.nobrace === true) || !/\{.*\}/.test(pattern)) {
451 return braces(pattern, options);
458 micromatch.braceExpand = (pattern, options) => {
459 if (typeof pattern !== 'string') throw new TypeError('Expected a string');
460 return micromatch.braces(pattern, { ...options, expand: true });
467 module.exports = micromatch;