index.js
7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*!
* braces <https://github.com/jonschlinkert/braces>
*
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT license.
*/
'use strict';
/**
* Module dependencies
*/
var expand = require('expand-range');
var repeat = require('repeat-element');
var tokens = require('preserve');
/**
* Expose `braces`
*/
module.exports = function(str, options) {
if (typeof str !== 'string') {
throw new Error('braces expects a string');
}
return braces(str, options);
};
/**
* Expand `{foo,bar}` or `{1..5}` braces in the
* given `string`.
*
* @param {String} `str`
* @param {Array} `arr`
* @param {Object} `options`
* @return {Array}
*/
function braces(str, arr, options) {
if (str === '') {
return [];
}
if (!Array.isArray(arr)) {
options = arr;
arr = [];
}
var opts = options || {};
arr = arr || [];
if (typeof opts.nodupes === 'undefined') {
opts.nodupes = true;
}
var fn = opts.fn;
var es6;
if (typeof opts === 'function') {
fn = opts;
opts = {};
}
if (!(patternRe instanceof RegExp)) {
patternRe = patternRegex();
}
var matches = str.match(patternRe) || [];
var m = matches[0];
switch(m) {
case '\\,':
return escapeCommas(str, arr, opts);
case '\\.':
return escapeDots(str, arr, opts);
case '\/.':
return escapePaths(str, arr, opts);
case ' ':
return splitWhitespace(str);
case '{,}':
return exponential(str, opts, braces);
case '{}':
return emptyBraces(str, arr, opts);
case '\\{':
case '\\}':
return escapeBraces(str, arr, opts);
case '${':
if (!/\{[^{]+\{/.test(str)) {
return arr.concat(str);
} else {
es6 = true;
str = tokens.before(str, es6Regex());
}
}
if (!(braceRe instanceof RegExp)) {
braceRe = braceRegex();
}
var match = braceRe.exec(str);
if (match == null) {
return [str];
}
var outter = match[1];
var inner = match[2];
if (inner === '') { return [str]; }
var segs, segsLength;
if (inner.indexOf('..') !== -1) {
segs = expand(inner, opts, fn) || inner.split(',');
segsLength = segs.length;
} else if (inner[0] === '"' || inner[0] === '\'') {
return arr.concat(str.split(/['"]/).join(''));
} else {
segs = inner.split(',');
if (opts.makeRe) {
return braces(str.replace(outter, wrap(segs, '|')), opts);
}
segsLength = segs.length;
if (segsLength === 1 && opts.bash) {
segs[0] = wrap(segs[0], '\\');
}
}
var len = segs.length;
var i = 0, val;
while (len--) {
var path = segs[i++];
if (/(\.[^.\/])/.test(path)) {
if (segsLength > 1) {
return segs;
} else {
return [str];
}
}
val = splice(str, outter, path);
if (/\{[^{}]+?\}/.test(val)) {
arr = braces(val, arr, opts);
} else if (val !== '') {
if (opts.nodupes && arr.indexOf(val) !== -1) { continue; }
arr.push(es6 ? tokens.after(val) : val);
}
}
if (opts.strict) { return filter(arr, filterEmpty); }
return arr;
}
/**
* Expand exponential ranges
*
* `a{,}{,}` => ['a', 'a', 'a', 'a']
*/
function exponential(str, options, fn) {
if (typeof options === 'function') {
fn = options;
options = null;
}
var opts = options || {};
var esc = '__ESC_EXP__';
var exp = 0;
var res;
var parts = str.split('{,}');
if (opts.nodupes) {
return fn(parts.join(''), opts);
}
exp = parts.length - 1;
res = fn(parts.join(esc), opts);
var len = res.length;
var arr = [];
var i = 0;
while (len--) {
var ele = res[i++];
var idx = ele.indexOf(esc);
if (idx === -1) {
arr.push(ele);
} else {
ele = ele.split('__ESC_EXP__').join('');
if (!!ele && opts.nodupes !== false) {
arr.push(ele);
} else {
var num = Math.pow(2, exp);
arr.push.apply(arr, repeat(ele, num));
}
}
}
return arr;
}
/**
* Wrap a value with parens, brackets or braces,
* based on the given character/separator.
*
* @param {String|Array} `val`
* @param {String} `ch`
* @return {String}
*/
function wrap(val, ch) {
if (ch === '|') {
return '(' + val.join(ch) + ')';
}
if (ch === ',') {
return '{' + val.join(ch) + '}';
}
if (ch === '-') {
return '[' + val.join(ch) + ']';
}
if (ch === '\\') {
return '\\{' + val + '\\}';
}
}
/**
* Handle empty braces: `{}`
*/
function emptyBraces(str, arr, opts) {
return braces(str.split('{}').join('\\{\\}'), arr, opts);
}
/**
* Filter out empty-ish values
*/
function filterEmpty(ele) {
return !!ele && ele !== '\\';
}
/**
* Handle patterns with whitespace
*/
function splitWhitespace(str) {
var segs = str.split(' ');
var len = segs.length;
var res = [];
var i = 0;
while (len--) {
res.push.apply(res, braces(segs[i++]));
}
return res;
}
/**
* Handle escaped braces: `\\{foo,bar}`
*/
function escapeBraces(str, arr, opts) {
if (!/\{[^{]+\{/.test(str)) {
return arr.concat(str.split('\\').join(''));
} else {
str = str.split('\\{').join('__LT_BRACE__');
str = str.split('\\}').join('__RT_BRACE__');
return map(braces(str, arr, opts), function(ele) {
ele = ele.split('__LT_BRACE__').join('{');
return ele.split('__RT_BRACE__').join('}');
});
}
}
/**
* Handle escaped dots: `{1\\.2}`
*/
function escapeDots(str, arr, opts) {
if (!/[^\\]\..+\\\./.test(str)) {
return arr.concat(str.split('\\').join(''));
} else {
str = str.split('\\.').join('__ESC_DOT__');
return map(braces(str, arr, opts), function(ele) {
return ele.split('__ESC_DOT__').join('.');
});
}
}
/**
* Handle escaped dots: `{1\\.2}`
*/
function escapePaths(str, arr, opts) {
str = str.split('\/.').join('__ESC_PATH__');
return map(braces(str, arr, opts), function(ele) {
return ele.split('__ESC_PATH__').join('\/.');
});
}
/**
* Handle escaped commas: `{a\\,b}`
*/
function escapeCommas(str, arr, opts) {
if (!/\w,/.test(str)) {
return arr.concat(str.split('\\').join(''));
} else {
str = str.split('\\,').join('__ESC_COMMA__');
return map(braces(str, arr, opts), function(ele) {
return ele.split('__ESC_COMMA__').join(',');
});
}
}
/**
* Regex for common patterns
*/
function patternRegex() {
return /\${|( (?=[{,}])|(?=[{,}]) )|{}|{,}|\\,(?=.*[{}])|\/\.(?=.*[{}])|\\\.(?={)|\\{|\\}/;
}
/**
* Braces regex.
*/
function braceRegex() {
return /.*(\\?\{([^}]+)\})/;
}
/**
* es6 delimiter regex.
*/
function es6Regex() {
return /\$\{([^}]+)\}/;
}
var braceRe;
var patternRe;
/**
* Faster alternative to `String.replace()` when the
* index of the token to be replaces can't be supplied
*/
function splice(str, token, replacement) {
var i = str.indexOf(token);
return str.substr(0, i) + replacement
+ str.substr(i + token.length);
}
/**
* Fast array map
*/
function map(arr, fn) {
if (arr == null) {
return [];
}
var len = arr.length;
var res = new Array(len);
var i = -1;
while (++i < len) {
res[i] = fn(arr[i], i, arr);
}
return res;
}
/**
* Fast array filter
*/
function filter(arr, cb) {
if (arr == null) return [];
if (typeof cb !== 'function') {
throw new TypeError('braces: filter expects a callback function.');
}
var len = arr.length;
var res = arr.slice();
var i = 0;
while (len--) {
if (!cb(arr[len], i++)) {
res.splice(len, 1);
}
}
return res;
}