forked from phanan/tieqviet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (41 loc) · 851 Bytes
/
index.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
'use strict';
const maps = [
['kh', 'x'],
['c(?!h)', 'k'],
['q', 'k'],
['tr', 'c'],
['ch', 'c'],
['d', 'z'],
['gi', 'z'],
['r', 'z'],
['đ', 'd'],
['ph', 'f'],
['ngh?', 'q'],
['gh', 'g'],
['th', 'w'],
['nh', 'n\'']
];
/**
* Capitalize the first letter in a string.
* @param {*} string
*/
const capitalize = string => `${string.charAt(0).toUpperCase()}${string.slice(1)}`;
/**
* Convert using a single map element.
* @param {*} input
* @param {*} map
*/
const convert = (input, map) => {
return input
.replace(new RegExp(map[0], 'g'), map[1])
.replace(new RegExp(capitalize(map[0]), 'g'), capitalize(map[1]));
};
module.exports = input => {
if (typeof input !== 'string') {
throw new TypeError(`Expected a string, got ${typeof input}`);
}
maps.forEach(map => {
input = convert(input, map);
});
return input;
};