index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*!
  2. * to-regex-range <https://github.com/micromatch/to-regex-range>
  3. *
  4. * Copyright (c) 2015-present, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. 'use strict';
  8. const isNumber = require('is-number');
  9. const toRegexRange = (min, max, options) => {
  10. if (isNumber(min) === false) {
  11. throw new TypeError('toRegexRange: expected the first argument to be a number');
  12. }
  13. if (max === void 0 || min === max) {
  14. return String(min);
  15. }
  16. if (isNumber(max) === false) {
  17. throw new TypeError('toRegexRange: expected the second argument to be a number.');
  18. }
  19. let opts = { relaxZeros: true, ...options };
  20. if (typeof opts.strictZeros === 'boolean') {
  21. opts.relaxZeros = opts.strictZeros === false;
  22. }
  23. let relax = String(opts.relaxZeros);
  24. let shorthand = String(opts.shorthand);
  25. let capture = String(opts.capture);
  26. let wrap = String(opts.wrap);
  27. let cacheKey = min + ':' + max + '=' + relax + shorthand + capture + wrap;
  28. if (toRegexRange.cache.hasOwnProperty(cacheKey)) {
  29. return toRegexRange.cache[cacheKey].result;
  30. }
  31. let a = Math.min(min, max);
  32. let b = Math.max(min, max);
  33. if (Math.abs(a - b) === 1) {
  34. let result = min + '|' + max;
  35. if (opts.capture) {
  36. return `(${result})`;
  37. }
  38. if (opts.wrap === false) {
  39. return result;
  40. }
  41. return `(?:${result})`;
  42. }
  43. let isPadded = hasPadding(min) || hasPadding(max);
  44. let state = { min, max, a, b };
  45. let positives = [];
  46. let negatives = [];
  47. if (isPadded) {
  48. state.isPadded = isPadded;
  49. state.maxLen = String(state.max).length;
  50. }
  51. if (a < 0) {
  52. let newMin = b < 0 ? Math.abs(b) : 1;
  53. negatives = splitToPatterns(newMin, Math.abs(a), state, opts);
  54. a = state.a = 0;
  55. }
  56. if (b >= 0) {
  57. positives = splitToPatterns(a, b, state, opts);
  58. }
  59. state.negatives = negatives;
  60. state.positives = positives;
  61. state.result = collatePatterns(negatives, positives, opts);
  62. if (opts.capture === true) {
  63. state.result = `(${state.result})`;
  64. } else if (opts.wrap !== false && (positives.length + negatives.length) > 1) {
  65. state.result = `(?:${state.result})`;
  66. }
  67. toRegexRange.cache[cacheKey] = state;
  68. return state.result;
  69. };
  70. function collatePatterns(neg, pos, options) {
  71. let onlyNegative = filterPatterns(neg, pos, '-', false, options) || [];
  72. let onlyPositive = filterPatterns(pos, neg, '', false, options) || [];
  73. let intersected = filterPatterns(neg, pos, '-?', true, options) || [];
  74. let subpatterns = onlyNegative.concat(intersected).concat(onlyPositive);
  75. return subpatterns.join('|');
  76. }
  77. function splitToRanges(min, max) {
  78. let nines = 1;
  79. let zeros = 1;
  80. let stop = countNines(min, nines);
  81. let stops = new Set([max]);
  82. while (min <= stop && stop <= max) {
  83. stops.add(stop);
  84. nines += 1;
  85. stop = countNines(min, nines);
  86. }
  87. stop = countZeros(max + 1, zeros) - 1;
  88. while (min < stop && stop <= max) {
  89. stops.add(stop);
  90. zeros += 1;
  91. stop = countZeros(max + 1, zeros) - 1;
  92. }
  93. stops = [...stops];
  94. stops.sort(compare);
  95. return stops;
  96. }
  97. /**
  98. * Convert a range to a regex pattern
  99. * @param {Number} `start`
  100. * @param {Number} `stop`
  101. * @return {String}
  102. */
  103. function rangeToPattern(start, stop, options) {
  104. if (start === stop) {
  105. return { pattern: start, count: [], digits: 0 };
  106. }
  107. let zipped = zip(start, stop);
  108. let digits = zipped.length;
  109. let pattern = '';
  110. let count = 0;
  111. for (let i = 0; i < digits; i++) {
  112. let [startDigit, stopDigit] = zipped[i];
  113. if (startDigit === stopDigit) {
  114. pattern += startDigit;
  115. } else if (startDigit !== '0' || stopDigit !== '9') {
  116. pattern += toCharacterClass(startDigit, stopDigit, options);
  117. } else {
  118. count++;
  119. }
  120. }
  121. if (count) {
  122. pattern += options.shorthand === true ? '\\d' : '[0-9]';
  123. }
  124. return { pattern, count: [count], digits };
  125. }
  126. function splitToPatterns(min, max, tok, options) {
  127. let ranges = splitToRanges(min, max);
  128. let tokens = [];
  129. let start = min;
  130. let prev;
  131. for (let i = 0; i < ranges.length; i++) {
  132. let max = ranges[i];
  133. let obj = rangeToPattern(String(start), String(max), options);
  134. let zeros = '';
  135. if (!tok.isPadded && prev && prev.pattern === obj.pattern) {
  136. if (prev.count.length > 1) {
  137. prev.count.pop();
  138. }
  139. prev.count.push(obj.count[0]);
  140. prev.string = prev.pattern + toQuantifier(prev.count);
  141. start = max + 1;
  142. continue;
  143. }
  144. if (tok.isPadded) {
  145. zeros = padZeros(max, tok, options);
  146. }
  147. obj.string = zeros + obj.pattern + toQuantifier(obj.count);
  148. tokens.push(obj);
  149. start = max + 1;
  150. prev = obj;
  151. }
  152. return tokens;
  153. }
  154. function filterPatterns(arr, comparison, prefix, intersection, options) {
  155. let result = [];
  156. for (let ele of arr) {
  157. let { string } = ele;
  158. // only push if _both_ are negative...
  159. if (!intersection && !contains(comparison, 'string', string)) {
  160. result.push(prefix + string);
  161. }
  162. // or _both_ are positive
  163. if (intersection && contains(comparison, 'string', string)) {
  164. result.push(prefix + string);
  165. }
  166. }
  167. return result;
  168. }
  169. /**
  170. * Zip strings
  171. */
  172. function zip(a, b) {
  173. let arr = [];
  174. for (let i = 0; i < a.length; i++) arr.push([a[i], b[i]]);
  175. return arr;
  176. }
  177. function compare(a, b) {
  178. return a > b ? 1 : b > a ? -1 : 0;
  179. }
  180. function contains(arr, key, val) {
  181. return arr.some(ele => ele[key] === val);
  182. }
  183. function countNines(min, len) {
  184. return Number(String(min).slice(0, -len) + '9'.repeat(len));
  185. }
  186. function countZeros(integer, zeros) {
  187. return integer - (integer % Math.pow(10, zeros));
  188. }
  189. function toQuantifier(digits) {
  190. let [start = 0, stop = ''] = digits;
  191. if (stop || start > 1) {
  192. return `{${start + (stop ? ',' + stop : '')}}`;
  193. }
  194. return '';
  195. }
  196. function toCharacterClass(a, b, options) {
  197. return `[${a}${(b - a === 1) ? '' : '-'}${b}]`;
  198. }
  199. function hasPadding(str) {
  200. return /^-?(0+)\d/.test(str);
  201. }
  202. function padZeros(value, tok, options) {
  203. if (!tok.isPadded) {
  204. return value;
  205. }
  206. let diff = Math.abs(tok.maxLen - String(value).length);
  207. let relax = options.relaxZeros !== false;
  208. switch (diff) {
  209. case 0:
  210. return '';
  211. case 1:
  212. return relax ? '0?' : '0';
  213. case 2:
  214. return relax ? '0{0,2}' : '00';
  215. default: {
  216. return relax ? `0{0,${diff}}` : `0{${diff}}`;
  217. }
  218. }
  219. }
  220. /**
  221. * Cache
  222. */
  223. toRegexRange.cache = {};
  224. toRegexRange.clearCache = () => (toRegexRange.cache = {});
  225. /**
  226. * Expose `toRegexRange`
  227. */
  228. module.exports = toRegexRange;