2024-05-15 22:08:06 +02:00
|
|
|
import hangul from 'korean-unpacker';
|
feat(Text to NATO): add other languages, upper/lower and other
Add support for many languages and countries ((International), (France), (Belgium), (Switzerland), (Québec), (Germany, 2022), (Austria), (Germany, informal, 2022), (Netherlands), Italian, Spanish, (Brazil), (Portugal), Swedish, Danish, Norwegian, Finnish, Turkish, Romanian, Czech, Yugoslav, Serbian, Slovene, Russian, Korean, Greek, Japanese)
Handle uppercase/lowercase and some punctuations
Fix #794
2024-04-02 08:57:13 +02:00
|
|
|
import allAlphabets from './nato.alphabets.json';
|
2023-02-15 00:43:08 +01:00
|
|
|
|
2024-10-26 22:12:10 +02:00
|
|
|
type AllAlphabetsKeys = keyof typeof allAlphabets[0];
|
2023-02-15 00:43:08 +01:00
|
|
|
|
feat(Text to NATO): add other languages, upper/lower and other
Add support for many languages and countries ((International), (France), (Belgium), (Switzerland), (Québec), (Germany, 2022), (Austria), (Germany, informal, 2022), (Netherlands), Italian, Spanish, (Brazil), (Portugal), Swedish, Danish, Norwegian, Finnish, Turkish, Romanian, Czech, Yugoslav, Serbian, Slovene, Russian, Korean, Greek, Japanese)
Handle uppercase/lowercase and some punctuations
Fix #794
2024-04-02 08:57:13 +02:00
|
|
|
export { textToNatoAlphabet };
|
2023-02-15 00:43:08 +01:00
|
|
|
|
2024-10-26 22:12:10 +02:00
|
|
|
function isPunctuation(char: string) {
|
|
|
|
const punctuations = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
|
|
|
|
return punctuations.includes(char);
|
|
|
|
}
|
|
|
|
function isDigit(char: string) {
|
|
|
|
const digits = '0123456789';
|
|
|
|
return digits.includes(char);
|
|
|
|
}
|
|
|
|
|
|
|
|
function escapeRegExp(string: string) {
|
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
|
|
|
}
|
|
|
|
|
|
|
|
function textToNatoAlphabet({
|
|
|
|
text, langOrCountry = '(International)',
|
|
|
|
useDigitsNames = false, usePunctuationsNames = false,
|
|
|
|
}: {
|
|
|
|
text: string
|
|
|
|
langOrCountry: string
|
|
|
|
useDigitsNames?: boolean
|
|
|
|
usePunctuationsNames?: boolean
|
|
|
|
}) {
|
|
|
|
const getNatoWord = (searchChar: string) => {
|
|
|
|
const alphabetLetter = allAlphabets.find(letter => letter.Letter === searchChar);
|
|
|
|
if (alphabetLetter && alphabetLetter[langOrCountry as AllAlphabetsKeys]) {
|
|
|
|
return alphabetLetter[langOrCountry as AllAlphabetsKeys] || '';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
feat(Text to NATO): add other languages, upper/lower and other
Add support for many languages and countries ((International), (France), (Belgium), (Switzerland), (Québec), (Germany, 2022), (Austria), (Germany, informal, 2022), (Netherlands), Italian, Spanish, (Brazil), (Portugal), Swedish, Danish, Norwegian, Finnish, Turkish, Romanian, Czech, Yugoslav, Serbian, Slovene, Russian, Korean, Greek, Japanese)
Handle uppercase/lowercase and some punctuations
Fix #794
2024-04-02 08:57:13 +02:00
|
|
|
const charRegex = new RegExp(
|
|
|
|
`(${
|
|
|
|
allAlphabets
|
|
|
|
.sort((a, b) => b.Letter.length - a.Letter.length)
|
2024-10-26 22:12:10 +02:00
|
|
|
.filter(a => a[langOrCountry as AllAlphabetsKeys])
|
|
|
|
.map(a => escapeRegExp(a.Letter))
|
feat(Text to NATO): add other languages, upper/lower and other
Add support for many languages and countries ((International), (France), (Belgium), (Switzerland), (Québec), (Germany, 2022), (Austria), (Germany, informal, 2022), (Netherlands), Italian, Spanish, (Brazil), (Portugal), Swedish, Danish, Norwegian, Finnish, Turkish, Romanian, Czech, Yugoslav, Serbian, Slovene, Russian, Korean, Greek, Japanese)
Handle uppercase/lowercase and some punctuations
Fix #794
2024-04-02 08:57:13 +02:00
|
|
|
.join('|')
|
|
|
|
}|.)`,
|
|
|
|
'gi');
|
2024-05-15 22:08:06 +02:00
|
|
|
return hangul.unpack(text)
|
|
|
|
.replace(/\s+/g, ' ')
|
feat(Text to NATO): add other languages, upper/lower and other
Add support for many languages and countries ((International), (France), (Belgium), (Switzerland), (Québec), (Germany, 2022), (Austria), (Germany, informal, 2022), (Netherlands), Italian, Spanish, (Brazil), (Portugal), Swedish, Danish, Norwegian, Finnish, Turkish, Romanian, Czech, Yugoslav, Serbian, Slovene, Russian, Korean, Greek, Japanese)
Handle uppercase/lowercase and some punctuations
Fix #794
2024-04-02 08:57:13 +02:00
|
|
|
.replace(
|
|
|
|
charRegex,
|
|
|
|
(character) => {
|
|
|
|
const searchChar = character.toUpperCase();
|
|
|
|
const isUpper = character[0].toUpperCase() === character[0];
|
2024-10-26 22:12:10 +02:00
|
|
|
const natoWord = getNatoWord(searchChar);
|
|
|
|
|
|
|
|
if (isDigit(searchChar)) {
|
|
|
|
if (useDigitsNames) {
|
|
|
|
return ` {digit ${searchChar} => ${natoWord}}`;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return ` (digit ${character})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isPunctuation(searchChar)) {
|
|
|
|
if (usePunctuationsNames) {
|
|
|
|
return ` {punctuation ${searchChar} => ${natoWord}}`;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return ` (punctuation ${character})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (natoWord) {
|
2024-05-15 22:08:06 +02:00
|
|
|
return ` ${isUpper ? natoWord.toUpperCase() : natoWord.toLowerCase()}`;
|
feat(Text to NATO): add other languages, upper/lower and other
Add support for many languages and countries ((International), (France), (Belgium), (Switzerland), (Québec), (Germany, 2022), (Austria), (Germany, informal, 2022), (Netherlands), Italian, Spanish, (Brazil), (Portugal), Swedish, Danish, Norwegian, Finnish, Turkish, Romanian, Czech, Yugoslav, Serbian, Slovene, Russian, Korean, Greek, Japanese)
Handle uppercase/lowercase and some punctuations
Fix #794
2024-04-02 08:57:13 +02:00
|
|
|
}
|
2023-02-15 00:43:08 +01:00
|
|
|
|
2024-05-15 22:08:06 +02:00
|
|
|
return ` (${character})`;
|
|
|
|
})
|
|
|
|
.trim();
|
2023-02-15 00:43:08 +01:00
|
|
|
}
|