-
Notifications
You must be signed in to change notification settings - Fork 43
/
CyrillicToTranslit.js
265 lines (227 loc) · 7.05 KB
/
CyrillicToTranslit.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
'use strict';
module.exports = function cyrillicToTranslit(config) {
const invert = require('lodash.invert');
const _preset = config ? config.preset : "ru";
/*
ASSOCIATIONS FOR INITIAL POSITION
*/
// letters shared between languages
const _firstLetters = {
"а": "a",
"б": "b",
"в": "v",
"д": "d",
"з": "z",
"й": "y",
"к": "k",
"л": "l",
"м": "m",
"н": "n",
"о": "o",
"п": "p",
"р": "r",
"с": "s",
"т": "t",
"у": "u",
"ф": "f",
"ь": ""
};
// language-specific letters
if (_preset === "ru") {
Object.assign(_firstLetters, {
"г": "g",
"и": "i",
"ъ": "",
"ы": "i",
"э": "e",
});
} else if (_preset === "uk") {
Object.assign(_firstLetters, {
"г": "h",
"ґ": "g",
"е": "e",
"и": "y",
"і": "i",
"'": "",
"’": "",
"ʼ": "",
})
} else if (_preset === "mn") {
Object.assign(_firstLetters, {
"г": "g",
"ө": "o",
"ү": "u",
"и": "i",
"ы": "y",
"э": "e",
"ъ": ""
})
}
let _reversedFirstLetters;
if (_preset === "ru") {
// Russian: i > always и, y > й in initial position, e > э in initial position
_reversedFirstLetters = Object.assign(invert(_firstLetters), { "i": "и", "": "" });
} else if (_preset === "uk") {
// Ukrainian: i > always i, y > always и, e > always е
_reversedFirstLetters = Object.assign(invert(_firstLetters), { "": "" });
} else if (_preset === "mn") {
_reversedFirstLetters = Object.assign(invert(_firstLetters), { "": "" });
}
// digraphs appearing only in initial position
const _initialDigraphs = (_preset === "ru") ? { "е": "ye" } : { "є": "ye", "ї": "yi" };
// digraphs appearing in all positions
const _regularDigraphs = {
"ё": "yo",
"ж": "zh",
"х": "kh",
"ц": "ts",
"ч": "ch",
"ш": "sh",
"щ": "shch",
"ю": "yu",
"я": "ya",
}
const _firstDigraphs = Object.assign({}, _regularDigraphs, _initialDigraphs);
const _reversedFirstDigraphs = Object.assign(invert(_firstDigraphs));
const _firstAssociations = Object.assign(_firstLetters, _firstDigraphs);
/*
ASSOCIATIONS FOR NON-INITIAL POSITION
*/
const _nonFirstLetters = Object.assign({}, _firstLetters, { "й": "i" });
if (_preset === "ru") {
Object.assign(_nonFirstLetters, { "е": "e" });
} else if (_preset === "uk") {
Object.assign(_nonFirstLetters, { "ї": "i" });
} else if (_preset === "mn") {
Object.assign(_nonFirstLetters, { "е": "e" });
}
let _reversedNonFirstLetters;
if (_preset === "ru") {
// Russian: i > always и, y > ы in non-initial position, e > е in non-initial position
_reversedNonFirstLetters = Object.assign(invert(_firstLetters), {
"i": "и",
"y": "ы",
"e": "е",
"": ""
});
} else if (_preset === "uk") {
// Ukrainian: i > always i, y > always и, e > always е
_reversedNonFirstLetters = Object.assign(invert(_firstLetters), { "": "" });
}
// digraphs appearing only in non-initial positions
let _nonInitialDigraphs = {};
if (_preset === "uk") {
_nonInitialDigraphs = {
"є": "ie",
"ю": "iu",
"я": "ia",
};
}
const _nonFirstDigraphs = Object.assign(_regularDigraphs, _nonInitialDigraphs);
const _reversedNonFirstDigraphs = Object.assign(invert(_nonFirstDigraphs));
const _nonFirstAssociations = Object.assign(_nonFirstLetters, _nonFirstDigraphs);
function transform(input, spaceReplacement) {
if (!input) {
return "";
}
// We must normalize string for transform all unicode chars to uniform form
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
const normalizedInput = input.normalize();
let newStr = "";
let isWordBoundary = false;
for (let i = 0; i < normalizedInput.length; i++) {
const isUpperCaseOrWhatever = normalizedInput[i] === normalizedInput[i].toUpperCase();
let strLowerCase = normalizedInput[i].toLowerCase();
if (strLowerCase === " ") {
newStr += spaceReplacement ? spaceReplacement : " ";
isWordBoundary = true;
continue;
}
let newLetter;
if ( _preset === "uk" && normalizedInput.slice(i-1, i+1).toLowerCase() === "зг") {
// handle ukrainian special case зг > zgh
newLetter = "gh";
} else if (i === 0 || isWordBoundary) {
newLetter = _firstAssociations[strLowerCase];
isWordBoundary = false;
} else {
newLetter = _nonFirstAssociations[strLowerCase];
}
if ("undefined" === typeof newLetter) {
newStr += isUpperCaseOrWhatever ? strLowerCase.toUpperCase() : strLowerCase;
} else if (isUpperCaseOrWhatever) {
// handle multi-symbol letters
newLetter.length > 1
? newStr += newLetter[0].toUpperCase() + newLetter.slice(1)
: newStr += newLetter.toUpperCase();
} else {
newStr += newLetter;
}
}
return newStr;
}
function reverse(input, spaceReplacement) {
if (!input) return "";
const normalizedInput = input.normalize();
let newStr = "";
let isWordBoundary = false;
let i = 0;
while (i < normalizedInput.length) {
const isUpperCaseOrWhatever = normalizedInput[i] === normalizedInput[i].toUpperCase();
let strLowerCase = normalizedInput[i].toLowerCase();
let currentIndex = i;
if (strLowerCase === " " || strLowerCase === spaceReplacement) {
newStr += " ";
isWordBoundary = true;
i++;
continue;
}
let newLetter;
let digraph = normalizedInput.slice(i, i + 2).toLowerCase();
if (i === 0 || isWordBoundary) {
newLetter = _reversedFirstDigraphs[digraph];
if (newLetter) {
i += 2;
} else {
newLetter = _reversedFirstLetters[strLowerCase];
i++;
}
isWordBoundary = false;
} else {
newLetter = _reversedNonFirstDigraphs[digraph];
if (newLetter) {
i += 2;
} else {
newLetter = _reversedNonFirstLetters[strLowerCase];
i++;
}
}
// special cases: щ and зг
if (normalizedInput.slice(currentIndex, currentIndex + 4).toLowerCase() === "shch") {
newLetter = "щ";
i = currentIndex + 4;
} else if (normalizedInput.slice(currentIndex - 1, currentIndex + 2).toLowerCase() === "zgh") {
newLetter = "г";
i = currentIndex + 2;
}
if ("undefined" === typeof newLetter) {
newStr += isUpperCaseOrWhatever ? strLowerCase.toUpperCase() : strLowerCase;
}
else {
if (isUpperCaseOrWhatever) {
// handle multi-symbol letters
newLetter.length > 1
? newStr += newLetter[0].toUpperCase() + newLetter.slice(1)
: newStr += newLetter.toUpperCase();
} else {
newStr += newLetter;
}
}
}
return newStr;
}
return {
transform: transform,
reverse: reverse
};
};