暫無描述

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. 'use strict';
  2. var splitString = require('split-string');
  3. var utils = module.exports;
  4. /**
  5. * Module dependencies
  6. */
  7. utils.define = require('define-property');
  8. utils.extend = require('extend-shallow');
  9. utils.flatten = require('arr-flatten');
  10. utils.isObject = require('isobject');
  11. utils.fillRange = require('fill-range');
  12. utils.repeat = require('repeat-element');
  13. utils.unique = require('array-unique');
  14. /**
  15. * Returns true if the given string contains only empty brace sets.
  16. */
  17. utils.isEmptySets = function(str) {
  18. return /^(?:\{,\})+$/.test(str);
  19. };
  20. /**
  21. * Returns true if the given string contains only empty brace sets.
  22. */
  23. utils.isQuotedString = function(str) {
  24. var open = str.charAt(0);
  25. if (open === '\'' || open === '"' || open === '`') {
  26. return str.slice(-1) === open;
  27. }
  28. return false;
  29. };
  30. /**
  31. * Create the key to use for memoization. The unique key is generated
  32. * by iterating over the options and concatenating key-value pairs
  33. * to the pattern string.
  34. */
  35. utils.createKey = function(pattern, options) {
  36. var id = pattern;
  37. if (typeof options === 'undefined') {
  38. return id;
  39. }
  40. var keys = Object.keys(options);
  41. for (var i = 0; i < keys.length; i++) {
  42. var key = keys[i];
  43. id += ';' + key + '=' + String(options[key]);
  44. }
  45. return id;
  46. };
  47. /**
  48. * Normalize options
  49. */
  50. utils.createOptions = function(options) {
  51. var opts = utils.extend.apply(null, arguments);
  52. if (typeof opts.expand === 'boolean') {
  53. opts.optimize = !opts.expand;
  54. }
  55. if (typeof opts.optimize === 'boolean') {
  56. opts.expand = !opts.optimize;
  57. }
  58. if (opts.optimize === true) {
  59. opts.makeRe = true;
  60. }
  61. return opts;
  62. };
  63. /**
  64. * Join patterns in `a` to patterns in `b`
  65. */
  66. utils.join = function(a, b, options) {
  67. options = options || {};
  68. a = utils.arrayify(a);
  69. b = utils.arrayify(b);
  70. if (!a.length) return b;
  71. if (!b.length) return a;
  72. var len = a.length;
  73. var idx = -1;
  74. var arr = [];
  75. while (++idx < len) {
  76. var val = a[idx];
  77. if (Array.isArray(val)) {
  78. for (var i = 0; i < val.length; i++) {
  79. val[i] = utils.join(val[i], b, options);
  80. }
  81. arr.push(val);
  82. continue;
  83. }
  84. for (var j = 0; j < b.length; j++) {
  85. var bval = b[j];
  86. if (Array.isArray(bval)) {
  87. arr.push(utils.join(val, bval, options));
  88. } else {
  89. arr.push(val + bval);
  90. }
  91. }
  92. }
  93. return arr;
  94. };
  95. /**
  96. * Split the given string on `,` if not escaped.
  97. */
  98. utils.split = function(str, options) {
  99. var opts = utils.extend({sep: ','}, options);
  100. if (typeof opts.keepQuotes !== 'boolean') {
  101. opts.keepQuotes = true;
  102. }
  103. if (opts.unescape === false) {
  104. opts.keepEscaping = true;
  105. }
  106. return splitString(str, opts, utils.escapeBrackets(opts));
  107. };
  108. /**
  109. * Expand ranges or sets in the given `pattern`.
  110. *
  111. * @param {String} `str`
  112. * @param {Object} `options`
  113. * @return {Object}
  114. */
  115. utils.expand = function(str, options) {
  116. var opts = utils.extend({rangeLimit: 10000}, options);
  117. var segs = utils.split(str, opts);
  118. var tok = { segs: segs };
  119. if (utils.isQuotedString(str)) {
  120. return tok;
  121. }
  122. if (opts.rangeLimit === true) {
  123. opts.rangeLimit = 10000;
  124. }
  125. if (segs.length > 1) {
  126. if (opts.optimize === false) {
  127. tok.val = segs[0];
  128. return tok;
  129. }
  130. tok.segs = utils.stringifyArray(tok.segs);
  131. } else if (segs.length === 1) {
  132. var arr = str.split('..');
  133. if (arr.length === 1) {
  134. tok.val = tok.segs[tok.segs.length - 1] || tok.val || str;
  135. tok.segs = [];
  136. return tok;
  137. }
  138. if (arr.length === 2 && arr[0] === arr[1]) {
  139. tok.escaped = true;
  140. tok.val = arr[0];
  141. tok.segs = [];
  142. return tok;
  143. }
  144. if (arr.length > 1) {
  145. if (opts.optimize !== false) {
  146. opts.optimize = true;
  147. delete opts.expand;
  148. }
  149. if (opts.optimize !== true) {
  150. var min = Math.min(arr[0], arr[1]);
  151. var max = Math.max(arr[0], arr[1]);
  152. var step = arr[2] || 1;
  153. if (opts.rangeLimit !== false && ((max - min) / step >= opts.rangeLimit)) {
  154. throw new RangeError('expanded array length exceeds range limit. Use options.rangeLimit to increase or disable the limit.');
  155. }
  156. }
  157. arr.push(opts);
  158. tok.segs = utils.fillRange.apply(null, arr);
  159. if (!tok.segs.length) {
  160. tok.escaped = true;
  161. tok.val = str;
  162. return tok;
  163. }
  164. if (opts.optimize === true) {
  165. tok.segs = utils.stringifyArray(tok.segs);
  166. }
  167. if (tok.segs === '') {
  168. tok.val = str;
  169. } else {
  170. tok.val = tok.segs[0];
  171. }
  172. return tok;
  173. }
  174. } else {
  175. tok.val = str;
  176. }
  177. return tok;
  178. };
  179. /**
  180. * Ensure commas inside brackets and parens are not split.
  181. * @param {Object} `tok` Token from the `split-string` module
  182. * @return {undefined}
  183. */
  184. utils.escapeBrackets = function(options) {
  185. return function(tok) {
  186. if (tok.escaped && tok.val === 'b') {
  187. tok.val = '\\b';
  188. return;
  189. }
  190. if (tok.val !== '(' && tok.val !== '[') return;
  191. var opts = utils.extend({}, options);
  192. var brackets = [];
  193. var parens = [];
  194. var stack = [];
  195. var val = tok.val;
  196. var str = tok.str;
  197. var i = tok.idx - 1;
  198. while (++i < str.length) {
  199. var ch = str[i];
  200. if (ch === '\\') {
  201. val += (opts.keepEscaping === false ? '' : ch) + str[++i];
  202. continue;
  203. }
  204. if (ch === '(') {
  205. parens.push(ch);
  206. stack.push(ch);
  207. }
  208. if (ch === '[') {
  209. brackets.push(ch);
  210. stack.push(ch);
  211. }
  212. if (ch === ')') {
  213. parens.pop();
  214. stack.pop();
  215. if (!stack.length) {
  216. val += ch;
  217. break;
  218. }
  219. }
  220. if (ch === ']') {
  221. brackets.pop();
  222. stack.pop();
  223. if (!stack.length) {
  224. val += ch;
  225. break;
  226. }
  227. }
  228. val += ch;
  229. }
  230. tok.split = false;
  231. tok.val = val.slice(1);
  232. tok.idx = i;
  233. };
  234. };
  235. /**
  236. * Returns true if the given string looks like a regex quantifier
  237. * @return {Boolean}
  238. */
  239. utils.isQuantifier = function(str) {
  240. return /^(?:[0-9]?,[0-9]|[0-9],)$/.test(str);
  241. };
  242. /**
  243. * Cast `val` to an array.
  244. * @param {*} `val`
  245. */
  246. utils.stringifyArray = function(arr) {
  247. return [utils.arrayify(arr).join('|')];
  248. };
  249. /**
  250. * Cast `val` to an array.
  251. * @param {*} `val`
  252. */
  253. utils.arrayify = function(arr) {
  254. if (typeof arr === 'undefined') {
  255. return [];
  256. }
  257. if (typeof arr === 'string') {
  258. return [arr];
  259. }
  260. return arr;
  261. };
  262. /**
  263. * Returns true if the given `str` is a non-empty string
  264. * @return {Boolean}
  265. */
  266. utils.isString = function(str) {
  267. return str != null && typeof str === 'string';
  268. };
  269. /**
  270. * Get the last element from `array`
  271. * @param {Array} `array`
  272. * @return {*}
  273. */
  274. utils.last = function(arr, n) {
  275. return arr[arr.length - (n || 1)];
  276. };
  277. utils.escapeRegex = function(str) {
  278. return str.replace(/\\?([!^*?()\[\]{}+?/])/g, '\\$1');
  279. };