Нет описания

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. 'use strict';
  2. /**
  3. * Module dependencies
  4. */
  5. var extend = require('extend-shallow');
  6. var unique = require('array-unique');
  7. var toRegex = require('to-regex');
  8. /**
  9. * Local dependencies
  10. */
  11. var compilers = require('./lib/compilers');
  12. var parsers = require('./lib/parsers');
  13. var Extglob = require('./lib/extglob');
  14. var utils = require('./lib/utils');
  15. var MAX_LENGTH = 1024 * 64;
  16. /**
  17. * Convert the given `extglob` pattern into a regex-compatible string. Returns
  18. * an object with the compiled result and the parsed AST.
  19. *
  20. * ```js
  21. * var extglob = require('extglob');
  22. * console.log(extglob('*.!(*a)'));
  23. * //=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
  24. * ```
  25. * @param {String} `pattern`
  26. * @param {Object} `options`
  27. * @return {String}
  28. * @api public
  29. */
  30. function extglob(pattern, options) {
  31. return extglob.create(pattern, options).output;
  32. }
  33. /**
  34. * Takes an array of strings and an extglob pattern and returns a new
  35. * array that contains only the strings that match the pattern.
  36. *
  37. * ```js
  38. * var extglob = require('extglob');
  39. * console.log(extglob.match(['a.a', 'a.b', 'a.c'], '*.!(*a)'));
  40. * //=> ['a.b', 'a.c']
  41. * ```
  42. * @param {Array} `list` Array of strings to match
  43. * @param {String} `pattern` Extglob pattern
  44. * @param {Object} `options`
  45. * @return {Array} Returns an array of matches
  46. * @api public
  47. */
  48. extglob.match = function(list, pattern, options) {
  49. if (typeof pattern !== 'string') {
  50. throw new TypeError('expected pattern to be a string');
  51. }
  52. list = utils.arrayify(list);
  53. var isMatch = extglob.matcher(pattern, options);
  54. var len = list.length;
  55. var idx = -1;
  56. var matches = [];
  57. while (++idx < len) {
  58. var ele = list[idx];
  59. if (isMatch(ele)) {
  60. matches.push(ele);
  61. }
  62. }
  63. // if no options were passed, uniquify results and return
  64. if (typeof options === 'undefined') {
  65. return unique(matches);
  66. }
  67. if (matches.length === 0) {
  68. if (options.failglob === true) {
  69. throw new Error('no matches found for "' + pattern + '"');
  70. }
  71. if (options.nonull === true || options.nullglob === true) {
  72. return [pattern.split('\\').join('')];
  73. }
  74. }
  75. return options.nodupes !== false ? unique(matches) : matches;
  76. };
  77. /**
  78. * Returns true if the specified `string` matches the given
  79. * extglob `pattern`.
  80. *
  81. * ```js
  82. * var extglob = require('extglob');
  83. *
  84. * console.log(extglob.isMatch('a.a', '*.!(*a)'));
  85. * //=> false
  86. * console.log(extglob.isMatch('a.b', '*.!(*a)'));
  87. * //=> true
  88. * ```
  89. * @param {String} `string` String to match
  90. * @param {String} `pattern` Extglob pattern
  91. * @param {String} `options`
  92. * @return {Boolean}
  93. * @api public
  94. */
  95. extglob.isMatch = function(str, pattern, options) {
  96. if (typeof pattern !== 'string') {
  97. throw new TypeError('expected pattern to be a string');
  98. }
  99. if (typeof str !== 'string') {
  100. throw new TypeError('expected a string');
  101. }
  102. if (pattern === str) {
  103. return true;
  104. }
  105. if (pattern === '' || pattern === ' ' || pattern === '.') {
  106. return pattern === str;
  107. }
  108. var isMatch = utils.memoize('isMatch', pattern, options, extglob.matcher);
  109. return isMatch(str);
  110. };
  111. /**
  112. * Returns true if the given `string` contains the given pattern. Similar to `.isMatch` but
  113. * the pattern can match any part of the string.
  114. *
  115. * ```js
  116. * var extglob = require('extglob');
  117. * console.log(extglob.contains('aa/bb/cc', '*b'));
  118. * //=> true
  119. * console.log(extglob.contains('aa/bb/cc', '*d'));
  120. * //=> false
  121. * ```
  122. * @param {String} `str` The string to match.
  123. * @param {String} `pattern` Glob pattern to use for matching.
  124. * @param {Object} `options`
  125. * @return {Boolean} Returns true if the patter matches any part of `str`.
  126. * @api public
  127. */
  128. extglob.contains = function(str, pattern, options) {
  129. if (typeof str !== 'string') {
  130. throw new TypeError('expected a string');
  131. }
  132. if (pattern === '' || pattern === ' ' || pattern === '.') {
  133. return pattern === str;
  134. }
  135. var opts = extend({}, options, {contains: true});
  136. opts.strictClose = false;
  137. opts.strictOpen = false;
  138. return extglob.isMatch(str, pattern, opts);
  139. };
  140. /**
  141. * Takes an extglob pattern and returns a matcher function. The returned
  142. * function takes the string to match as its only argument.
  143. *
  144. * ```js
  145. * var extglob = require('extglob');
  146. * var isMatch = extglob.matcher('*.!(*a)');
  147. *
  148. * console.log(isMatch('a.a'));
  149. * //=> false
  150. * console.log(isMatch('a.b'));
  151. * //=> true
  152. * ```
  153. * @param {String} `pattern` Extglob pattern
  154. * @param {String} `options`
  155. * @return {Boolean}
  156. * @api public
  157. */
  158. extglob.matcher = function(pattern, options) {
  159. if (typeof pattern !== 'string') {
  160. throw new TypeError('expected pattern to be a string');
  161. }
  162. function matcher() {
  163. var re = extglob.makeRe(pattern, options);
  164. return function(str) {
  165. return re.test(str);
  166. };
  167. }
  168. return utils.memoize('matcher', pattern, options, matcher);
  169. };
  170. /**
  171. * Convert the given `extglob` pattern into a regex-compatible string. Returns
  172. * an object with the compiled result and the parsed AST.
  173. *
  174. * ```js
  175. * var extglob = require('extglob');
  176. * console.log(extglob.create('*.!(*a)').output);
  177. * //=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
  178. * ```
  179. * @param {String} `str`
  180. * @param {Object} `options`
  181. * @return {String}
  182. * @api public
  183. */
  184. extglob.create = function(pattern, options) {
  185. if (typeof pattern !== 'string') {
  186. throw new TypeError('expected pattern to be a string');
  187. }
  188. function create() {
  189. var ext = new Extglob(options);
  190. var ast = ext.parse(pattern, options);
  191. return ext.compile(ast, options);
  192. }
  193. return utils.memoize('create', pattern, options, create);
  194. };
  195. /**
  196. * Returns an array of matches captured by `pattern` in `string`, or `null`
  197. * if the pattern did not match.
  198. *
  199. * ```js
  200. * var extglob = require('extglob');
  201. * extglob.capture(pattern, string[, options]);
  202. *
  203. * console.log(extglob.capture('test/*.js', 'test/foo.js'));
  204. * //=> ['foo']
  205. * console.log(extglob.capture('test/*.js', 'foo/bar.css'));
  206. * //=> null
  207. * ```
  208. * @param {String} `pattern` Glob pattern to use for matching.
  209. * @param {String} `string` String to match
  210. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  211. * @return {Boolean} Returns an array of captures if the string matches the glob pattern, otherwise `null`.
  212. * @api public
  213. */
  214. extglob.capture = function(pattern, str, options) {
  215. var re = extglob.makeRe(pattern, extend({capture: true}, options));
  216. function match() {
  217. return function(string) {
  218. var match = re.exec(string);
  219. if (!match) {
  220. return null;
  221. }
  222. return match.slice(1);
  223. };
  224. }
  225. var capture = utils.memoize('capture', pattern, options, match);
  226. return capture(str);
  227. };
  228. /**
  229. * Create a regular expression from the given `pattern` and `options`.
  230. *
  231. * ```js
  232. * var extglob = require('extglob');
  233. * var re = extglob.makeRe('*.!(*a)');
  234. * console.log(re);
  235. * //=> /^[^\/]*?\.(?![^\/]*?a)[^\/]*?$/
  236. * ```
  237. * @param {String} `pattern` The pattern to convert to regex.
  238. * @param {Object} `options`
  239. * @return {RegExp}
  240. * @api public
  241. */
  242. extglob.makeRe = function(pattern, options) {
  243. if (pattern instanceof RegExp) {
  244. return pattern;
  245. }
  246. if (typeof pattern !== 'string') {
  247. throw new TypeError('expected pattern to be a string');
  248. }
  249. if (pattern.length > MAX_LENGTH) {
  250. throw new Error('expected pattern to be less than ' + MAX_LENGTH + ' characters');
  251. }
  252. function makeRe() {
  253. var opts = extend({strictErrors: false}, options);
  254. if (opts.strictErrors === true) opts.strict = true;
  255. var res = extglob.create(pattern, opts);
  256. return toRegex(res.output, opts);
  257. }
  258. var regex = utils.memoize('makeRe', pattern, options, makeRe);
  259. if (regex.source.length > MAX_LENGTH) {
  260. throw new SyntaxError('potentially malicious regex detected');
  261. }
  262. return regex;
  263. };
  264. /**
  265. * Cache
  266. */
  267. extglob.cache = utils.cache;
  268. extglob.clearCache = function() {
  269. extglob.cache.__data__ = {};
  270. };
  271. /**
  272. * Expose `Extglob` constructor, parsers and compilers
  273. */
  274. extglob.Extglob = Extglob;
  275. extglob.compilers = compilers;
  276. extglob.parsers = parsers;
  277. /**
  278. * Expose `extglob`
  279. * @type {Function}
  280. */
  281. module.exports = extglob;