NATO phonetic alphabet encoder/decoder
⇔ DecodeSpell out text in NATO phonetic alphabet
$ echo "hello" | nato (input, opts = {}) => {
const alpha = {
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",
};
const digits = {
0: "Zero",
1: "One",
2: "Two",
3: "Three",
4: "Four",
5: "Five",
6: "Six",
7: "Seven",
8: "Eight",
9: "Niner",
};
if (opts.decode) {
const rev = {};
for (const [k, v] of Object.entries(alpha))
rev[v.toLowerCase()] = k.toLowerCase();
for (const [k, v] of Object.entries(digits)) rev[v.toLowerCase()] = k;
return input
.split("\n")
.map((line) =>
line
.split(/\s+/)
.map((w) => rev[w.toLowerCase()] ?? w)
.join(""),
)
.join("\n");
}
return input
.split("\n")
.map((line) =>
[...line]
.map((c) => {
const u = c.toUpperCase();
return alpha[u] || digits[c] || c;
})
.join(" "),
)
.join("\n");
}