説明なし

index.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. 'use strict';
  2. /**
  3. * Module dependencies
  4. */
  5. var toRegex = require('to-regex');
  6. var unique = require('array-unique');
  7. var extend = require('extend-shallow');
  8. var define = require('define-property');
  9. /**
  10. * Local dependencies
  11. */
  12. var compilers = require('./lib/compilers');
  13. var parsers = require('./lib/parsers');
  14. var Braces = require('./lib/braces');
  15. var utils = require('./lib/utils');
  16. var MAX_LENGTH = 1024 * 64;
  17. var cache = {};
  18. /**
  19. * Convert the given `braces` pattern into a regex-compatible string. By default, only one string is generated for every input string. Set `options.expand` to true to return an array of patterns (similar to Bash or minimatch. Before using `options.expand`, it's recommended that you read the [performance notes](#performance)).
  20. *
  21. * ```js
  22. * var braces = require('braces');
  23. * console.log(braces('{a,b,c}'));
  24. * //=> ['(a|b|c)']
  25. *
  26. * console.log(braces('{a,b,c}', {expand: true}));
  27. * //=> ['a', 'b', 'c']
  28. * ```
  29. * @param {String} `str`
  30. * @param {Object} `options`
  31. * @return {String}
  32. * @api public
  33. */
  34. function braces(pattern, options) {
  35. var key = utils.createKey(String(pattern), options);
  36. var arr = [];
  37. var disabled = options && options.cache === false;
  38. if (!disabled && cache.hasOwnProperty(key)) {
  39. return cache[key];
  40. }
  41. if (Array.isArray(pattern)) {
  42. for (var i = 0; i < pattern.length; i++) {
  43. arr.push.apply(arr, braces.create(pattern[i], options));
  44. }
  45. } else {
  46. arr = braces.create(pattern, options);
  47. }
  48. if (options && options.nodupes === true) {
  49. arr = unique(arr);
  50. }
  51. if (!disabled) {
  52. cache[key] = arr;
  53. }
  54. return arr;
  55. }
  56. /**
  57. * Expands a brace pattern into an array. This method is called by the main [braces](#braces) function when `options.expand` is true. Before using this method it's recommended that you read the [performance notes](#performance)) and advantages of using [.optimize](#optimize) instead.
  58. *
  59. * ```js
  60. * var braces = require('braces');
  61. * console.log(braces.expand('a/{b,c}/d'));
  62. * //=> ['a/b/d', 'a/c/d'];
  63. * ```
  64. * @param {String} `pattern` Brace pattern
  65. * @param {Object} `options`
  66. * @return {Array} Returns an array of expanded values.
  67. * @api public
  68. */
  69. braces.expand = function(pattern, options) {
  70. return braces.create(pattern, extend({}, options, {expand: true}));
  71. };
  72. /**
  73. * Expands a brace pattern into a regex-compatible, optimized string. This method is called by the main [braces](#braces) function by default.
  74. *
  75. * ```js
  76. * var braces = require('braces');
  77. * console.log(braces.expand('a/{b,c}/d'));
  78. * //=> ['a/(b|c)/d']
  79. * ```
  80. * @param {String} `pattern` Brace pattern
  81. * @param {Object} `options`
  82. * @return {Array} Returns an array of expanded values.
  83. * @api public
  84. */
  85. braces.optimize = function(pattern, options) {
  86. return braces.create(pattern, options);
  87. };
  88. /**
  89. * Processes a brace pattern and returns either an expanded array (if `options.expand` is true), a highly optimized regex-compatible string. This method is called by the main [braces](#braces) function.
  90. *
  91. * ```js
  92. * var braces = require('braces');
  93. * console.log(braces.create('user-{200..300}/project-{a,b,c}-{1..10}'))
  94. * //=> 'user-(20[0-9]|2[1-9][0-9]|300)/project-(a|b|c)-([1-9]|10)'
  95. * ```
  96. * @param {String} `pattern` Brace pattern
  97. * @param {Object} `options`
  98. * @return {Array} Returns an array of expanded values.
  99. * @api public
  100. */
  101. braces.create = function(pattern, options) {
  102. if (typeof pattern !== 'string') {
  103. throw new TypeError('expected a string');
  104. }
  105. if (pattern.length >= MAX_LENGTH) {
  106. throw new Error('expected pattern to be less than ' + MAX_LENGTH + ' characters');
  107. }
  108. function create() {
  109. if (pattern === '' || pattern.length < 3) {
  110. return [pattern];
  111. }
  112. if (utils.isEmptySets(pattern)) {
  113. return [];
  114. }
  115. if (utils.isQuotedString(pattern)) {
  116. return [pattern.slice(1, -1)];
  117. }
  118. var proto = new Braces(options);
  119. var result = !options || options.expand !== true
  120. ? proto.optimize(pattern, options)
  121. : proto.expand(pattern, options);
  122. // get the generated pattern(s)
  123. var arr = result.output;
  124. // filter out empty strings if specified
  125. if (options && options.noempty === true) {
  126. arr = arr.filter(Boolean);
  127. }
  128. // filter out duplicates if specified
  129. if (options && options.nodupes === true) {
  130. arr = unique(arr);
  131. }
  132. define(arr, 'result', result);
  133. return arr;
  134. }
  135. return memoize('create', pattern, options, create);
  136. };
  137. /**
  138. * Create a regular expression from the given string `pattern`.
  139. *
  140. * ```js
  141. * var braces = require('braces');
  142. *
  143. * console.log(braces.makeRe('id-{200..300}'));
  144. * //=> /^(?:id-(20[0-9]|2[1-9][0-9]|300))$/
  145. * ```
  146. * @param {String} `pattern` The pattern to convert to regex.
  147. * @param {Object} `options`
  148. * @return {RegExp}
  149. * @api public
  150. */
  151. braces.makeRe = function(pattern, options) {
  152. if (typeof pattern !== 'string') {
  153. throw new TypeError('expected a string');
  154. }
  155. if (pattern.length >= MAX_LENGTH) {
  156. throw new Error('expected pattern to be less than ' + MAX_LENGTH + ' characters');
  157. }
  158. function makeRe() {
  159. var arr = braces(pattern, options);
  160. var opts = extend({strictErrors: false}, options);
  161. return toRegex(arr, opts);
  162. }
  163. return memoize('makeRe', pattern, options, makeRe);
  164. };
  165. /**
  166. * Parse the given `str` with the given `options`.
  167. *
  168. * ```js
  169. * var braces = require('braces');
  170. * var ast = braces.parse('a/{b,c}/d');
  171. * console.log(ast);
  172. * // { type: 'root',
  173. * // errors: [],
  174. * // input: 'a/{b,c}/d',
  175. * // nodes:
  176. * // [ { type: 'bos', val: '' },
  177. * // { type: 'text', val: 'a/' },
  178. * // { type: 'brace',
  179. * // nodes:
  180. * // [ { type: 'brace.open', val: '{' },
  181. * // { type: 'text', val: 'b,c' },
  182. * // { type: 'brace.close', val: '}' } ] },
  183. * // { type: 'text', val: '/d' },
  184. * // { type: 'eos', val: '' } ] }
  185. * ```
  186. * @param {String} `pattern` Brace pattern to parse
  187. * @param {Object} `options`
  188. * @return {Object} Returns an AST
  189. * @api public
  190. */
  191. braces.parse = function(pattern, options) {
  192. var proto = new Braces(options);
  193. return proto.parse(pattern, options);
  194. };
  195. /**
  196. * Compile the given `ast` or string with the given `options`.
  197. *
  198. * ```js
  199. * var braces = require('braces');
  200. * var ast = braces.parse('a/{b,c}/d');
  201. * console.log(braces.compile(ast));
  202. * // { options: { source: 'string' },
  203. * // state: {},
  204. * // compilers:
  205. * // { eos: [Function],
  206. * // noop: [Function],
  207. * // bos: [Function],
  208. * // brace: [Function],
  209. * // 'brace.open': [Function],
  210. * // text: [Function],
  211. * // 'brace.close': [Function] },
  212. * // output: [ 'a/(b|c)/d' ],
  213. * // ast:
  214. * // { ... },
  215. * // parsingErrors: [] }
  216. * ```
  217. * @param {Object|String} `ast` AST from [.parse](#parse). If a string is passed it will be parsed first.
  218. * @param {Object} `options`
  219. * @return {Object} Returns an object that has an `output` property with the compiled string.
  220. * @api public
  221. */
  222. braces.compile = function(ast, options) {
  223. var proto = new Braces(options);
  224. return proto.compile(ast, options);
  225. };
  226. /**
  227. * Clear the regex cache.
  228. *
  229. * ```js
  230. * braces.clearCache();
  231. * ```
  232. * @api public
  233. */
  234. braces.clearCache = function() {
  235. cache = braces.cache = {};
  236. };
  237. /**
  238. * Memoize a generated regex or function. A unique key is generated
  239. * from the method name, pattern, and user-defined options. Set
  240. * options.memoize to false to disable.
  241. */
  242. function memoize(type, pattern, options, fn) {
  243. var key = utils.createKey(type + ':' + pattern, options);
  244. var disabled = options && options.cache === false;
  245. if (disabled) {
  246. braces.clearCache();
  247. return fn(pattern, options);
  248. }
  249. if (cache.hasOwnProperty(key)) {
  250. return cache[key];
  251. }
  252. var res = fn(pattern, options);
  253. cache[key] = res;
  254. return res;
  255. }
  256. /**
  257. * Expose `Braces` constructor and methods
  258. * @type {Function}
  259. */
  260. braces.Braces = Braces;
  261. braces.compilers = compilers;
  262. braces.parsers = parsers;
  263. braces.cache = cache;
  264. /**
  265. * Expose `braces`
  266. * @type {Function}
  267. */
  268. module.exports = braces;