mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-11 16:41:36 -04:00
feat: handle option to handle digits and punctuations pronunciation
This commit is contained in:
parent
6db5c32bcd
commit
6cfaf63502
4 changed files with 3056 additions and 1691 deletions
|
@ -1,17 +1,46 @@
|
|||
import hangul from 'korean-unpacker';
|
||||
import allAlphabets from './nato.alphabets.json';
|
||||
|
||||
type AllALphabetsKeys = keyof typeof allAlphabets[0];
|
||||
type AllAlphabetsKeys = keyof typeof allAlphabets[0];
|
||||
|
||||
export { textToNatoAlphabet };
|
||||
|
||||
function textToNatoAlphabet({ text, langOrCountry = '(International)' }: { text: string; langOrCountry: string }) {
|
||||
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;
|
||||
};
|
||||
|
||||
const charRegex = new RegExp(
|
||||
`(${
|
||||
allAlphabets
|
||||
.sort((a, b) => b.Letter.length - a.Letter.length)
|
||||
.filter(a => a[langOrCountry as AllALphabetsKeys])
|
||||
.map(a => a.Letter)
|
||||
.filter(a => a[langOrCountry as AllAlphabetsKeys])
|
||||
.map(a => escapeRegExp(a.Letter))
|
||||
.join('|')
|
||||
}|.)`,
|
||||
'gi');
|
||||
|
@ -22,9 +51,26 @@ function textToNatoAlphabet({ text, langOrCountry = '(International)' }: { text:
|
|||
(character) => {
|
||||
const searchChar = character.toUpperCase();
|
||||
const isUpper = character[0].toUpperCase() === character[0];
|
||||
const alphabetLetter = allAlphabets.find(letter => letter.Letter === searchChar);
|
||||
if (alphabetLetter && alphabetLetter[langOrCountry as AllALphabetsKeys]) {
|
||||
const natoWord = alphabetLetter[langOrCountry as AllALphabetsKeys] || '';
|
||||
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) {
|
||||
return ` ${isUpper ? natoWord.toUpperCase() : natoWord.toLowerCase()}`;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue