From e8913d152d2f79b5282956e972bf85b5acd9bba1 Mon Sep 17 00:00:00 2001 From: Edosicca <47154408+sichej@users.noreply.github.com> Date: Fri, 6 May 2022 14:52:18 +0200 Subject: [PATCH] Added new operation Inverting character case --- src/core/config/Categories.json | 6 +- .../operations/InvertingCharactersCase.mjs | 62 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/core/operations/InvertingCharactersCase.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 9540f8a3..b0bf802f 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -65,7 +65,8 @@ "JSON to CSV", "Avro to JSON", "CBOR Encode", - "CBOR Decode" + "CBOR Decode", + "Inverting characters case" ] }, { @@ -220,7 +221,8 @@ "Unicode Text Format", "Remove Diacritics", "Unescape Unicode Characters", - "Convert to NATO alphabet" + "Convert to NATO alphabet", + "Inverting characters case" ] }, { diff --git a/src/core/operations/InvertingCharactersCase.mjs b/src/core/operations/InvertingCharactersCase.mjs new file mode 100644 index 00000000..baf293c9 --- /dev/null +++ b/src/core/operations/InvertingCharactersCase.mjs @@ -0,0 +1,62 @@ +/** + * @author Sichej [edoardo.sichelli@gmail.com] + * @copyright Crown Copyright 2022 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; + +/** + * Inverting characters case operation + */ +class InvertingCharactersCase extends Operation { + + /** + * InvertingCharactersCase constructor + */ + constructor() { + super(); + + this.name = "Inverting characters case"; + this.module = "Default"; + this.description = "Inverting characters case vgHPCYbcyxnLnJqGBwvHBNmGC29TzxrOAw5NlG== becomes VGhpcyBCYXNlNjQgbWVhbnMgc29tZXRoaW5nLg=="; + this.infoURL = ""; + this.inputType = "string"; + this.outputType = "string"; + this.args = []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + var result = ""; + for(var i = 0; i < input.length; i++){ + result += this.checkAndChangeCase(input[i]); + } + return result; + } + + /** + * Check if a character is Uppercase or Lowercase + * and returns the opposite + */ + checkAndChangeCase(character){ + try { + if(character.charCodeAt(0) > 64 && character.charCodeAt(0) < 91) + return character.toLowerCase(); + else if (character.charCodeAt(0) > 96 && character.charCodeAt(0) < 123) + return character.toUpperCase(); + else + return character; + } catch (error) { + throw new OperationError("Error, something went wrong"); + } + } + +} + +export default InvertingCharactersCase;