-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
96 lines (88 loc) · 3.5 KB
/
Copy pathindex.ts
File metadata and controls
96 lines (88 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { mappingAlphabet } from './mapping.js';
export default function cyrillicToLatin(
input: string,
// Code names of languages according to ISO 639-2:1998
language?:
| 'iso9'
| 'alalc'
| 'aze'
| 'bel'
| 'bul'
| 'cnr'
| 'kaz'
| 'mkd'
| 'mon'
| 'rus'
| 'srp'
| 'tuk'
| 'ukr'
| 'uzb',
): string {
let newString = '';
// Bulgarian exception according to https://bg.wikisource.org/wiki/Закон_за_транслитерацията
// The letter combination "-ия" at the end of a word is spelled and transliterated by "-ia"
if (language === 'bul') {
// It's impossible to use simple regexp (/ия\b/gi, 'ia'),
// because word boundary \b doesn't work for non-latin alphabets
input = input.replace(/ия(?=$|[\s.,])/gi, 'ia');
}
// Ukrainian exceptions according to https://czo.gov.ua/en/translit
if (language === 'ukr') {
// Є, Ї, Й, Ю and Я at the beginning of words are used as "Ye", "Yi", "Y", "Yu" and "Ya"
input = input.replace(/(?<!\S)Є/g, 'Ye');
input = input.replace(/(?<!\S)є/g, 'ye');
input = input.replace(/(?<!\S)Ї/g, 'Yi');
input = input.replace(/(?<!\S)ї/g, 'yi');
input = input.replace(/(?<!\S)Й/g, 'Y');
input = input.replace(/(?<!\S)й/g, 'y');
input = input.replace(/(?<!\S)Ю/g, 'Yu');
input = input.replace(/(?<!\S)ю/g, 'yu');
input = input.replace(/(?<!\S)Я/g, 'Ya');
input = input.replace(/(?<!\S)я/g, 'ya');
// The letter combination "-зг" is reproduced in Latin as "-zgh" in contrast to "zh" - the counterpart of the letter "ж"
input = input.replace(/Зг/g, 'Zgh');
input = input.replace(/зг/g, 'zgh');
}
if (language === 'uzb') {
// Е at the beginning of words are used as "Ye"
input = input.replace(/(?<!\S)Е/g, 'Ye');
input = input.replace(/(?<!\S)е/g, 'ye');
}
for (const char of input) {
if (language === 'alalc') {
newString += mappingAlphabet.alalc[char] || char;
} else if (language === 'aze') {
newString += mappingAlphabet.azerbaijani[char] || char;
} else if (language === 'bul') {
// "-ия" exception was replaced with "-ia" in the previous condition;
// thus, Latin characters are not affected by this transformation
newString += mappingAlphabet.bulgarian[char] || char;
} else if (language === 'cnr') {
newString += mappingAlphabet.montenegrin[char] || char;
} else if (language === 'kaz') {
newString += mappingAlphabet.kazakh[char] || char;
} else if (language === 'mkd') {
newString += mappingAlphabet.macedonian[char] || char;
} else if (language === 'mon') {
newString += mappingAlphabet.mongolian[char] || char;
} else if (language === 'rus' || language === 'bel') {
newString += mappingAlphabet.russian[char] || char;
} else if (language === 'srp') {
newString += mappingAlphabet.serbian[char] || char;
} else if (language === 'tuk') {
newString += mappingAlphabet.turkmen[char] || char;
} else if (language === 'ukr') {
newString += mappingAlphabet.ukrainian[char] || char;
} else if (language === 'uzb') {
newString += mappingAlphabet.uzbek[char] || char;
} else {
newString += mappingAlphabet.iso9[char] || char;
}
}
if (language === 'rus' || language === 'bel') {
// GOST recommends to use C before the letters I, E, Y and J
newString = newString.replace(/[C]z([ieyj])/g, `C${'$1'}`);
newString = newString.replace(/[c]z([ieyj])/g, `c${'$1'}`);
}
return newString;
}