feat(new-tool): String to NATO alphabet

This commit is contained in:
Corentin Thomasset 2023-02-15 00:43:08 +01:00
parent 9634f5d9a8
commit 0ddf18f4b5
No known key found for this signature in database
GPG key ID: DBD997E935996158
5 changed files with 90 additions and 0 deletions

View file

@ -0,0 +1,19 @@
import { natoAlphabet } from './text-to-nato-alphabet.constants';
export { textToNatoAlphabet };
function getLetterPositionInAlphabet({ letter }: { letter: string }) {
return letter.toLowerCase().charCodeAt(0) - 'a'.charCodeAt(0);
}
function textToNatoAlphabet({ text }: { text: string }) {
return text
.split('')
.map((character) => {
const alphabetIndex = getLetterPositionInAlphabet({ letter: character });
const natoWord = natoAlphabet[alphabetIndex];
return natoWord ?? character;
})
.join(' ');
}