From d68c8cb845e7976623f35740de50eb4a5ec29a09 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Mon, 6 Jul 2020 10:43:52 +0100 Subject: [PATCH 1/2] Casing Variations --- src/core/config/Categories.json | 1 + src/core/operations/GetAllCasings.mjs | 53 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/core/operations/GetAllCasings.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 77e3d319..1bf0b68a 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -217,6 +217,7 @@ "From Case Insensitive Regex", "Add line numbers", "Remove line numbers", + "Get All Casings", "To Table", "Reverse", "Sort", diff --git a/src/core/operations/GetAllCasings.mjs b/src/core/operations/GetAllCasings.mjs new file mode 100644 index 00000000..33892ffc --- /dev/null +++ b/src/core/operations/GetAllCasings.mjs @@ -0,0 +1,53 @@ +/** + * @author n1073645 [n1073645@gmail.com] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * Permutate String operation + */ +class GetAllCasings extends Operation { + + /** + * GetAllCasings constructor + */ + constructor() { + super(); + + this.name = "Get All Casings"; + this.module = "Default"; + this.description = "Outputs all possible casing variations of a string."; + this.infoURL = ""; + this.inputType = "string"; + this.outputType = "string"; + this.args = []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const length = input.length; + const max = 1 << length; + input = input.toLowerCase(); + let result = ""; + + for (let i = 0; i < max; i++) { + const temp = input.split(""); + for (let j = 0; j < length; j++) { + if (((i >> j) & 1) === 1) { + temp[j] = temp[j].toUpperCase(); + } + } + result += temp.join("") + "\n"; + } + return result; + } +} + +export default GetAllCasings; From c01ce90e06db1d764f836282aa0e6693831230f5 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Mon, 6 Jul 2020 11:20:54 +0100 Subject: [PATCH 2/2] Tests Added --- tests/operations/index.mjs | 2 +- tests/operations/tests/GetAllCasings.mjs | 44 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/operations/tests/GetAllCasings.mjs diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 8d3cd623..33260005 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -101,7 +101,7 @@ import "./tests/LuhnChecksum.mjs"; import "./tests/CipherSaber2.mjs"; import "./tests/Colossus.mjs"; import "./tests/ParseObjectIDTimestamp.mjs"; - +import "./tests/GetAllCasings.mjs"; // Cannot test operations that use the File type yet // import "./tests/SplitColourChannels.mjs"; diff --git a/tests/operations/tests/GetAllCasings.mjs b/tests/operations/tests/GetAllCasings.mjs new file mode 100644 index 00000000..e5c6a25b --- /dev/null +++ b/tests/operations/tests/GetAllCasings.mjs @@ -0,0 +1,44 @@ +/** + * GetAllCasings tests. + * + * @author n1073645 [n1073645@gmail.com] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "All casings of test", + input: "test", + expectedOutput: "test\nTest\ntEst\nTEst\nteSt\nTeSt\ntESt\nTESt\ntesT\nTesT\ntEsT\nTEsT\nteST\nTeST\ntEST\nTEST\n", + recipeConfig: [ + { + "op": "Get All Casings", + "args": [] + } + ] + }, + { + name: "All casings of t", + input: "t", + expectedOutput: "t\nT\n", + recipeConfig: [ + { + "op": "Get All Casings", + "args": [] + } + ] + }, + { + name: "All casings of null", + input: "", + expectedOutput: "\n", + recipeConfig: [ + { + "op": "Get All Casings", + "args": [] + } + ] + } +]);