From 02ec4a3bfdb89ece796921bda609397e2e10d3a3 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Mon, 18 Nov 2019 13:21:05 +0000 Subject: [PATCH] ToCaseInsensitiveRegex improvements --- .../operations/ToCaseInsensitiveRegex.mjs | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/core/operations/ToCaseInsensitiveRegex.mjs b/src/core/operations/ToCaseInsensitiveRegex.mjs index 28bd3dc9..4850846d 100644 --- a/src/core/operations/ToCaseInsensitiveRegex.mjs +++ b/src/core/operations/ToCaseInsensitiveRegex.mjs @@ -32,7 +32,56 @@ class ToCaseInsensitiveRegex extends Operation { * @returns {string} */ run(input, args) { - return input.replace(/[a-z]/ig, m => `[${m.toLowerCase()}${m.toUpperCase()}]`); + /** + * Simulates look behind behaviour since javascript doesn't support it. + * + * @param {string} input + * @returns {string} + */ + function preProcess(input) { + let result = ""; + for (let i = 0; i < input.length; i++) { + const temp = input.charAt(i); + if (temp.match(/[a-zA-Z]/g) && (input.charAt(i-1) !== "-") && (input.charAt(i+1) !== "-")) + result += "[" + temp.toLowerCase() + temp.toUpperCase() + "]"; + else + result += temp; + } + return result; + } + + input = preProcess(input); + + // Example: [a-z] -> [a-zA-Z] + input = input.replace(/[a-z]-[a-z]/g, m => `${m}${m[0].toUpperCase()}-${m[2].toUpperCase()}`); + + // Example: [a-z] -> [a-zA-Z] + input = input.replace(/[A-Z]-[A-Z]/g, m => `${m}${m[0].toLowerCase()}-${m[2].toLowerCase()}`); + + // Example: [H-d] -> [A-DH-dh-z] + input = input.replace(/[A-Z]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}${m[0].toLowerCase()}-z`); + + // Example: [!-D] -> [!-Da-d] + input = input.replace(/[ -@]-[A-Z]/g, m => `${m}a-${m[2].toLowerCase()}`); + + // Example: [%-^] -> [%-^a-z] + input = input.replace(/[ -@]-[[-`]/g, m => `${m}a-z`); + + // Example: [K-`] -> [K-`k-z] + input = input.replace(/[A-Z]-[[-`]/g, m => `${m}${m[0].toLowerCase()}-z`); + + // Example: [[-}] -> [[-}A-Z] + input = input.replace(/[[-`]-[{-~]/g, m => `${m}A-Z`); + + // Example: [b-}] -> [b-}B-Z] + input = input.replace(/[a-z]-[{-~]/g, m => `${m}${m[0].toUpperCase()}-Z`); + + // Example: [<-j] -> [<-z] + input = input.replace(/[ -@]-[a-z]/g, m => `${m[0]}-z`); + + // Example: [^-j] -> [A-J^-j] + input = input.replace(/[[-`]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}`); + return input; } }