-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
223 lines (205 loc) · 4.52 KB
/
index.ts
File metadata and controls
223 lines (205 loc) · 4.52 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
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
type Alphabet = {
[key: string]: string;
}
export class CarSlugger {
/**
* Alphabet needs to translate cyrillic letters to latin
*/
private dict: Alphabet = {
'а': 'a',
'б': 'b',
'в': 'v',
'г': 'g',
'д': 'd',
'е': 'e',
'ё': 'yo',
'ж': 'zh',
'з': 'z',
'и': 'i',
'й': 'i',
'к': 'k',
'л': 'l',
'м': 'm',
'н': 'n',
'о': 'o',
'п': 'p',
'р': 'r',
'с': 's',
'т': 't',
'у': 'u',
'ф': 'ph',
'х': 'h',
'ц': 'c',
'ч': 'ch',
'ш': 'sh',
'щ': 'sh',
'ъ': '',
'ы': 'y',
'ь': '',
'э': 'e',
'ю': 'u',
'я': 'ya',
' ': ' ',
};
/**
* Alphabet needs to translate latin letters to cyrillic
*/
private latinDict: Alphabet = {
'a': 'а',
'b': 'б',
'c': 'к',
'd': 'д',
'e': 'е',
'f': 'ф',
'g': 'г',
'h': 'х',
'i': 'и',
'j': 'дж',
'k': 'к',
'l': 'л',
'm': 'м',
'n': 'н',
'o': 'о',
'p': 'п',
'q': 'к',
'r': 'р',
's': 'с',
't': 'т',
'u': 'у',
'v': 'в',
'w': 'в',
'x': 'кс',
'y': 'й',
'z': 'з',
' ': ' ',
};
/**
* Just replace space to score
*
* @param text string
* @returns string
*/
private wordsToSlug(text: string): string {
return text.replaceAll(' ', '-');
};
/**
* Matches pairs like “4x4”,
* words contains lating letters,
* words contains cyrillic letters,
* any digit
*
* @example "LADA (ВАЗ) 2112 4x4" > {
* match #1: LADA
* match #2: ВАЗ
* match #3: 2112
* match #4: 4x4
* }
*/
private commonRegExp: RegExp = new RegExp(/([a-zA-Z]+\d+)|(\dx\d)|([a-zA-Z]+)|([а-яА-Я]+)|(\d+)/g);
/**
* Matches all cyrillic letters
*
* @example ЛАДА ВАЗ > {
* match #1: ЛАДА
* match #2: ВАЗ
* }
*/
private cyrillicRegExp: RegExp = new RegExp(/[а-яА-Я]+/);
private cyrillicUppercase: RegExp = new RegExp(/[А-Я]+/);
private latinRegExp: RegExp = new RegExp(/[a-zA-Z]+/);
private latinUppercase: RegExp = new RegExp(/[A-Z]+/);
/**
* Returns new sliced lowercase string
* Cyrillic symbols translited to lating.
*
* @param text string
* @returns string
*/
private parseString(text: string): string {
const words: string[] | null = text.match(this.commonRegExp);
let output = '';
words?.forEach((word: string, index: number, arr: any) => {
if (this.cyrillicRegExp.test(word)) {
for (let i = 0; i <= word.length; i++) {
if (i < word.length) output += this.dict[word[i].toLocaleLowerCase()] ?? '';
else output += arr.length === index + 1 ? '' : ' ';
};
} else {
const length = arr.length;
const trueIndex = index + 1;
if (trueIndex !== length && trueIndex > 1 || (trueIndex === 1 && length > 1)) {
output += word.toLowerCase() + ' ';
} else if (trueIndex === length) {
output += word.toLowerCase();
};
};
});
return output;
};
/**
* Easy translit cyrillic to latin
*
* @param text string
* @returns string
*/
public translateCyrillic(text: string): string {
if (this.cyrillicRegExp.test(text)) {
let output = '';
for (let i = 0; i <= text.length; i++) {
if (this.cyrillicUppercase.test(text[i])) output += this.dict[text[i].toLowerCase()].toUpperCase() ?? '';
else output += this.dict[text[i]] ?? '';
};
return output;
} else {
throw new Error("String don't matches [а-яА-Я] regexp;")
}
}
/**
* Easy translit latin to cyrillic
*
* @param text string
* @returns string
*/
public translateLatin(text: string): string {
if (this.latinRegExp.test(text)) {
let output = '';
for (let i = 0; i <= text.length; i++) {
if (this.latinUppercase.test(text[i])) output += this.latinDict[text[i].toLowerCase()].toUpperCase() ?? '';
else output += this.latinDict[text[i]] ?? '';
};
return output;
} else {
throw new Error("String don't matches [a-zA-Z] regexp;");
}
}
public translateCustom(text: string, dict: Alphabet): string {
let output = '';
for (let i = 0; i <= text.length; i++) {
output += dict[text[i]] ?? '';
}
return output;
}
public testString(string: string) {
console.debug('Input string: ', string);
console.debug('Output string: ', this.parseString(string));
console.debug('Output Slug: ',
this.wordsToSlug(
this.parseString(string)
)
);
};
/**
* Combined two functions
* Making slug from a simple and complex strings
*
* @param string string
* @returns string
*
* @example "LADA (ВАЗ) 2112 4x4" >>> "lada-vaz-2112-4x4"
*/
public getSlug(string: string): string {
return this.wordsToSlug(
this.parseString(string)
);
};
}