Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

utils.js 6.9KB

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