From 02ec4a3bfdb89ece796921bda609397e2e10d3a3 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Mon, 18 Nov 2019 13:21:05 +0000 Subject: [PATCH 1/8] 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; } } From 40d3c8b071de12afc627ad6654d654fc72873da8 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Mon, 18 Nov 2019 13:31:19 +0000 Subject: [PATCH 2/8] ToCaseInsensitiveRegex improvements --- src/core/operations/ToCaseInsensitiveRegex.mjs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/core/operations/ToCaseInsensitiveRegex.mjs b/src/core/operations/ToCaseInsensitiveRegex.mjs index 4850846d..044c350c 100644 --- a/src/core/operations/ToCaseInsensitiveRegex.mjs +++ b/src/core/operations/ToCaseInsensitiveRegex.mjs @@ -54,31 +54,31 @@ class ToCaseInsensitiveRegex extends Operation { // 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; From 6d77fe6eb3ae6e1bc72a0c448ae8f680f21ef7d7 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Wed, 20 Nov 2019 09:28:34 +0000 Subject: [PATCH 3/8] Combined two rules into one case insensitive rule --- .../operations/ToCaseInsensitiveRegex.mjs | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/core/operations/ToCaseInsensitiveRegex.mjs b/src/core/operations/ToCaseInsensitiveRegex.mjs index 044c350c..dcf0758b 100644 --- a/src/core/operations/ToCaseInsensitiveRegex.mjs +++ b/src/core/operations/ToCaseInsensitiveRegex.mjs @@ -50,37 +50,36 @@ class ToCaseInsensitiveRegex extends Operation { return result; } + // Example: [test] -> [[tT][eE][sS][tT]] 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: [A-Z] -> [A-Za-z] + input = input.replace(/[A-Z]-[A-Z]/ig, m => `${m[0].toUpperCase()}-${m[2].toUpperCase()}${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()}`); + input = input.replace(/\\?[ -@]-[A-Z]/g, m => `${m}a-${m[2].toLowerCase()}`); // Example: [%-^] -> [%-^a-z] - input = input.replace(/[ -@]-[[-`]/g, m => `${m}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`); + input = input.replace(/[A-Z]-\\?[[-`]/g, m => `${m}${m[0].toLowerCase()}-z`); // Example: [[-}] -> [[-}A-Z] - input = input.replace(/[[-`]-[{-~]/g, m => `${m}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`); + 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`); + 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}`); + input = input.replace(/\\?[[-`]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}`); + return input; } } From 7d41d4d030192844796dcaebfe7e8af7bc946247 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Thu, 21 Nov 2019 09:11:12 +0000 Subject: [PATCH 4/8] Replaced the .replaces in regex operation --- .../operations/ToCaseInsensitiveRegex.mjs | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/core/operations/ToCaseInsensitiveRegex.mjs b/src/core/operations/ToCaseInsensitiveRegex.mjs index dcf0758b..32c14a3c 100644 --- a/src/core/operations/ToCaseInsensitiveRegex.mjs +++ b/src/core/operations/ToCaseInsensitiveRegex.mjs @@ -51,36 +51,35 @@ class ToCaseInsensitiveRegex extends Operation { } // Example: [test] -> [[tT][eE][sS][tT]] - input = preProcess(input); + return preProcess(input) // Example: [A-Z] -> [A-Za-z] - input = input.replace(/[A-Z]-[A-Z]/ig, m => `${m[0].toUpperCase()}-${m[2].toUpperCase()}${m[0].toLowerCase()}-${m[2].toLowerCase()}`); + .replace(/[A-Z]-[A-Z]/ig, m => `${m[0].toUpperCase()}-${m[2].toUpperCase()}${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`); + .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()}`); + .replace(/\\?[ -@]-[A-Z]/g, m => `${m}a-${m[2].toLowerCase()}`) // Example: [%-^] -> [%-^a-z] - input = input.replace(/\\?[ -@]-\\?[[-`]/g, m => `${m}a-z`); + .replace(/\\?[ -@]-\\?[[-`]/g, m => `${m}a-z`) // Example: [K-`] -> [K-`k-z] - input = input.replace(/[A-Z]-\\?[[-`]/g, m => `${m}${m[0].toLowerCase()}-z`); + .replace(/[A-Z]-\\?[[-`]/g, m => `${m}${m[0].toLowerCase()}-z`) // Example: [[-}] -> [[-}A-Z] - input = input.replace(/\\?[[-`]-\\?[{-~]/g, m => `${m}A-Z`); + .replace(/\\?[[-`]-\\?[{-~]/g, m => `${m}A-Z`) // Example: [b-}] -> [b-}B-Z] - input = input.replace(/[a-z]-\\?[{-~]/g, m => `${m}${m[0].toUpperCase()}-Z`); + .replace(/[a-z]-\\?[{-~]/g, m => `${m}${m[0].toUpperCase()}-Z`) // Example: [<-j] -> [<-z] - input = input.replace(/\\?[ -@]-[a-z]/g, m => `${m[0]}-z`); + .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}`); + .replace(/\\?[[-`]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}`); - return input; } } From c60ed2c4033dc04327bf9807decd822cd5be99ec Mon Sep 17 00:00:00 2001 From: n1073645 Date: Thu, 21 Nov 2019 09:56:52 +0000 Subject: [PATCH 5/8] Linting on regex operation --- .../operations/ToCaseInsensitiveRegex.mjs | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/core/operations/ToCaseInsensitiveRegex.mjs b/src/core/operations/ToCaseInsensitiveRegex.mjs index 32c14a3c..9467b8c8 100644 --- a/src/core/operations/ToCaseInsensitiveRegex.mjs +++ b/src/core/operations/ToCaseInsensitiveRegex.mjs @@ -53,32 +53,32 @@ class ToCaseInsensitiveRegex extends Operation { // Example: [test] -> [[tT][eE][sS][tT]] return preProcess(input) - // Example: [A-Z] -> [A-Za-z] - .replace(/[A-Z]-[A-Z]/ig, m => `${m[0].toUpperCase()}-${m[2].toUpperCase()}${m[0].toLowerCase()}-${m[2].toLowerCase()}`) + // Example: [A-Z] -> [A-Za-z] + .replace(/[A-Z]-[A-Z]/ig, m => `${m[0].toUpperCase()}-${m[2].toUpperCase()}${m[0].toLowerCase()}-${m[2].toLowerCase()}`) - // Example: [H-d] -> [A-DH-dh-z] - .replace(/[A-Z]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}${m[0].toLowerCase()}-z`) + // Example: [H-d] -> [A-DH-dh-z] + .replace(/[A-Z]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}${m[0].toLowerCase()}-z`) - // Example: [!-D] -> [!-Da-d] - .replace(/\\?[ -@]-[A-Z]/g, m => `${m}a-${m[2].toLowerCase()}`) + // Example: [!-D] -> [!-Da-d] + .replace(/\\?[ -@]-[A-Z]/g, m => `${m}a-${m[2].toLowerCase()}`) - // Example: [%-^] -> [%-^a-z] - .replace(/\\?[ -@]-\\?[[-`]/g, m => `${m}a-z`) + // Example: [%-^] -> [%-^a-z] + .replace(/\\?[ -@]-\\?[[-`]/g, m => `${m}a-z`) - // Example: [K-`] -> [K-`k-z] - .replace(/[A-Z]-\\?[[-`]/g, m => `${m}${m[0].toLowerCase()}-z`) + // Example: [K-`] -> [K-`k-z] + .replace(/[A-Z]-\\?[[-`]/g, m => `${m}${m[0].toLowerCase()}-z`) - // Example: [[-}] -> [[-}A-Z] - .replace(/\\?[[-`]-\\?[{-~]/g, m => `${m}A-Z`) + // Example: [[-}] -> [[-}A-Z] + .replace(/\\?[[-`]-\\?[{-~]/g, m => `${m}A-Z`) - // Example: [b-}] -> [b-}B-Z] - .replace(/[a-z]-\\?[{-~]/g, m => `${m}${m[0].toUpperCase()}-Z`) + // Example: [b-}] -> [b-}B-Z] + .replace(/[a-z]-\\?[{-~]/g, m => `${m}${m[0].toUpperCase()}-Z`) - // Example: [<-j] -> [<-z] - .replace(/\\?[ -@]-[a-z]/g, m => `${m[0]}-z`) + // Example: [<-j] -> [<-z] + .replace(/\\?[ -@]-[a-z]/g, m => `${m[0]}-z`) - // Example: [^-j] -> [A-J^-j] - .replace(/\\?[[-`]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}`); + // Example: [^-j] -> [A-J^-j] + .replace(/\\?[[-`]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}`); } } From 04036e001e3ca230bc19d3ed19d54c4ff17735ed Mon Sep 17 00:00:00 2001 From: n1073645 Date: Thu, 21 Nov 2019 12:13:34 +0000 Subject: [PATCH 6/8] Comments and linting for regex operation. --- src/core/operations/ToCaseInsensitiveRegex.mjs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/operations/ToCaseInsensitiveRegex.mjs b/src/core/operations/ToCaseInsensitiveRegex.mjs index 9467b8c8..5cb42d02 100644 --- a/src/core/operations/ToCaseInsensitiveRegex.mjs +++ b/src/core/operations/ToCaseInsensitiveRegex.mjs @@ -32,6 +32,7 @@ class ToCaseInsensitiveRegex extends Operation { * @returns {string} */ run(input, args) { + /** * Simulates look behind behaviour since javascript doesn't support it. * From 81d1007bb78db41e4581a8046976ae3ac2428b46 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Fri, 22 Nov 2019 10:45:02 +0000 Subject: [PATCH 7/8] Added tests for regex operation and a slight bug fix --- .../operations/ToCaseInsensitiveRegex.mjs | 8 +- .../tests/ToFromInsensitiveRegex.mjs | 132 ++++++++++++++++++ 2 files changed, 139 insertions(+), 1 deletion(-) diff --git a/src/core/operations/ToCaseInsensitiveRegex.mjs b/src/core/operations/ToCaseInsensitiveRegex.mjs index 5cb42d02..58b21ef5 100644 --- a/src/core/operations/ToCaseInsensitiveRegex.mjs +++ b/src/core/operations/ToCaseInsensitiveRegex.mjs @@ -51,11 +51,17 @@ class ToCaseInsensitiveRegex extends Operation { return result; } + try { + RegExp(input); + } catch (error) { + return "Invalid Regular Expression (Please note this version of node does not support look behinds)."; + } + // Example: [test] -> [[tT][eE][sS][tT]] return preProcess(input) // Example: [A-Z] -> [A-Za-z] - .replace(/[A-Z]-[A-Z]/ig, m => `${m[0].toUpperCase()}-${m[2].toUpperCase()}${m[0].toLowerCase()}-${m[2].toLowerCase()}`) + .replace(/([A-Z]-[A-Z]|[a-z]-[a-z])/g, m => `${m[0].toUpperCase()}-${m[2].toUpperCase()}${m[0].toLowerCase()}-${m[2].toLowerCase()}`) // Example: [H-d] -> [A-DH-dh-z] .replace(/[A-Z]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}${m[0].toLowerCase()}-z`) diff --git a/tests/operations/tests/ToFromInsensitiveRegex.mjs b/tests/operations/tests/ToFromInsensitiveRegex.mjs index b74e9973..f33d6706 100644 --- a/tests/operations/tests/ToFromInsensitiveRegex.mjs +++ b/tests/operations/tests/ToFromInsensitiveRegex.mjs @@ -53,4 +53,136 @@ TestRegister.addTests([ }, ], }, + { + name: "To Case Insensitive Regex: [A-Z] -> [A-Za-z]", + input: "[A-Z]", + expectedOutput: "[A-Za-z]", + recipeConfig: [ + { + op: "To Case Insensitive Regex", + args: [], + }, + ], + }, + { + name: "To Case Insensitive Regex: [a-z] -> [A-Za-z]", + input: "[a-z]", + expectedOutput: "[A-Za-z]", + recipeConfig: [ + { + op: "To Case Insensitive Regex", + args: [], + }, + ], + }, + { + name: "To Case Insensitive Regex: [H-d] -> [A-DH-dh-z]", + input: "[H-d]", + expectedOutput: "[A-DH-dh-z]", + recipeConfig: [ + { + op: "To Case Insensitive Regex", + args: [], + }, + ], + }, + { + name: "To Case Insensitive Regex: [!-D] -> [!-Da-d]", + input: "[!-D]", + expectedOutput: "[!-Da-d]", + recipeConfig: [ + { + op: "To Case Insensitive Regex", + args: [], + }, + ], + }, + { + name: "To Case Insensitive Regex: [%-^] -> [%-^a-z]", + input: "[%-^]", + expectedOutput: "[%-^a-z]", + recipeConfig: [ + { + op: "To Case Insensitive Regex", + args: [], + }, + ], + }, + { + name: "To Case Insensitive Regex: [K-`] -> [K-`k-z]", + input: "[K-`]", + expectedOutput: "[K-`k-z]", + recipeConfig: [ + { + op: "To Case Insensitive Regex", + args: [], + }, + ], + }, + { + name: "To Case Insensitive Regex: [[-}] -> [[-}A-Z]", + input: "[[-}]", + expectedOutput: "[[-}A-Z]", + recipeConfig: [ + { + op: "To Case Insensitive Regex", + args: [], + }, + ], + }, + { + name: "To Case Insensitive Regex: [b-}] -> [b-}B-Z]", + input: "[b-}]", + expectedOutput: "[b-}B-Z]", + recipeConfig: [ + { + op: "To Case Insensitive Regex", + args: [], + }, + ], + }, + { + name: "To Case Insensitive Regex: [<-j] -> [<-z]", + input: "[<-j]", + expectedOutput: "[<-z]", + recipeConfig: [ + { + op: "To Case Insensitive Regex", + args: [], + }, + ], + }, + { + name: "To Case Insensitive Regex: [^-j] -> [A-J^-j]", + input: "[^-j]", + expectedOutput: "[A-J^-j]", + recipeConfig: [ + { + op: "To Case Insensitive Regex", + args: [], + }, + ], + }, + { + name: "To Case Insensitive Regex: not simple test", + input: "Mozilla[A-Z0-9]+[A-Z]Mozilla[0-9whatA-Z][H-d][!-H][a-~](.)+", + expectedOutput: "[mM][oO][zZ][iI][lL][lL][aA][A-Za-z0-9]+[A-Za-z][mM][oO][zZ][iI][lL][lL][aA][0-9[wW][hH][aA][tT]A-Za-z][A-DH-dh-z][!-Ha-h][a-~A-Z](.)+", + recipeConfig: [ + { + op: "To Case Insensitive Regex", + args: [], + }, + ], + }, + { + name: "To Case Insensitive Regex: erroneous test", + input: "Mozilla[A-Z", + expectedOutput: "Invalid Regular Expression (Please note this version of node does not support look behinds).", + recipeConfig: [ + { + op: "To Case Insensitive Regex", + args: [], + }, + ], + }, ]); From 4814922e67728dd6157056308487784540a70048 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Fri, 22 Nov 2019 10:58:24 +0000 Subject: [PATCH 8/8] Linting for regex operation --- tests/operations/tests/ToFromInsensitiveRegex.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/operations/tests/ToFromInsensitiveRegex.mjs b/tests/operations/tests/ToFromInsensitiveRegex.mjs index f33d6706..1394bfe8 100644 --- a/tests/operations/tests/ToFromInsensitiveRegex.mjs +++ b/tests/operations/tests/ToFromInsensitiveRegex.mjs @@ -184,5 +184,5 @@ TestRegister.addTests([ args: [], }, ], - }, + } ]);