Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. 'use strict';
  2. /**
  3. * Module dependencies
  4. */
  5. var util = require('util');
  6. var braces = require('braces');
  7. var toRegex = require('to-regex');
  8. var extend = require('extend-shallow');
  9. /**
  10. * Local dependencies
  11. */
  12. var compilers = require('./lib/compilers');
  13. var parsers = require('./lib/parsers');
  14. var cache = require('./lib/cache');
  15. var utils = require('./lib/utils');
  16. var MAX_LENGTH = 1024 * 64;
  17. /**
  18. * The main function takes a list of strings and one or more
  19. * glob patterns to use for matching.
  20. *
  21. * ```js
  22. * var mm = require('micromatch');
  23. * mm(list, patterns[, options]);
  24. *
  25. * console.log(mm(['a.js', 'a.txt'], ['*.js']));
  26. * //=> [ 'a.js' ]
  27. * ```
  28. * @param {Array} `list` A list of strings to match
  29. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  30. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  31. * @return {Array} Returns an array of matches
  32. * @summary false
  33. * @api public
  34. */
  35. function micromatch(list, patterns, options) {
  36. patterns = utils.arrayify(patterns);
  37. list = utils.arrayify(list);
  38. var len = patterns.length;
  39. if (list.length === 0 || len === 0) {
  40. return [];
  41. }
  42. if (len === 1) {
  43. return micromatch.match(list, patterns[0], options);
  44. }
  45. var omit = [];
  46. var keep = [];
  47. var idx = -1;
  48. while (++idx < len) {
  49. var pattern = patterns[idx];
  50. if (typeof pattern === 'string' && pattern.charCodeAt(0) === 33 /* ! */) {
  51. omit.push.apply(omit, micromatch.match(list, pattern.slice(1), options));
  52. } else {
  53. keep.push.apply(keep, micromatch.match(list, pattern, options));
  54. }
  55. }
  56. var matches = utils.diff(keep, omit);
  57. if (!options || options.nodupes !== false) {
  58. return utils.unique(matches);
  59. }
  60. return matches;
  61. }
  62. /**
  63. * Similar to the main function, but `pattern` must be a string.
  64. *
  65. * ```js
  66. * var mm = require('micromatch');
  67. * mm.match(list, pattern[, options]);
  68. *
  69. * console.log(mm.match(['a.a', 'a.aa', 'a.b', 'a.c'], '*.a'));
  70. * //=> ['a.a', 'a.aa']
  71. * ```
  72. * @param {Array} `list` Array of strings to match
  73. * @param {String} `pattern` Glob pattern to use for matching.
  74. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  75. * @return {Array} Returns an array of matches
  76. * @api public
  77. */
  78. micromatch.match = function(list, pattern, options) {
  79. if (Array.isArray(pattern)) {
  80. throw new TypeError('expected pattern to be a string');
  81. }
  82. var unixify = utils.unixify(options);
  83. var isMatch = memoize('match', pattern, options, micromatch.matcher);
  84. var matches = [];
  85. list = utils.arrayify(list);
  86. var len = list.length;
  87. var idx = -1;
  88. while (++idx < len) {
  89. var ele = list[idx];
  90. if (ele === pattern || isMatch(ele)) {
  91. matches.push(utils.value(ele, unixify, options));
  92. }
  93. }
  94. // if no options were passed, uniquify results and return
  95. if (typeof options === 'undefined') {
  96. return utils.unique(matches);
  97. }
  98. if (matches.length === 0) {
  99. if (options.failglob === true) {
  100. throw new Error('no matches found for "' + pattern + '"');
  101. }
  102. if (options.nonull === true || options.nullglob === true) {
  103. return [options.unescape ? utils.unescape(pattern) : pattern];
  104. }
  105. }
  106. // if `opts.ignore` was defined, diff ignored list
  107. if (options.ignore) {
  108. matches = micromatch.not(matches, options.ignore, options);
  109. }
  110. return options.nodupes !== false ? utils.unique(matches) : matches;
  111. };
  112. /**
  113. * Returns true if the specified `string` matches the given glob `pattern`.
  114. *
  115. * ```js
  116. * var mm = require('micromatch');
  117. * mm.isMatch(string, pattern[, options]);
  118. *
  119. * console.log(mm.isMatch('a.a', '*.a'));
  120. * //=> true
  121. * console.log(mm.isMatch('a.b', '*.a'));
  122. * //=> false
  123. * ```
  124. * @param {String} `string` String to match
  125. * @param {String} `pattern` Glob pattern to use for matching.
  126. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  127. * @return {Boolean} Returns true if the string matches the glob pattern.
  128. * @api public
  129. */
  130. micromatch.isMatch = function(str, pattern, options) {
  131. if (typeof str !== 'string') {
  132. throw new TypeError('expected a string: "' + util.inspect(str) + '"');
  133. }
  134. if (isEmptyString(str) || isEmptyString(pattern)) {
  135. return false;
  136. }
  137. var equals = utils.equalsPattern(options);
  138. if (equals(str)) {
  139. return true;
  140. }
  141. var isMatch = memoize('isMatch', pattern, options, micromatch.matcher);
  142. return isMatch(str);
  143. };
  144. /**
  145. * Returns true if some of the strings in the given `list` match any of the
  146. * given glob `patterns`.
  147. *
  148. * ```js
  149. * var mm = require('micromatch');
  150. * mm.some(list, patterns[, options]);
  151. *
  152. * console.log(mm.some(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  153. * // true
  154. * console.log(mm.some(['foo.js'], ['*.js', '!foo.js']));
  155. * // false
  156. * ```
  157. * @param {String|Array} `list` The string or array of strings to test. Returns as soon as the first match is found.
  158. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  159. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  160. * @return {Boolean} Returns true if any patterns match `str`
  161. * @api public
  162. */
  163. micromatch.some = function(list, patterns, options) {
  164. if (typeof list === 'string') {
  165. list = [list];
  166. }
  167. for (var i = 0; i < list.length; i++) {
  168. if (micromatch(list[i], patterns, options).length === 1) {
  169. return true;
  170. }
  171. }
  172. return false;
  173. };
  174. /**
  175. * Returns true if every string in the given `list` matches
  176. * any of the given glob `patterns`.
  177. *
  178. * ```js
  179. * var mm = require('micromatch');
  180. * mm.every(list, patterns[, options]);
  181. *
  182. * console.log(mm.every('foo.js', ['foo.js']));
  183. * // true
  184. * console.log(mm.every(['foo.js', 'bar.js'], ['*.js']));
  185. * // true
  186. * console.log(mm.every(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  187. * // false
  188. * console.log(mm.every(['foo.js'], ['*.js', '!foo.js']));
  189. * // false
  190. * ```
  191. * @param {String|Array} `list` The string or array of strings to test.
  192. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  193. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  194. * @return {Boolean} Returns true if any patterns match `str`
  195. * @api public
  196. */
  197. micromatch.every = function(list, patterns, options) {
  198. if (typeof list === 'string') {
  199. list = [list];
  200. }
  201. for (var i = 0; i < list.length; i++) {
  202. if (micromatch(list[i], patterns, options).length !== 1) {
  203. return false;
  204. }
  205. }
  206. return true;
  207. };
  208. /**
  209. * Returns true if **any** of the given glob `patterns`
  210. * match the specified `string`.
  211. *
  212. * ```js
  213. * var mm = require('micromatch');
  214. * mm.any(string, patterns[, options]);
  215. *
  216. * console.log(mm.any('a.a', ['b.*', '*.a']));
  217. * //=> true
  218. * console.log(mm.any('a.a', 'b.*'));
  219. * //=> false
  220. * ```
  221. * @param {String|Array} `str` The string to test.
  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 match `str`
  225. * @api public
  226. */
  227. micromatch.any = function(str, patterns, options) {
  228. if (typeof str !== 'string') {
  229. throw new TypeError('expected a string: "' + util.inspect(str) + '"');
  230. }
  231. if (isEmptyString(str) || isEmptyString(patterns)) {
  232. return false;
  233. }
  234. if (typeof patterns === 'string') {
  235. patterns = [patterns];
  236. }
  237. for (var i = 0; i < patterns.length; i++) {
  238. if (micromatch.isMatch(str, patterns[i], options)) {
  239. return true;
  240. }
  241. }
  242. return false;
  243. };
  244. /**
  245. * Returns true if **all** of the given `patterns` match
  246. * the specified string.
  247. *
  248. * ```js
  249. * var mm = require('micromatch');
  250. * mm.all(string, patterns[, options]);
  251. *
  252. * console.log(mm.all('foo.js', ['foo.js']));
  253. * // true
  254. *
  255. * console.log(mm.all('foo.js', ['*.js', '!foo.js']));
  256. * // false
  257. *
  258. * console.log(mm.all('foo.js', ['*.js', 'foo.js']));
  259. * // true
  260. *
  261. * console.log(mm.all('foo.js', ['*.js', 'f*', '*o*', '*o.js']));
  262. * // true
  263. * ```
  264. * @param {String|Array} `str` The string to test.
  265. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  266. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  267. * @return {Boolean} Returns true if any patterns match `str`
  268. * @api public
  269. */
  270. micromatch.all = function(str, patterns, options) {
  271. if (typeof str !== 'string') {
  272. throw new TypeError('expected a string: "' + util.inspect(str) + '"');
  273. }
  274. if (typeof patterns === 'string') {
  275. patterns = [patterns];
  276. }
  277. for (var i = 0; i < patterns.length; i++) {
  278. if (!micromatch.isMatch(str, patterns[i], options)) {
  279. return false;
  280. }
  281. }
  282. return true;
  283. };
  284. /**
  285. * Returns a list of strings that _**do not match any**_ of the given `patterns`.
  286. *
  287. * ```js
  288. * var mm = require('micromatch');
  289. * mm.not(list, patterns[, options]);
  290. *
  291. * console.log(mm.not(['a.a', 'b.b', 'c.c'], '*.a'));
  292. * //=> ['b.b', 'c.c']
  293. * ```
  294. * @param {Array} `list` Array of strings to match.
  295. * @param {String|Array} `patterns` One or more glob pattern to use for matching.
  296. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  297. * @return {Array} Returns an array of strings that **do not match** the given patterns.
  298. * @api public
  299. */
  300. micromatch.not = function(list, patterns, options) {
  301. var opts = extend({}, options);
  302. var ignore = opts.ignore;
  303. delete opts.ignore;
  304. var unixify = utils.unixify(opts);
  305. list = utils.arrayify(list).map(unixify);
  306. var matches = utils.diff(list, micromatch(list, patterns, opts));
  307. if (ignore) {
  308. matches = utils.diff(matches, micromatch(list, ignore));
  309. }
  310. return opts.nodupes !== false ? utils.unique(matches) : matches;
  311. };
  312. /**
  313. * Returns true if the given `string` contains the given pattern. Similar
  314. * to [.isMatch](#isMatch) but the pattern can match any part of the string.
  315. *
  316. * ```js
  317. * var mm = require('micromatch');
  318. * mm.contains(string, pattern[, options]);
  319. *
  320. * console.log(mm.contains('aa/bb/cc', '*b'));
  321. * //=> true
  322. * console.log(mm.contains('aa/bb/cc', '*d'));
  323. * //=> false
  324. * ```
  325. * @param {String} `str` The string to match.
  326. * @param {String|Array} `patterns` Glob pattern to use for matching.
  327. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  328. * @return {Boolean} Returns true if the patter matches any part of `str`.
  329. * @api public
  330. */
  331. micromatch.contains = function(str, patterns, options) {
  332. if (typeof str !== 'string') {
  333. throw new TypeError('expected a string: "' + util.inspect(str) + '"');
  334. }
  335. if (typeof patterns === 'string') {
  336. if (isEmptyString(str) || isEmptyString(patterns)) {
  337. return false;
  338. }
  339. var equals = utils.equalsPattern(patterns, options);
  340. if (equals(str)) {
  341. return true;
  342. }
  343. var contains = utils.containsPattern(patterns, options);
  344. if (contains(str)) {
  345. return true;
  346. }
  347. }
  348. var opts = extend({}, options, {contains: true});
  349. return micromatch.any(str, patterns, opts);
  350. };
  351. /**
  352. * Returns true if the given pattern and options should enable
  353. * the `matchBase` option.
  354. * @return {Boolean}
  355. * @api private
  356. */
  357. micromatch.matchBase = function(pattern, options) {
  358. if (pattern && pattern.indexOf('/') !== -1 || !options) return false;
  359. return options.basename === true || options.matchBase === true;
  360. };
  361. /**
  362. * Filter the keys of the given object with the given `glob` pattern
  363. * and `options`. Does not attempt to match nested keys. If you need this feature,
  364. * use [glob-object][] instead.
  365. *
  366. * ```js
  367. * var mm = require('micromatch');
  368. * mm.matchKeys(object, patterns[, options]);
  369. *
  370. * var obj = { aa: 'a', ab: 'b', ac: 'c' };
  371. * console.log(mm.matchKeys(obj, '*b'));
  372. * //=> { ab: 'b' }
  373. * ```
  374. * @param {Object} `object` The object with keys to filter.
  375. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  376. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  377. * @return {Object} Returns an object with only keys that match the given patterns.
  378. * @api public
  379. */
  380. micromatch.matchKeys = function(obj, patterns, options) {
  381. if (!utils.isObject(obj)) {
  382. throw new TypeError('expected the first argument to be an object');
  383. }
  384. var keys = micromatch(Object.keys(obj), patterns, options);
  385. return utils.pick(obj, keys);
  386. };
  387. /**
  388. * Returns a memoized matcher function from the given glob `pattern` and `options`.
  389. * The returned function takes a string to match as its only argument and returns
  390. * true if the string is a match.
  391. *
  392. * ```js
  393. * var mm = require('micromatch');
  394. * mm.matcher(pattern[, options]);
  395. *
  396. * var isMatch = mm.matcher('*.!(*a)');
  397. * console.log(isMatch('a.a'));
  398. * //=> false
  399. * console.log(isMatch('a.b'));
  400. * //=> true
  401. * ```
  402. * @param {String} `pattern` Glob pattern
  403. * @param {Object} `options` See available [options](#options) for changing how matches are performed.
  404. * @return {Function} Returns a matcher function.
  405. * @api public
  406. */
  407. micromatch.matcher = function matcher(pattern, options) {
  408. if (Array.isArray(pattern)) {
  409. return compose(pattern, options, matcher);
  410. }
  411. // if pattern is a regex
  412. if (pattern instanceof RegExp) {
  413. return test(pattern);
  414. }
  415. // if pattern is invalid
  416. if (!utils.isString(pattern)) {
  417. throw new TypeError('expected pattern to be an array, string or regex');
  418. }
  419. // if pattern is a non-glob string
  420. if (!utils.hasSpecialChars(pattern)) {
  421. if (options && options.nocase === true) {
  422. pattern = pattern.toLowerCase();
  423. }
  424. return utils.matchPath(pattern, options);
  425. }
  426. // if pattern is a glob string
  427. var re = micromatch.makeRe(pattern, options);
  428. // if `options.matchBase` or `options.basename` is defined
  429. if (micromatch.matchBase(pattern, options)) {
  430. return utils.matchBasename(re, options);
  431. }
  432. function test(regex) {
  433. var equals = utils.equalsPattern(options);
  434. var unixify = utils.unixify(options);
  435. return function(str) {
  436. if (equals(str)) {
  437. return true;
  438. }
  439. if (regex.test(unixify(str))) {
  440. return true;
  441. }
  442. return false;
  443. };
  444. }
  445. var fn = test(re);
  446. Object.defineProperty(fn, 'result', {
  447. configurable: true,
  448. enumerable: false,
  449. value: re.result
  450. });
  451. return fn;
  452. };
  453. /**
  454. * Returns an array of matches captured by `pattern` in `string, or `null` if the pattern did not match.
  455. *
  456. * ```js
  457. * var mm = require('micromatch');
  458. * mm.capture(pattern, string[, options]);
  459. *
  460. * console.log(mm.capture('test/*.js', 'test/foo.js'));
  461. * //=> ['foo']
  462. * console.log(mm.capture('test/*.js', 'foo/bar.css'));
  463. * //=> null
  464. * ```
  465. * @param {String} `pattern` Glob pattern to use for matching.
  466. * @param {String} `string` String to match
  467. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  468. * @return {Boolean} Returns an array of captures if the string matches the glob pattern, otherwise `null`.
  469. * @api public
  470. */
  471. micromatch.capture = function(pattern, str, options) {
  472. var re = micromatch.makeRe(pattern, extend({capture: true}, options));
  473. var unixify = utils.unixify(options);
  474. function match() {
  475. return function(string) {
  476. var match = re.exec(unixify(string));
  477. if (!match) {
  478. return null;
  479. }
  480. return match.slice(1);
  481. };
  482. }
  483. var capture = memoize('capture', pattern, options, match);
  484. return capture(str);
  485. };
  486. /**
  487. * Create a regular expression from the given glob `pattern`.
  488. *
  489. * ```js
  490. * var mm = require('micromatch');
  491. * mm.makeRe(pattern[, options]);
  492. *
  493. * console.log(mm.makeRe('*.js'));
  494. * //=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/
  495. * ```
  496. * @param {String} `pattern` A glob pattern to convert to regex.
  497. * @param {Object} `options` See available [options](#options) for changing how matches are performed.
  498. * @return {RegExp} Returns a regex created from the given pattern.
  499. * @api public
  500. */
  501. micromatch.makeRe = function(pattern, options) {
  502. if (typeof pattern !== 'string') {
  503. throw new TypeError('expected pattern to be a string');
  504. }
  505. if (pattern.length > MAX_LENGTH) {
  506. throw new Error('expected pattern to be less than ' + MAX_LENGTH + ' characters');
  507. }
  508. function makeRe() {
  509. var result = micromatch.create(pattern, options);
  510. var ast_array = [];
  511. var output = result.map(function(obj) {
  512. obj.ast.state = obj.state;
  513. ast_array.push(obj.ast);
  514. return obj.output;
  515. });
  516. var regex = toRegex(output.join('|'), options);
  517. Object.defineProperty(regex, 'result', {
  518. configurable: true,
  519. enumerable: false,
  520. value: ast_array
  521. });
  522. return regex;
  523. }
  524. return memoize('makeRe', pattern, options, makeRe);
  525. };
  526. /**
  527. * Expand the given brace `pattern`.
  528. *
  529. * ```js
  530. * var mm = require('micromatch');
  531. * console.log(mm.braces('foo/{a,b}/bar'));
  532. * //=> ['foo/(a|b)/bar']
  533. *
  534. * console.log(mm.braces('foo/{a,b}/bar', {expand: true}));
  535. * //=> ['foo/(a|b)/bar']
  536. * ```
  537. * @param {String} `pattern` String with brace pattern to expand.
  538. * @param {Object} `options` Any [options](#options) to change how expansion is performed. See the [braces][] library for all available options.
  539. * @return {Array}
  540. * @api public
  541. */
  542. micromatch.braces = function(pattern, options) {
  543. if (typeof pattern !== 'string' && !Array.isArray(pattern)) {
  544. throw new TypeError('expected pattern to be an array or string');
  545. }
  546. function expand() {
  547. if (options && options.nobrace === true || !/\{.*\}/.test(pattern)) {
  548. return utils.arrayify(pattern);
  549. }
  550. return braces(pattern, options);
  551. }
  552. return memoize('braces', pattern, options, expand);
  553. };
  554. /**
  555. * Proxy to the [micromatch.braces](#method), for parity with
  556. * minimatch.
  557. */
  558. micromatch.braceExpand = function(pattern, options) {
  559. var opts = extend({}, options, {expand: true});
  560. return micromatch.braces(pattern, opts);
  561. };
  562. /**
  563. * Parses the given glob `pattern` and returns an array of abstract syntax
  564. * trees (ASTs), with the compiled `output` and optional source `map` on
  565. * each AST.
  566. *
  567. * ```js
  568. * var mm = require('micromatch');
  569. * mm.create(pattern[, options]);
  570. *
  571. * console.log(mm.create('abc/*.js'));
  572. * // [{ options: { source: 'string', sourcemap: true },
  573. * // state: {},
  574. * // compilers:
  575. * // { ... },
  576. * // output: '(\\.[\\\\\\/])?abc\\/(?!\\.)(?=.)[^\\/]*?\\.js',
  577. * // ast:
  578. * // { type: 'root',
  579. * // errors: [],
  580. * // nodes:
  581. * // [ ... ],
  582. * // dot: false,
  583. * // input: 'abc/*.js' },
  584. * // parsingErrors: [],
  585. * // map:
  586. * // { version: 3,
  587. * // sources: [ 'string' ],
  588. * // names: [],
  589. * // mappings: 'AAAA,GAAG,EAAC,kBAAC,EAAC,EAAE',
  590. * // sourcesContent: [ 'abc/*.js' ] },
  591. * // position: { line: 1, column: 28 },
  592. * // content: {},
  593. * // files: {},
  594. * // idx: 6 }]
  595. * ```
  596. * @param {String} `pattern` Glob pattern to parse and compile.
  597. * @param {Object} `options` Any [options](#options) to change how parsing and compiling is performed.
  598. * @return {Object} Returns an object with the parsed AST, compiled string and optional source map.
  599. * @api public
  600. */
  601. micromatch.create = function(pattern, options) {
  602. return memoize('create', pattern, options, function() {
  603. function create(str, opts) {
  604. return micromatch.compile(micromatch.parse(str, opts), opts);
  605. }
  606. pattern = micromatch.braces(pattern, options);
  607. var len = pattern.length;
  608. var idx = -1;
  609. var res = [];
  610. while (++idx < len) {
  611. res.push(create(pattern[idx], options));
  612. }
  613. return res;
  614. });
  615. };
  616. /**
  617. * Parse the given `str` with the given `options`.
  618. *
  619. * ```js
  620. * var mm = require('micromatch');
  621. * mm.parse(pattern[, options]);
  622. *
  623. * var ast = mm.parse('a/{b,c}/d');
  624. * console.log(ast);
  625. * // { type: 'root',
  626. * // errors: [],
  627. * // input: 'a/{b,c}/d',
  628. * // nodes:
  629. * // [ { type: 'bos', val: '' },
  630. * // { type: 'text', val: 'a/' },
  631. * // { type: 'brace',
  632. * // nodes:
  633. * // [ { type: 'brace.open', val: '{' },
  634. * // { type: 'text', val: 'b,c' },
  635. * // { type: 'brace.close', val: '}' } ] },
  636. * // { type: 'text', val: '/d' },
  637. * // { type: 'eos', val: '' } ] }
  638. * ```
  639. * @param {String} `str`
  640. * @param {Object} `options`
  641. * @return {Object} Returns an AST
  642. * @api public
  643. */
  644. micromatch.parse = function(pattern, options) {
  645. if (typeof pattern !== 'string') {
  646. throw new TypeError('expected a string');
  647. }
  648. function parse() {
  649. var snapdragon = utils.instantiate(null, options);
  650. parsers(snapdragon, options);
  651. var ast = snapdragon.parse(pattern, options);
  652. utils.define(ast, 'snapdragon', snapdragon);
  653. ast.input = pattern;
  654. return ast;
  655. }
  656. return memoize('parse', pattern, options, parse);
  657. };
  658. /**
  659. * Compile the given `ast` or string with the given `options`.
  660. *
  661. * ```js
  662. * var mm = require('micromatch');
  663. * mm.compile(ast[, options]);
  664. *
  665. * var ast = mm.parse('a/{b,c}/d');
  666. * console.log(mm.compile(ast));
  667. * // { options: { source: 'string' },
  668. * // state: {},
  669. * // compilers:
  670. * // { eos: [Function],
  671. * // noop: [Function],
  672. * // bos: [Function],
  673. * // brace: [Function],
  674. * // 'brace.open': [Function],
  675. * // text: [Function],
  676. * // 'brace.close': [Function] },
  677. * // output: [ 'a/(b|c)/d' ],
  678. * // ast:
  679. * // { ... },
  680. * // parsingErrors: [] }
  681. * ```
  682. * @param {Object|String} `ast`
  683. * @param {Object} `options`
  684. * @return {Object} Returns an object that has an `output` property with the compiled string.
  685. * @api public
  686. */
  687. micromatch.compile = function(ast, options) {
  688. if (typeof ast === 'string') {
  689. ast = micromatch.parse(ast, options);
  690. }
  691. return memoize('compile', ast.input, options, function() {
  692. var snapdragon = utils.instantiate(ast, options);
  693. compilers(snapdragon, options);
  694. return snapdragon.compile(ast, options);
  695. });
  696. };
  697. /**
  698. * Clear the regex cache.
  699. *
  700. * ```js
  701. * mm.clearCache();
  702. * ```
  703. * @api public
  704. */
  705. micromatch.clearCache = function() {
  706. micromatch.cache.caches = {};
  707. };
  708. /**
  709. * Returns true if the given value is effectively an empty string
  710. */
  711. function isEmptyString(val) {
  712. return String(val) === '' || String(val) === './';
  713. }
  714. /**
  715. * Compose a matcher function with the given patterns.
  716. * This allows matcher functions to be compiled once and
  717. * called multiple times.
  718. */
  719. function compose(patterns, options, matcher) {
  720. var matchers;
  721. return memoize('compose', String(patterns), options, function() {
  722. return function(file) {
  723. // delay composition until it's invoked the first time,
  724. // after that it won't be called again
  725. if (!matchers) {
  726. matchers = [];
  727. for (var i = 0; i < patterns.length; i++) {
  728. matchers.push(matcher(patterns[i], options));
  729. }
  730. }
  731. var len = matchers.length;
  732. while (len--) {
  733. if (matchers[len](file) === true) {
  734. return true;
  735. }
  736. }
  737. return false;
  738. };
  739. });
  740. }
  741. /**
  742. * Memoize a generated regex or function. A unique key is generated
  743. * from the `type` (usually method name), the `pattern`, and
  744. * user-defined options.
  745. */
  746. function memoize(type, pattern, options, fn) {
  747. var key = utils.createKey(type + '=' + pattern, options);
  748. if (options && options.cache === false) {
  749. return fn(pattern, options);
  750. }
  751. if (cache.has(type, key)) {
  752. return cache.get(type, key);
  753. }
  754. var val = fn(pattern, options);
  755. cache.set(type, key, val);
  756. return val;
  757. }
  758. /**
  759. * Expose compiler, parser and cache on `micromatch`
  760. */
  761. micromatch.compilers = compilers;
  762. micromatch.parsers = parsers;
  763. micromatch.caches = cache.caches;
  764. /**
  765. * Expose `micromatch`
  766. * @type {Function}
  767. */
  768. module.exports = micromatch;