7 var toRegex = require('to-regex');
8 var unique = require('array-unique');
9 var extend = require('extend-shallow');
15 var compilers = require('./lib/compilers');
16 var parsers = require('./lib/parsers');
17 var Braces = require('./lib/braces');
18 var utils = require('./lib/utils');
19 var MAX_LENGTH = 1024 * 64;
23 * 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)).
26 * var braces = require('braces');
27 * console.log(braces('{a,b,c}'));
30 * console.log(braces('{a,b,c}', {expand: true}));
31 * //=> ['a', 'b', 'c']
33 * @param {String} `str`
34 * @param {Object} `options`
39 function braces(pattern, options) {
40 var key = utils.createKey(String(pattern), options);
43 var disabled = options && options.cache === false;
44 if (!disabled && cache.hasOwnProperty(key)) {
48 if (Array.isArray(pattern)) {
49 for (var i = 0; i < pattern.length; i++) {
50 arr.push.apply(arr, braces.create(pattern[i], options));
53 arr = braces.create(pattern, options);
56 if (options && options.nodupes === true) {
67 * 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.
70 * var braces = require('braces');
71 * console.log(braces.expand('a/{b,c}/d'));
72 * //=> ['a/b/d', 'a/c/d'];
74 * @param {String} `pattern` Brace pattern
75 * @param {Object} `options`
76 * @return {Array} Returns an array of expanded values.
80 braces.expand = function(pattern, options) {
81 return braces.create(pattern, extend({}, options, {expand: true}));
85 * Expands a brace pattern into a regex-compatible, optimized string. This method is called by the main [braces](#braces) function by default.
88 * var braces = require('braces');
89 * console.log(braces.expand('a/{b,c}/d'));
92 * @param {String} `pattern` Brace pattern
93 * @param {Object} `options`
94 * @return {Array} Returns an array of expanded values.
98 braces.optimize = function(pattern, options) {
99 return braces.create(pattern, options);
103 * 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.
106 * var braces = require('braces');
107 * console.log(braces.create('user-{200..300}/project-{a,b,c}-{1..10}'))
108 * //=> 'user-(20[0-9]|2[1-9][0-9]|300)/project-(a|b|c)-([1-9]|10)'
110 * @param {String} `pattern` Brace pattern
111 * @param {Object} `options`
112 * @return {Array} Returns an array of expanded values.
116 braces.create = function(pattern, options) {
117 if (typeof pattern !== 'string') {
118 throw new TypeError('expected a string');
121 var maxLength = (options && options.maxLength) || MAX_LENGTH;
122 if (pattern.length >= maxLength) {
123 throw new Error('expected pattern to be less than ' + maxLength + ' characters');
127 if (pattern === '' || pattern.length < 3) {
131 if (utils.isEmptySets(pattern)) {
135 if (utils.isQuotedString(pattern)) {
136 return [pattern.slice(1, -1)];
139 var proto = new Braces(options);
140 var result = !options || options.expand !== true
141 ? proto.optimize(pattern, options)
142 : proto.expand(pattern, options);
144 // get the generated pattern(s)
145 var arr = result.output;
147 // filter out empty strings if specified
148 if (options && options.noempty === true) {
149 arr = arr.filter(Boolean);
152 // filter out duplicates if specified
153 if (options && options.nodupes === true) {
157 Object.defineProperty(arr, 'result', {
165 return memoize('create', pattern, options, create);
169 * Create a regular expression from the given string `pattern`.
172 * var braces = require('braces');
174 * console.log(braces.makeRe('id-{200..300}'));
175 * //=> /^(?:id-(20[0-9]|2[1-9][0-9]|300))$/
177 * @param {String} `pattern` The pattern to convert to regex.
178 * @param {Object} `options`
183 braces.makeRe = function(pattern, options) {
184 if (typeof pattern !== 'string') {
185 throw new TypeError('expected a string');
188 var maxLength = (options && options.maxLength) || MAX_LENGTH;
189 if (pattern.length >= maxLength) {
190 throw new Error('expected pattern to be less than ' + maxLength + ' characters');
194 var arr = braces(pattern, options);
195 var opts = extend({strictErrors: false}, options);
196 return toRegex(arr, opts);
199 return memoize('makeRe', pattern, options, makeRe);
203 * Parse the given `str` with the given `options`.
206 * var braces = require('braces');
207 * var ast = braces.parse('a/{b,c}/d');
211 * // input: 'a/{b,c}/d',
213 * // [ { type: 'bos', val: '' },
214 * // { type: 'text', val: 'a/' },
215 * // { type: 'brace',
217 * // [ { type: 'brace.open', val: '{' },
218 * // { type: 'text', val: 'b,c' },
219 * // { type: 'brace.close', val: '}' } ] },
220 * // { type: 'text', val: '/d' },
221 * // { type: 'eos', val: '' } ] }
223 * @param {String} `pattern` Brace pattern to parse
224 * @param {Object} `options`
225 * @return {Object} Returns an AST
229 braces.parse = function(pattern, options) {
230 var proto = new Braces(options);
231 return proto.parse(pattern, options);
235 * Compile the given `ast` or string with the given `options`.
238 * var braces = require('braces');
239 * var ast = braces.parse('a/{b,c}/d');
240 * console.log(braces.compile(ast));
241 * // { options: { source: 'string' },
244 * // { eos: [Function],
245 * // noop: [Function],
246 * // bos: [Function],
247 * // brace: [Function],
248 * // 'brace.open': [Function],
249 * // text: [Function],
250 * // 'brace.close': [Function] },
251 * // output: [ 'a/(b|c)/d' ],
254 * // parsingErrors: [] }
256 * @param {Object|String} `ast` AST from [.parse](#parse). If a string is passed it will be parsed first.
257 * @param {Object} `options`
258 * @return {Object} Returns an object that has an `output` property with the compiled string.
262 braces.compile = function(ast, options) {
263 var proto = new Braces(options);
264 return proto.compile(ast, options);
268 * Clear the regex cache.
271 * braces.clearCache();
276 braces.clearCache = function() {
277 cache = braces.cache = {};
281 * Memoize a generated regex or function. A unique key is generated
282 * from the method name, pattern, and user-defined options. Set
283 * options.memoize to false to disable.
286 function memoize(type, pattern, options, fn) {
287 var key = utils.createKey(type + ':' + pattern, options);
288 var disabled = options && options.cache === false;
291 return fn(pattern, options);
294 if (cache.hasOwnProperty(key)) {
298 var res = fn(pattern, options);
304 * Expose `Braces` constructor and methods
308 braces.Braces = Braces;
309 braces.compilers = compilers;
310 braces.parsers = parsers;
311 braces.cache = cache;
318 module.exports = braces;