Skip to content

Commit

Permalink
Tidied up 'Convert to NATO alphabet' operation
Browse files Browse the repository at this point in the history
  • Loading branch information
n1474335 committed Mar 13, 2020
1 parent 0fad891 commit 022ef71
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 129 deletions.
172 changes: 47 additions & 125 deletions src/core/operations/ConvertToNATOAlphabet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class ConvertToNATOAlphabet extends Operation {

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.description = "Converts characters to their representation in the NATO phonetic alphabet.";
this.infoURL = "https://wikipedia.org/wiki/NATO_phonetic_alphabet";
this.inputType = "string";
this.outputType = "string";
this.args = [];
Expand All @@ -31,130 +31,52 @@ class ConvertToNATOAlphabet extends Operation {
* @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;
return input.replace(/[a-z0-9,/.]/ig, letter => {
return lookup[letter.toUpperCase()];
});
}
}

const lookup = {
"A": "Alfa ",
"B": "Bravo ",
"C": "Charlie ",
"D": "Delta ",
"E": "Echo ",
"F": "Foxtrot ",
"G": "Golf ",
"H": "Hotel ",
"I": "India ",
"J": "Juliett ",
"K": "Kilo ",
"L": "Lima ",
"M": "Mike ",
"N": "November ",
"O": "Oscar ",
"P": "Papa ",
"Q": "Quebec ",
"R": "Romeo ",
"S": "Sierra ",
"T": "Tango ",
"U": "Uniform ",
"V": "Victor ",
"W": "Whiskey ",
"X": "X-ray ",
"Y": "Yankee ",
"Z": "Zulu ",
"0": "Zero ",
"1": "One ",
"2": "Two ",
"3": "Three ",
"4": "Four ",
"5": "Five ",
"6": "Six ",
"7": "Seven ",
"8": "Eight ",
"9": "Nine ",
",": "Comma ",
"/": "Fraction bar ",
".": "Full stop ",
};

export default ConvertToNATOAlphabet;
8 changes: 4 additions & 4 deletions tests/operations/tests/ConvertToNATOAlphabet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ TestRegister.addTests([
recipeConfig: [
{
op: "Convert to NATO alphabet",
args: [""]
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 ",
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 X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine Comma Fraction bar Full stop ",
recipeConfig: [
{
op: "Convert to NATO alphabet",
args: [""]
args: []
}
]
}
Expand Down

0 comments on commit 022ef71

Please # to comment.