-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
87 lines (83 loc) · 2.37 KB
/
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
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
const config = require('./bin/config');
exports.getSwitch = function(str, settings) {
let type = (settings && settings.hasOwnProperty('type')) ? settings.type : false;
switch (type) {
case "rueng":
{
config.default = config.dictionary.keys;
break;
}
case "engru":
{
config.default = _flip(config.dictionary.keys);
break;
}
case "translit":
{
config.default = config.dictionary.translit;
break;
}
case "retranslit":
{
config.default = _flip(config.dictionary.translit);
str = _fix(str);
break;
}
case "custom":
{
config.custom = true;
config.dictionary.retranslit = settings.input;
str = _fix(str);
break;
}
default:
{
config.default = _flip(config.dictionary.keys);
break;
}
}
let textToArray = str.split(''), result = [];
if (!config.custom) {
let obj = config.default;
if (settings && settings.hasOwnProperty('input') && settings.input) {
obj = Object.assign(obj, settings.input);
}
textToArray.forEach(function(sym, i) {
if (obj.hasOwnProperty(textToArray[i])) {
result.push(obj[textToArray[i]]);
} else {
result.push(sym);
}
});
if (settings && settings.hasOwnProperty('normal') && settings.normal) {
return _normalize(result.join(''));
} else {
return result.join('');
}
} else {
return str;
}
};
const _normalize = function(str) {
str = str.replace(/(Ю\s|Б\s|Ь\s)/g, (s) => {
return config.words[s];
}).replace(/\s{2,}/g, ' ').trim();
return str;
};
const _fix = function(str) {
let obj = config.dictionary.retranslit;
Object.keys(obj).map(function(key, v) {
let reg = new RegExp('(' + key + ')', 'g');
str = str.replace(reg, (s) => {
return obj[s];
})
});
return str;
};
const _flip = function(trans) {
let key, tmp = {};
for (key in trans) {
tmp[trans[key]] = key;
}
return tmp;
};