index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. 'use strict';
  2. const util = require('util');
  3. const braces = require('braces');
  4. const picomatch = require('picomatch');
  5. const utils = require('picomatch/lib/utils');
  6. const isEmptyString = val => val === '' || val === './';
  7. /**
  8. * Returns an array of strings that match one or more glob patterns.
  9. *
  10. * ```js
  11. * const mm = require('micromatch');
  12. * // mm(list, patterns[, options]);
  13. *
  14. * console.log(mm(['a.js', 'a.txt'], ['*.js']));
  15. * //=> [ 'a.js' ]
  16. * ```
  17. * @param {String|Array<string>} `list` List of strings to match.
  18. * @param {String|Array<string>} `patterns` One or more glob patterns to use for matching.
  19. * @param {Object} `options` See available [options](#options)
  20. * @return {Array} Returns an array of matches
  21. * @summary false
  22. * @api public
  23. */
  24. const micromatch = (list, patterns, options) => {
  25. patterns = [].concat(patterns);
  26. list = [].concat(list);
  27. let omit = new Set();
  28. let keep = new Set();
  29. let items = new Set();
  30. let negatives = 0;
  31. let onResult = state => {
  32. items.add(state.output);
  33. if (options && options.onResult) {
  34. options.onResult(state);
  35. }
  36. };
  37. for (let i = 0; i < patterns.length; i++) {
  38. let isMatch = picomatch(String(patterns[i]), { ...options, onResult }, true);
  39. let negated = isMatch.state.negated || isMatch.state.negatedExtglob;
  40. if (negated) negatives++;
  41. for (let item of list) {
  42. let matched = isMatch(item, true);
  43. let match = negated ? !matched.isMatch : matched.isMatch;
  44. if (!match) continue;
  45. if (negated) {
  46. omit.add(matched.output);
  47. } else {
  48. omit.delete(matched.output);
  49. keep.add(matched.output);
  50. }
  51. }
  52. }
  53. let result = negatives === patterns.length ? [...items] : [...keep];
  54. let matches = result.filter(item => !omit.has(item));
  55. if (options && matches.length === 0) {
  56. if (options.failglob === true) {
  57. throw new Error(`No matches found for "${patterns.join(', ')}"`);
  58. }
  59. if (options.nonull === true || options.nullglob === true) {
  60. return options.unescape ? patterns.map(p => p.replace(/\\/g, '')) : patterns;
  61. }
  62. }
  63. return matches;
  64. };
  65. /**
  66. * Backwards compatibility
  67. */
  68. micromatch.match = micromatch;
  69. /**
  70. * Returns a matcher function from the given glob `pattern` and `options`.
  71. * The returned function takes a string to match as its only argument and returns
  72. * true if the string is a match.
  73. *
  74. * ```js
  75. * const mm = require('micromatch');
  76. * // mm.matcher(pattern[, options]);
  77. *
  78. * const isMatch = mm.matcher('*.!(*a)');
  79. * console.log(isMatch('a.a')); //=> false
  80. * console.log(isMatch('a.b')); //=> true
  81. * ```
  82. * @param {String} `pattern` Glob pattern
  83. * @param {Object} `options`
  84. * @return {Function} Returns a matcher function.
  85. * @api public
  86. */
  87. micromatch.matcher = (pattern, options) => picomatch(pattern, options);
  88. /**
  89. * Returns true if **any** of the given glob `patterns` match the specified `string`.
  90. *
  91. * ```js
  92. * const mm = require('micromatch');
  93. * // mm.isMatch(string, patterns[, options]);
  94. *
  95. * console.log(mm.isMatch('a.a', ['b.*', '*.a'])); //=> true
  96. * console.log(mm.isMatch('a.a', 'b.*')); //=> false
  97. * ```
  98. * @param {String} `str` The string to test.
  99. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  100. * @param {Object} `[options]` See available [options](#options).
  101. * @return {Boolean} Returns true if any patterns match `str`
  102. * @api public
  103. */
  104. micromatch.isMatch = (str, patterns, options) => picomatch(patterns, options)(str);
  105. /**
  106. * Backwards compatibility
  107. */
  108. micromatch.any = micromatch.isMatch;
  109. /**
  110. * Returns a list of strings that _**do not match any**_ of the given `patterns`.
  111. *
  112. * ```js
  113. * const mm = require('micromatch');
  114. * // mm.not(list, patterns[, options]);
  115. *
  116. * console.log(mm.not(['a.a', 'b.b', 'c.c'], '*.a'));
  117. * //=> ['b.b', 'c.c']
  118. * ```
  119. * @param {Array} `list` Array of strings to match.
  120. * @param {String|Array} `patterns` One or more glob pattern to use for matching.
  121. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  122. * @return {Array} Returns an array of strings that **do not match** the given patterns.
  123. * @api public
  124. */
  125. micromatch.not = (list, patterns, options = {}) => {
  126. patterns = [].concat(patterns).map(String);
  127. let result = new Set();
  128. let items = [];
  129. let onResult = state => {
  130. if (options.onResult) options.onResult(state);
  131. items.push(state.output);
  132. };
  133. let matches = new Set(micromatch(list, patterns, { ...options, onResult }));
  134. for (let item of items) {
  135. if (!matches.has(item)) {
  136. result.add(item);
  137. }
  138. }
  139. return [...result];
  140. };
  141. /**
  142. * Returns true if the given `string` contains the given pattern. Similar
  143. * to [.isMatch](#isMatch) but the pattern can match any part of the string.
  144. *
  145. * ```js
  146. * var mm = require('micromatch');
  147. * // mm.contains(string, pattern[, options]);
  148. *
  149. * console.log(mm.contains('aa/bb/cc', '*b'));
  150. * //=> true
  151. * console.log(mm.contains('aa/bb/cc', '*d'));
  152. * //=> false
  153. * ```
  154. * @param {String} `str` The string to match.
  155. * @param {String|Array} `patterns` Glob pattern to use for matching.
  156. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  157. * @return {Boolean} Returns true if any of the patterns matches any part of `str`.
  158. * @api public
  159. */
  160. micromatch.contains = (str, pattern, options) => {
  161. if (typeof str !== 'string') {
  162. throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
  163. }
  164. if (Array.isArray(pattern)) {
  165. return pattern.some(p => micromatch.contains(str, p, options));
  166. }
  167. if (typeof pattern === 'string') {
  168. if (isEmptyString(str) || isEmptyString(pattern)) {
  169. return false;
  170. }
  171. if (str.includes(pattern) || (str.startsWith('./') && str.slice(2).includes(pattern))) {
  172. return true;
  173. }
  174. }
  175. return micromatch.isMatch(str, pattern, { ...options, contains: true });
  176. };
  177. /**
  178. * Filter the keys of the given object with the given `glob` pattern
  179. * and `options`. Does not attempt to match nested keys. If you need this feature,
  180. * use [glob-object][] instead.
  181. *
  182. * ```js
  183. * const mm = require('micromatch');
  184. * // mm.matchKeys(object, patterns[, options]);
  185. *
  186. * const obj = { aa: 'a', ab: 'b', ac: 'c' };
  187. * console.log(mm.matchKeys(obj, '*b'));
  188. * //=> { ab: 'b' }
  189. * ```
  190. * @param {Object} `object` The object with keys to filter.
  191. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  192. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  193. * @return {Object} Returns an object with only keys that match the given patterns.
  194. * @api public
  195. */
  196. micromatch.matchKeys = (obj, patterns, options) => {
  197. if (!utils.isObject(obj)) {
  198. throw new TypeError('Expected the first argument to be an object');
  199. }
  200. let keys = micromatch(Object.keys(obj), patterns, options);
  201. let res = {};
  202. for (let key of keys) res[key] = obj[key];
  203. return res;
  204. };
  205. /**
  206. * Returns true if some of the strings in the given `list` match any of the given glob `patterns`.
  207. *
  208. * ```js
  209. * const mm = require('micromatch');
  210. * // mm.some(list, patterns[, options]);
  211. *
  212. * console.log(mm.some(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  213. * // true
  214. * console.log(mm.some(['foo.js'], ['*.js', '!foo.js']));
  215. * // false
  216. * ```
  217. * @param {String|Array} `list` The string or array of strings to test. Returns as soon as the first match is found.
  218. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  219. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  220. * @return {Boolean} Returns true if any `patterns` matches any of the strings in `list`
  221. * @api public
  222. */
  223. micromatch.some = (list, patterns, options) => {
  224. let items = [].concat(list);
  225. for (let pattern of [].concat(patterns)) {
  226. let isMatch = picomatch(String(pattern), options);
  227. if (items.some(item => isMatch(item))) {
  228. return true;
  229. }
  230. }
  231. return false;
  232. };
  233. /**
  234. * Returns true if every string in the given `list` matches
  235. * any of the given glob `patterns`.
  236. *
  237. * ```js
  238. * const mm = require('micromatch');
  239. * // mm.every(list, patterns[, options]);
  240. *
  241. * console.log(mm.every('foo.js', ['foo.js']));
  242. * // true
  243. * console.log(mm.every(['foo.js', 'bar.js'], ['*.js']));
  244. * // true
  245. * console.log(mm.every(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  246. * // false
  247. * console.log(mm.every(['foo.js'], ['*.js', '!foo.js']));
  248. * // false
  249. * ```
  250. * @param {String|Array} `list` The string or array of strings to test.
  251. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  252. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  253. * @return {Boolean} Returns true if all `patterns` matches all of the strings in `list`
  254. * @api public
  255. */
  256. micromatch.every = (list, patterns, options) => {
  257. let items = [].concat(list);
  258. for (let pattern of [].concat(patterns)) {
  259. let isMatch = picomatch(String(pattern), options);
  260. if (!items.every(item => isMatch(item))) {
  261. return false;
  262. }
  263. }
  264. return true;
  265. };
  266. /**
  267. * Returns true if **all** of the given `patterns` match
  268. * the specified string.
  269. *
  270. * ```js
  271. * const mm = require('micromatch');
  272. * // mm.all(string, patterns[, options]);
  273. *
  274. * console.log(mm.all('foo.js', ['foo.js']));
  275. * // true
  276. *
  277. * console.log(mm.all('foo.js', ['*.js', '!foo.js']));
  278. * // false
  279. *
  280. * console.log(mm.all('foo.js', ['*.js', 'foo.js']));
  281. * // true
  282. *
  283. * console.log(mm.all('foo.js', ['*.js', 'f*', '*o*', '*o.js']));
  284. * // true
  285. * ```
  286. * @param {String|Array} `str` The string to test.
  287. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  288. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  289. * @return {Boolean} Returns true if any patterns match `str`
  290. * @api public
  291. */
  292. micromatch.all = (str, patterns, options) => {
  293. if (typeof str !== 'string') {
  294. throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
  295. }
  296. return [].concat(patterns).every(p => picomatch(p, options)(str));
  297. };
  298. /**
  299. * Returns an array of matches captured by `pattern` in `string, or `null` if the pattern did not match.
  300. *
  301. * ```js
  302. * const mm = require('micromatch');
  303. * // mm.capture(pattern, string[, options]);
  304. *
  305. * console.log(mm.capture('test/*.js', 'test/foo.js'));
  306. * //=> ['foo']
  307. * console.log(mm.capture('test/*.js', 'foo/bar.css'));
  308. * //=> null
  309. * ```
  310. * @param {String} `glob` Glob pattern to use for matching.
  311. * @param {String} `input` String to match
  312. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  313. * @return {Array|null} Returns an array of captures if the input matches the glob pattern, otherwise `null`.
  314. * @api public
  315. */
  316. micromatch.capture = (glob, input, options) => {
  317. let posix = utils.isWindows(options);
  318. let regex = picomatch.makeRe(String(glob), { ...options, capture: true });
  319. let match = regex.exec(posix ? utils.toPosixSlashes(input) : input);
  320. if (match) {
  321. return match.slice(1).map(v => v === void 0 ? '' : v);
  322. }
  323. };
  324. /**
  325. * Create a regular expression from the given glob `pattern`.
  326. *
  327. * ```js
  328. * const mm = require('micromatch');
  329. * // mm.makeRe(pattern[, options]);
  330. *
  331. * console.log(mm.makeRe('*.js'));
  332. * //=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/
  333. * ```
  334. * @param {String} `pattern` A glob pattern to convert to regex.
  335. * @param {Object} `options`
  336. * @return {RegExp} Returns a regex created from the given pattern.
  337. * @api public
  338. */
  339. micromatch.makeRe = (...args) => picomatch.makeRe(...args);
  340. /**
  341. * Scan a glob pattern to separate the pattern into segments. Used
  342. * by the [split](#split) method.
  343. *
  344. * ```js
  345. * const mm = require('micromatch');
  346. * const state = mm.scan(pattern[, options]);
  347. * ```
  348. * @param {String} `pattern`
  349. * @param {Object} `options`
  350. * @return {Object} Returns an object with
  351. * @api public
  352. */
  353. micromatch.scan = (...args) => picomatch.scan(...args);
  354. /**
  355. * Parse a glob pattern to create the source string for a regular
  356. * expression.
  357. *
  358. * ```js
  359. * const mm = require('micromatch');
  360. * const state = mm.parse(pattern[, options]);
  361. * ```
  362. * @param {String} `glob`
  363. * @param {Object} `options`
  364. * @return {Object} Returns an object with useful properties and output to be used as regex source string.
  365. * @api public
  366. */
  367. micromatch.parse = (patterns, options) => {
  368. let res = [];
  369. for (let pattern of [].concat(patterns || [])) {
  370. for (let str of braces(String(pattern), options)) {
  371. res.push(picomatch.parse(str, options));
  372. }
  373. }
  374. return res;
  375. };
  376. /**
  377. * Process the given brace `pattern`.
  378. *
  379. * ```js
  380. * const { braces } = require('micromatch');
  381. * console.log(braces('foo/{a,b,c}/bar'));
  382. * //=> [ 'foo/(a|b|c)/bar' ]
  383. *
  384. * console.log(braces('foo/{a,b,c}/bar', { expand: true }));
  385. * //=> [ 'foo/a/bar', 'foo/b/bar', 'foo/c/bar' ]
  386. * ```
  387. * @param {String} `pattern` String with brace pattern to process.
  388. * @param {Object} `options` Any [options](#options) to change how expansion is performed. See the [braces][] library for all available options.
  389. * @return {Array}
  390. * @api public
  391. */
  392. micromatch.braces = (pattern, options) => {
  393. if (typeof pattern !== 'string') throw new TypeError('Expected a string');
  394. if ((options && options.nobrace === true) || !/\{.*\}/.test(pattern)) {
  395. return [pattern];
  396. }
  397. return braces(pattern, options);
  398. };
  399. /**
  400. * Expand braces
  401. */
  402. micromatch.braceExpand = (pattern, options) => {
  403. if (typeof pattern !== 'string') throw new TypeError('Expected a string');
  404. return micromatch.braces(pattern, { ...options, expand: true });
  405. };
  406. /**
  407. * Expose micromatch
  408. */
  409. module.exports = micromatch;