index.js 7.3 KB

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