it-tools/src/tools/text-to-nato-alphabet/text-to-nato-alphabet.service.ts

20 lines
545 B
TypeScript
Raw Normal View History

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(' ');
}