picomatch.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. 'use strict';
  2. const scan = require('./scan');
  3. const parse = require('./parse');
  4. const utils = require('./utils');
  5. const constants = require('./constants');
  6. const isObject = val => val && typeof val === 'object' && !Array.isArray(val);
  7. /**
  8. * Creates a matcher function from one or more glob patterns. The
  9. * returned function takes a string to match as its first argument,
  10. * and returns true if the string is a match. The returned matcher
  11. * function also takes a boolean as the second argument that, when true,
  12. * returns an object with additional information.
  13. *
  14. * ```js
  15. * const picomatch = require('picomatch');
  16. * // picomatch(glob[, options]);
  17. *
  18. * const isMatch = picomatch('*.!(*a)');
  19. * console.log(isMatch('a.a')); //=> false
  20. * console.log(isMatch('a.b')); //=> true
  21. * ```
  22. * @name picomatch
  23. * @param {String|Array} `globs` One or more glob patterns.
  24. * @param {Object=} `options`
  25. * @return {Function=} Returns a matcher function.
  26. * @api public
  27. */
  28. const picomatch = (glob, options, returnState = false) => {
  29. if (Array.isArray(glob)) {
  30. const fns = glob.map(input => picomatch(input, options, returnState));
  31. const arrayMatcher = str => {
  32. for (const isMatch of fns) {
  33. const state = isMatch(str);
  34. if (state) return state;
  35. }
  36. return false;
  37. };
  38. return arrayMatcher;
  39. }
  40. const isState = isObject(glob) && glob.tokens && glob.input;
  41. if (glob === '' || (typeof glob !== 'string' && !isState)) {
  42. throw new TypeError('Expected pattern to be a non-empty string');
  43. }
  44. const opts = options || {};
  45. const posix = opts.windows;
  46. const regex = isState
  47. ? picomatch.compileRe(glob, options)
  48. : picomatch.makeRe(glob, options, false, true);
  49. const state = regex.state;
  50. delete regex.state;
  51. let isIgnored = () => false;
  52. if (opts.ignore) {
  53. const ignoreOpts = { ...options, ignore: null, onMatch: null, onResult: null };
  54. isIgnored = picomatch(opts.ignore, ignoreOpts, returnState);
  55. }
  56. const matcher = (input, returnObject = false) => {
  57. const { isMatch, match, output } = picomatch.test(input, regex, options, { glob, posix });
  58. const result = { glob, state, regex, posix, input, output, match, isMatch };
  59. if (typeof opts.onResult === 'function') {
  60. opts.onResult(result);
  61. }
  62. if (isMatch === false) {
  63. result.isMatch = false;
  64. return returnObject ? result : false;
  65. }
  66. if (isIgnored(input)) {
  67. if (typeof opts.onIgnore === 'function') {
  68. opts.onIgnore(result);
  69. }
  70. result.isMatch = false;
  71. return returnObject ? result : false;
  72. }
  73. if (typeof opts.onMatch === 'function') {
  74. opts.onMatch(result);
  75. }
  76. return returnObject ? result : true;
  77. };
  78. if (returnState) {
  79. matcher.state = state;
  80. }
  81. return matcher;
  82. };
  83. /**
  84. * Test `input` with the given `regex`. This is used by the main
  85. * `picomatch()` function to test the input string.
  86. *
  87. * ```js
  88. * const picomatch = require('picomatch');
  89. * // picomatch.test(input, regex[, options]);
  90. *
  91. * console.log(picomatch.test('foo/bar', /^(?:([^/]*?)\/([^/]*?))$/));
  92. * // { isMatch: true, match: [ 'foo/', 'foo', 'bar' ], output: 'foo/bar' }
  93. * ```
  94. * @param {String} `input` String to test.
  95. * @param {RegExp} `regex`
  96. * @return {Object} Returns an object with matching info.
  97. * @api public
  98. */
  99. picomatch.test = (input, regex, options, { glob, posix } = {}) => {
  100. if (typeof input !== 'string') {
  101. throw new TypeError('Expected input to be a string');
  102. }
  103. if (input === '') {
  104. return { isMatch: false, output: '' };
  105. }
  106. const opts = options || {};
  107. const format = opts.format || (posix ? utils.toPosixSlashes : null);
  108. let match = input === glob;
  109. let output = (match && format) ? format(input) : input;
  110. if (match === false) {
  111. output = format ? format(input) : input;
  112. match = output === glob;
  113. }
  114. if (match === false || opts.capture === true) {
  115. if (opts.matchBase === true || opts.basename === true) {
  116. match = picomatch.matchBase(input, regex, options, posix);
  117. } else {
  118. match = regex.exec(output);
  119. }
  120. }
  121. return { isMatch: Boolean(match), match, output };
  122. };
  123. /**
  124. * Match the basename of a filepath.
  125. *
  126. * ```js
  127. * const picomatch = require('picomatch');
  128. * // picomatch.matchBase(input, glob[, options]);
  129. * console.log(picomatch.matchBase('foo/bar.js', '*.js'); // true
  130. * ```
  131. * @param {String} `input` String to test.
  132. * @param {RegExp|String} `glob` Glob pattern or regex created by [.makeRe](#makeRe).
  133. * @return {Boolean}
  134. * @api public
  135. */
  136. picomatch.matchBase = (input, glob, options) => {
  137. const regex = glob instanceof RegExp ? glob : picomatch.makeRe(glob, options);
  138. return regex.test(utils.basename(input));
  139. };
  140. /**
  141. * Returns true if **any** of the given glob `patterns` match the specified `string`.
  142. *
  143. * ```js
  144. * const picomatch = require('picomatch');
  145. * // picomatch.isMatch(string, patterns[, options]);
  146. *
  147. * console.log(picomatch.isMatch('a.a', ['b.*', '*.a'])); //=> true
  148. * console.log(picomatch.isMatch('a.a', 'b.*')); //=> false
  149. * ```
  150. * @param {String|Array} str The string to test.
  151. * @param {String|Array} patterns One or more glob patterns to use for matching.
  152. * @param {Object} [options] See available [options](#options).
  153. * @return {Boolean} Returns true if any patterns match `str`
  154. * @api public
  155. */
  156. picomatch.isMatch = (str, patterns, options) => picomatch(patterns, options)(str);
  157. /**
  158. * Parse a glob pattern to create the source string for a regular
  159. * expression.
  160. *
  161. * ```js
  162. * const picomatch = require('picomatch');
  163. * const result = picomatch.parse(pattern[, options]);
  164. * ```
  165. * @param {String} `pattern`
  166. * @param {Object} `options`
  167. * @return {Object} Returns an object with useful properties and output to be used as a regex source string.
  168. * @api public
  169. */
  170. picomatch.parse = (pattern, options) => {
  171. if (Array.isArray(pattern)) return pattern.map(p => picomatch.parse(p, options));
  172. return parse(pattern, { ...options, fastpaths: false });
  173. };
  174. /**
  175. * Scan a glob pattern to separate the pattern into segments.
  176. *
  177. * ```js
  178. * const picomatch = require('picomatch');
  179. * // picomatch.scan(input[, options]);
  180. *
  181. * const result = picomatch.scan('!./foo/*.js');
  182. * console.log(result);
  183. * { prefix: '!./',
  184. * input: '!./foo/*.js',
  185. * start: 3,
  186. * base: 'foo',
  187. * glob: '*.js',
  188. * isBrace: false,
  189. * isBracket: false,
  190. * isGlob: true,
  191. * isExtglob: false,
  192. * isGlobstar: false,
  193. * negated: true }
  194. * ```
  195. * @param {String} `input` Glob pattern to scan.
  196. * @param {Object} `options`
  197. * @return {Object} Returns an object with
  198. * @api public
  199. */
  200. picomatch.scan = (input, options) => scan(input, options);
  201. /**
  202. * Compile a regular expression from the `state` object returned by the
  203. * [parse()](#parse) method.
  204. *
  205. * @param {Object} `state`
  206. * @param {Object} `options`
  207. * @param {Boolean} `returnOutput` Intended for implementors, this argument allows you to return the raw output from the parser.
  208. * @param {Boolean} `returnState` Adds the state to a `state` property on the returned regex. Useful for implementors and debugging.
  209. * @return {RegExp}
  210. * @api public
  211. */
  212. picomatch.compileRe = (state, options, returnOutput = false, returnState = false) => {
  213. if (returnOutput === true) {
  214. return state.output;
  215. }
  216. const opts = options || {};
  217. const prepend = opts.contains ? '' : '^';
  218. const append = opts.contains ? '' : '$';
  219. let source = `${prepend}(?:${state.output})${append}`;
  220. if (state && state.negated === true) {
  221. source = `^(?!${source}).*$`;
  222. }
  223. const regex = picomatch.toRegex(source, options);
  224. if (returnState === true) {
  225. regex.state = state;
  226. }
  227. return regex;
  228. };
  229. /**
  230. * Create a regular expression from a parsed glob pattern.
  231. *
  232. * ```js
  233. * const picomatch = require('picomatch');
  234. * const state = picomatch.parse('*.js');
  235. * // picomatch.compileRe(state[, options]);
  236. *
  237. * console.log(picomatch.compileRe(state));
  238. * //=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/
  239. * ```
  240. * @param {String} `state` The object returned from the `.parse` method.
  241. * @param {Object} `options`
  242. * @param {Boolean} `returnOutput` Implementors may use this argument to return the compiled output, instead of a regular expression. This is not exposed on the options to prevent end-users from mutating the result.
  243. * @param {Boolean} `returnState` Implementors may use this argument to return the state from the parsed glob with the returned regular expression.
  244. * @return {RegExp} Returns a regex created from the given pattern.
  245. * @api public
  246. */
  247. picomatch.makeRe = (input, options = {}, returnOutput = false, returnState = false) => {
  248. if (!input || typeof input !== 'string') {
  249. throw new TypeError('Expected a non-empty string');
  250. }
  251. let parsed = { negated: false, fastpaths: true };
  252. if (options.fastpaths !== false && (input[0] === '.' || input[0] === '*')) {
  253. parsed.output = parse.fastpaths(input, options);
  254. }
  255. if (!parsed.output) {
  256. parsed = parse(input, options);
  257. }
  258. return picomatch.compileRe(parsed, options, returnOutput, returnState);
  259. };
  260. /**
  261. * Create a regular expression from the given regex source string.
  262. *
  263. * ```js
  264. * const picomatch = require('picomatch');
  265. * // picomatch.toRegex(source[, options]);
  266. *
  267. * const { output } = picomatch.parse('*.js');
  268. * console.log(picomatch.toRegex(output));
  269. * //=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/
  270. * ```
  271. * @param {String} `source` Regular expression source string.
  272. * @param {Object} `options`
  273. * @return {RegExp}
  274. * @api public
  275. */
  276. picomatch.toRegex = (source, options) => {
  277. try {
  278. const opts = options || {};
  279. return new RegExp(source, opts.flags || (opts.nocase ? 'i' : ''));
  280. } catch (err) {
  281. if (options && options.debug === true) throw err;
  282. return /$^/;
  283. }
  284. };
  285. /**
  286. * Picomatch constants.
  287. * @return {Object}
  288. */
  289. picomatch.constants = constants;
  290. /**
  291. * Expose "picomatch"
  292. */
  293. module.exports = picomatch;