index.js 14 KB

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