-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPhoneticConverter.js
43 lines (41 loc) · 1.15 KB
/
PhoneticConverter.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
module.exports = function convertToPhonetics(orgStr, extraLength, languageMapString) {
if (!extraLength) {
extraLength = 0.8;
}
if (!languageMapString) {
languageMapString = '';
}
if (typeof orgStr !== 'string') {
return orgStr;
}
if (!orgStr) {
return '';
}
let PseudoLangMap = {};
try{
PseudoLangMap = JSON.parse(languageMapString);
}
catch (e){
console.error(e, "Failed to parse languageMapString");
}
function addExtraCharacters(str, extraLength) {
let strLen = Math.floor(extraLength * str.length);
let appendToStr = '';
while (strLen > 0) {
appendToStr = appendToStr + '!';
strLen--;
}
return appendToStr;
}
const origStrArr = orgStr.split('');
const phoneticArr = [];
origStrArr.forEach((char) => {
if (char in PseudoLangMap) {
const phoneticChar = PseudoLangMap[char];
phoneticArr.push(phoneticChar);
} else {
phoneticArr.push(char);
}
});
return phoneticArr.join('') + addExtraCharacters(orgStr, extraLength);
}