aboutsummaryrefslogtreecommitdiff
path: root/src/tools/text-to-nato-alphabet/text-to-nato-alphabet.service.ts
blob: 80290e83dd0d9f384234aa37859971db9eabca07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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(' ');
}