From 32159f618923ea7c4594d2784fb6b1bed6f695cb Mon Sep 17 00:00:00 2001 From: James Prevett Date: Sun, 22 Dec 2024 19:34:48 -0600 Subject: [PATCH 1/5] Simplify compare --- index.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/index.js b/index.js index c8071cc..e6fc886 100644 --- a/index.js +++ b/index.js @@ -115,7 +115,7 @@ function splitToRanges(min, max) { } stops = [...stops]; - stops.sort(compare); + stops.sort((a, b) => a - b); return stops; } @@ -221,10 +221,6 @@ function zip(a, b) { return arr; } -function compare(a, b) { - return a > b ? 1 : b > a ? -1 : 0; -} - function contains(arr, key, val) { return arr.some(ele => ele[key] === val); } From 2339d954a3d235e57c04a85ddca3e81e6c76f112 Mon Sep 17 00:00:00 2001 From: James Prevett Date: Sun, 22 Dec 2024 19:42:40 -0600 Subject: [PATCH 2/5] Inline, memoize, and ternary --- index.js | 56 +++++++++++++++++++------------------------------------- 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/index.js b/index.js index e6fc886..193a6b2 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,8 @@ const isNumber = (v) => (typeof v === "number" && v - v === 0) || (typeof v === "string" && Number.isFinite(+v) && v.trim() !== ""); +const paddingRegex = /^-?(0+)\d/; + const toRegexRange = (min, max, options) => { if (isNumber(min) === false) { throw new TypeError('toRegexRange: expected the first argument to be a number'); @@ -41,17 +43,12 @@ const toRegexRange = (min, max, options) => { let b = Math.max(min, max); if (Math.abs(a - b) === 1) { - let result = min + '|' + max; - if (opts.capture) { - return `(${result})`; - } - if (opts.wrap === false) { - return result; - } - return `(?:${result})`; + const result = min + '|' + max; + + return opts.capture ? `(${result})` : opts.wrap ? `(?:${result})` : result; } - let isPadded = hasPadding(min) || hasPadding(max); + let isPadded = paddingRegex.test(min) || paddingRegex.test(max); let state = { min, max, a, b }; let positives = []; let negatives = []; @@ -131,7 +128,9 @@ function rangeToPattern(start, stop, options) { return { pattern: start, count: [], digits: 0 }; } - let zipped = zip(start, stop); + let zipped = []; + for (let i = 0; i < start.length; i++) zipped.push([start[i], stop[i]]); + let digits = zipped.length; let pattern = ''; let count = 0; @@ -143,7 +142,7 @@ function rangeToPattern(start, stop, options) { pattern += startDigit; } else if (startDigit !== '0' || stopDigit !== '9') { - pattern += toCharacterClass(startDigit, stopDigit, options); + pattern += `[${startDigit}${(stopDigit - startDigit === 1) ? '' : '-'}${stopDigit}]`; } else { count++; @@ -158,10 +157,10 @@ function rangeToPattern(start, stop, options) { } function splitToPatterns(min, max, tok, options) { - let ranges = splitToRanges(min, max); - let tokens = []; - let start = min; - let prev; + let ranges = splitToRanges(min, max), + tokens = [], + start = min, + prev; for (let i = 0; i < ranges.length; i++) { let max = ranges[i]; @@ -198,29 +197,21 @@ function filterPatterns(arr, comparison, prefix, intersection, options) { for (let ele of arr) { let { string } = ele; + const contained = contains(comparison, 'string', string); + // only push if _both_ are negative... - if (!intersection && !contains(comparison, 'string', string)) { + if (!intersection && !contained) { result.push(prefix + string); } // or _both_ are positive - if (intersection && contains(comparison, 'string', string)) { + if (intersection && contained) { result.push(prefix + string); } } return result; } -/** - * Zip strings - */ - -function zip(a, b) { - let arr = []; - for (let i = 0; i < a.length; i++) arr.push([a[i], b[i]]); - return arr; -} - function contains(arr, key, val) { return arr.some(ele => ele[key] === val); } @@ -235,18 +226,9 @@ function countZeros(integer, zeros) { function toQuantifier(digits) { let [start = 0, stop = ''] = digits; - if (stop || start > 1) { - return `{${start + (stop ? ',' + stop : '')}}`; - } - return ''; -} -function toCharacterClass(a, b, options) { - return `[${a}${(b - a === 1) ? '' : '-'}${b}]`; -} + return (stop || start > 1) ? `{${start + (stop ? ',' + stop : '')}}` : ''; -function hasPadding(str) { - return /^-?(0+)\d/.test(str); } function padZeros(value, tok, options) { From c0a6a0355318f9a14e25b75a81bf27db50e841da Mon Sep 17 00:00:00 2001 From: James Prevett Date: Sun, 22 Dec 2024 19:53:02 -0600 Subject: [PATCH 3/5] More simplification --- index.js | 49 ++++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/index.js b/index.js index 193a6b2..fb25f21 100644 --- a/index.js +++ b/index.js @@ -26,14 +26,10 @@ const toRegexRange = (min, max, options) => { let opts = { relaxZeros: true, ...options }; if (typeof opts.strictZeros === 'boolean') { - opts.relaxZeros = opts.strictZeros === false; + opts.relaxZeros = !opts.strictZeros; } - let relax = String(opts.relaxZeros); - let shorthand = String(opts.shorthand); - let capture = String(opts.capture); - let wrap = String(opts.wrap); - let cacheKey = min + ':' + max + '=' + relax + shorthand + capture + wrap; + const cacheKey = min + ':' + max + '=' + String(opts.relaxZeros) + String(opts.shorthand) + String(opts.capture) + String(opts.wrap); if (toRegexRange.cache.hasOwnProperty(cacheKey)) { return toRegexRange.cache[cacheKey].result; @@ -44,7 +40,6 @@ const toRegexRange = (min, max, options) => { if (Math.abs(a - b) === 1) { const result = min + '|' + max; - return opts.capture ? `(${result})` : opts.wrap ? `(?:${result})` : result; } @@ -70,7 +65,11 @@ const toRegexRange = (min, max, options) => { state.negatives = negatives; state.positives = positives; - state.result = collatePatterns(negatives, positives, opts); + state.result = [ + ...filterPatterns(negatives, positives, '-', false, opts), + ...filterPatterns(negatives, positives, '-?', true, opts), + ...filterPatterns(positives, negatives, '', false, opts), + ].join('|'); if (opts.capture === true) { state.result = `(${state.result})`; @@ -82,14 +81,6 @@ const toRegexRange = (min, max, options) => { return state.result; }; -function collatePatterns(neg, pos, options) { - let onlyNegative = filterPatterns(neg, pos, '-', false, options) || []; - let onlyPositive = filterPatterns(pos, neg, '', false, options) || []; - let intersected = filterPatterns(neg, pos, '-?', true, options) || []; - let subpatterns = onlyNegative.concat(intersected).concat(onlyPositive); - return subpatterns.join('|'); -} - function splitToRanges(min, max) { let nines = 1; let zeros = 1; @@ -111,9 +102,7 @@ function splitToRanges(min, max) { stop = countZeros(max + 1, zeros) - 1; } - stops = [...stops]; - stops.sort((a, b) => a - b); - return stops; + return Array.from(stops).sort((a, b) => a - b); } /** @@ -128,10 +117,10 @@ function rangeToPattern(start, stop, options) { return { pattern: start, count: [], digits: 0 }; } - let zipped = []; + const zipped = []; for (let i = 0; i < start.length; i++) zipped.push([start[i], stop[i]]); - let digits = zipped.length; + const digits = zipped.length; let pattern = ''; let count = 0; @@ -157,10 +146,8 @@ function rangeToPattern(start, stop, options) { } function splitToPatterns(min, max, tok, options) { - let ranges = splitToRanges(min, max), - tokens = [], - start = min, - prev; + const tokens = [], ranges = splitToRanges(min, max); + let prev, start = min; for (let i = 0; i < ranges.length; i++) { let max = ranges[i]; @@ -192,10 +179,10 @@ function splitToPatterns(min, max, tok, options) { } function filterPatterns(arr, comparison, prefix, intersection, options) { - let result = []; + const result = []; - for (let ele of arr) { - let { string } = ele; + for (const ele of arr) { + const { string } = ele; const contained = contains(comparison, 'string', string); @@ -225,7 +212,7 @@ function countZeros(integer, zeros) { } function toQuantifier(digits) { - let [start = 0, stop = ''] = digits; + const [start = 0, stop = ''] = digits; return (stop || start > 1) ? `{${start + (stop ? ',' + stop : '')}}` : ''; @@ -236,8 +223,8 @@ function padZeros(value, tok, options) { return value; } - let diff = Math.abs(tok.maxLen - String(value).length); - let relax = options.relaxZeros !== false; + const diff = Math.abs(tok.maxLen - String(value).length); + const relax = !!options.relaxZeros !== false; switch (diff) { case 0: From 31a8efca8dd273821587acba9a8072be64209ca9 Mon Sep 17 00:00:00 2001 From: James Prevett Date: Sun, 22 Dec 2024 20:00:42 -0600 Subject: [PATCH 4/5] Simplify `filterPatterns` --- index.js | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index fb25f21..ccab3cd 100644 --- a/index.js +++ b/index.js @@ -178,31 +178,21 @@ function splitToPatterns(min, max, tok, options) { return tokens; } -function filterPatterns(arr, comparison, prefix, intersection, options) { +function filterPatterns(arr, comparison, prefix, intersection) { const result = []; - for (const ele of arr) { - const { string } = ele; + for (const { string } of arr) { + const contained = comparison.some(ele => ele.string === string); - const contained = contains(comparison, 'string', string); - - // only push if _both_ are negative... - if (!intersection && !contained) { + // only push if _both_ are negative or positive + if (intersection === contained) { result.push(prefix + string); } - // or _both_ are positive - if (intersection && contained) { - result.push(prefix + string); - } } return result; } -function contains(arr, key, val) { - return arr.some(ele => ele[key] === val); -} - function countNines(min, len) { return Number(String(min).slice(0, -len) + '9'.repeat(len)); } From dea0eeb30cf205b0bd5aaf88039fd6f4b3ff5cd9 Mon Sep 17 00:00:00 2001 From: James Prevett Date: Sun, 22 Dec 2024 20:14:19 -0600 Subject: [PATCH 5/5] Use parameter destructuring in `toQuantifier` --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index ccab3cd..ba9f061 100644 --- a/index.js +++ b/index.js @@ -201,8 +201,7 @@ function countZeros(integer, zeros) { return integer - (integer % Math.pow(10, zeros)); } -function toQuantifier(digits) { - const [start = 0, stop = ''] = digits; +function toQuantifier([start = 0, stop = '']) { return (stop || start > 1) ? `{${start + (stop ? ',' + stop : '')}}` : '';