diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 93d3d3bc..5957c8b7 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -201,7 +201,8 @@ "Encode text", "Decode text", "Remove Diacritics", - "Unescape Unicode Characters" + "Unescape Unicode Characters", + "Convert to NATO alphabet" ] }, { diff --git a/src/core/operations/ConvertToNATOAlphabet.mjs b/src/core/operations/ConvertToNATOAlphabet.mjs new file mode 100644 index 00000000..60ab5298 --- /dev/null +++ b/src/core/operations/ConvertToNATOAlphabet.mjs @@ -0,0 +1,160 @@ +/** + * @author MarvinJWendt [git@marvinjwendt.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * Convert to NATO alphabet operation + */ +class ConvertToNATOAlphabet extends Operation { + /** + * ConvertToNATOAlphabet constructor + */ + constructor() { + super(); + + this.name = "Convert to NATO alphabet"; + this.module = "Default"; + this.description = "Convert a text to NATO alphabet."; + this.infoURL = "https://en.wikipedia.org/wiki/NATO_phonetic_alphabet"; + this.inputType = "string"; + this.outputType = "string"; + this.args = []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + let result = ""; + + const text = input.toUpperCase(); + + for (let i = 0; i < text.length; i++) { + switch (text.charAt(i)) { + case "A": + result += "alfa "; + break; + case "B": + result += "bravo "; + break; + case "C": + result += "charlie "; + break; + case "D": + result += "delta "; + break; + case "E": + result += "echo "; + break; + case "F": + result += "foxtrot "; + break; + case "G": + result += "golf "; + break; + case "H": + result += "hotel "; + break; + case "I": + result += "india "; + break; + case "J": + result += "juliett "; + break; + case "K": + result += "kilo "; + break; + case "L": + result += "lima "; + break; + case "M": + result += "mike "; + break; + case "N": + result += "november "; + break; + case "O": + result += "oscar "; + break; + case "P": + result += "papa "; + break; + case "Q": + result += "quebec "; + break; + case "R": + result += "romeo "; + break; + case "S": + result += "sierra "; + break; + case "T": + result += "tango "; + break; + case "U": + result += "uniform "; + break; + case "V": + result += "victor "; + break; + case "W": + result += "whiskey "; + break; + case "X": + result += "xray "; + break; + case "Y": + result += "yankee "; + break; + case "Z": + result += "zulu "; + break; + case " ": + result += " "; + break; + case "0": + result += "zero "; + break; + case "1": + result += "one "; + break; + case "2": + result += "two "; + break; + case "3": + result += "three "; + break; + case "4": + result += "four "; + break; + case "5": + result += "five "; + break; + case "6": + result += "six "; + break; + case "7": + result += "seven "; + break; + case "8": + result += "eight "; + break; + case "9": + result += "nine "; + break; + default: + result += text.charAt(i) + " "; + } + } + + return result; + } +} + +export default ConvertToNATOAlphabet; diff --git a/tests/operations/tests/ConvertToNATOAlphabet.mjs b/tests/operations/tests/ConvertToNATOAlphabet.mjs new file mode 100644 index 00000000..50d98426 --- /dev/null +++ b/tests/operations/tests/ConvertToNATOAlphabet.mjs @@ -0,0 +1,32 @@ +/** + * @author MarvinJWendt [git@marvinjwendt.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "Convert to NATO alphabet: nothing", + input: "", + expectedOutput: "", + recipeConfig: [ + { + op: "Convert to NATO alphabet", + args: [""] + } + ] + }, + { + name: "Convert to NATO alphabet: full alphabet with numbers", + input: "abcdefghijklmnopqrstuvwxyz0123456789", + expectedOutput: "alfa bravo charlie delta echo foxtrot golf hotel india juliett kilo lima mike november oscar papa quebec romeo sierra tango uniform victor whiskey xray yankee zulu zero one two three four five six seven eight nine ", + recipeConfig: [ + { + op: "Convert to NATO alphabet", + args: [""] + } + ] + } +]);