From d5b72548fc81f5e5b60c094b4bdd6dddae8b2c2a Mon Sep 17 00:00:00 2001 From: MikeCAT Date: Wed, 11 Jan 2023 05:48:05 +0900 Subject: [PATCH] add new operation: Swap case --- src/core/config/Categories.json | 1 + src/core/operations/SwapCase.mjs | 77 +++++++++++++++++++++++++++++ tests/operations/index.mjs | 1 + tests/operations/tests/SwapCase.mjs | 33 +++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 src/core/operations/SwapCase.mjs create mode 100644 tests/operations/tests/SwapCase.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 307270d2..80834d4d 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -245,6 +245,7 @@ "Remove null bytes", "To Upper case", "To Lower case", + "Swap case", "To Case Insensitive Regex", "From Case Insensitive Regex", "Add line numbers", diff --git a/src/core/operations/SwapCase.mjs b/src/core/operations/SwapCase.mjs new file mode 100644 index 00000000..e1fad730 --- /dev/null +++ b/src/core/operations/SwapCase.mjs @@ -0,0 +1,77 @@ +/** + * @author mikecat + * @copyright Crown Copyright 2023 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * Swap case operation + */ +class SwapCase extends Operation { + + /** + * SwapCase constructor + */ + constructor() { + super(); + + this.name = "Swap case"; + this.module = "Default"; + this.description = "Converts uppercase letters to lowercase ones, and lowercase ones to uppercase ones."; + this.infoURL = ""; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + let result = ""; + for (let i = 0; i < input.length; i++) { + const c = input.charAt(i); + const upper = c.toUpperCase(); + if (c === upper) { + result += c.toLowerCase(); + } else { + result += upper; + } + } + return result; + } + + /** + * Highlight Swap case + * + * @param {Object[]} pos + * @param {number} pos[].start + * @param {number} pos[].end + * @param {Object[]} args + * @returns {Object[]} pos + */ + highlight(pos, args) { + return pos; + } + + /** + * Highlight Swap case in reverse + * + * @param {Object[]} pos + * @param {number} pos[].start + * @param {number} pos[].end + * @param {Object[]} args + * @returns {Object[]} pos + */ + highlightReverse(pos, args) { + return pos; + } + +} + +export default SwapCase; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 7a3361f2..43c6a5dd 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -130,6 +130,7 @@ import "./tests/FletcherChecksum.mjs"; import "./tests/CMAC.mjs"; import "./tests/AESKeyWrap.mjs"; import "./tests/Rabbit.mjs"; +import "./tests/SwapCase.mjs"; // Cannot test operations that use the File type yet // import "./tests/SplitColourChannels.mjs"; diff --git a/tests/operations/tests/SwapCase.mjs b/tests/operations/tests/SwapCase.mjs new file mode 100644 index 00000000..2506fc44 --- /dev/null +++ b/tests/operations/tests/SwapCase.mjs @@ -0,0 +1,33 @@ +/** + * @author mikecat + * @copyright Crown Copyright 2023 + * @license Apache-2.0 + */ +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + "name": "Swap Case: basic example", + "input": "Hello, World!", + "expectedOutput": "hELLO, wORLD!", + "recipeConfig": [ + { + "op": "Swap case", + "args": [ + ], + }, + ], + }, + { + "name": "Swap Case: empty input", + "input": "", + "expectedOutput": "", + "recipeConfig": [ + { + "op": "Swap case", + "args": [ + ], + }, + ], + }, +]);