-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
120 lines (98 loc) · 2.56 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
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
function translate(text) {
text = text.toUpperCase();
var words
, j
, prefix
, suffix
, word
, translatedWords
;
words = text.split(' ');
translatedWords = [];
for (j in words) {
prefix = words[j].match(/^\W+/) || '';
suffix = words[j].match(/\W+$/) || '';
word = words[j].replace(prefix, '').replace(suffix, '');
if (word) {
translatedWords.push(prefix + _translateWord(word) + suffix);
} else {
translatedWords.push(words[j]);
}
}
return translatedWords.join(' ');
};
function _translateWord(word) {
// Don't translate short words
if (word.length == 1) {
return word;
}
// Handle specific words
switch (word) {
case 'AWESOME':
return 'ERSUM';
case 'BANANA':
return 'BERNERNER';
case 'BAYOU':
return 'BERU';
case 'FAVORITE':
case 'FAVOURITE':
return 'FRAVRIT';
case 'GOOSEBUMPS':
return 'GERSBERMS';
case 'LONG':
return 'LERNG';
case 'MY':
return 'MAH';
case 'THE':
return 'DA';
case 'THEY':
return 'DEY';
case 'WE\'RE':
return 'WER';
case 'YOU':
return 'U';
case 'YOU\'RE':
return 'YER';
}
// Before translating, keep a reference of the original word
var originalWord = word;
// Drop vowel from end of words
if (originalWord.length > 2) { // Keep it for short words, like "WE"
word = word.replace(/[AEIOU]$/, '');
}
// Reduce duplicate letters
word = word.replace(/[^\w\s]|(.)(?=\1)/gi, '');
// Reduce adjacent vowels to one
word = word.replace(/[AEIOUY]{2,}/g, 'E'); // TODO: Keep Y as first letter
// DOWN -> DERN
word = word.replace(/OW/g, 'ER');
// PANCAKES -> PERNKERKS
word = word.replace(/AKES/g, 'ERKS');
// The mean and potatoes: replace vowels with ER
word = word.replace(/[AEIOUY]/g, 'ER'); // TODO: Keep Y as first letter
// OH -> ER
word = word.replace(/ERH/g, 'ER');
// MY -> MAH
word = word.replace(/MER/g, 'MAH');
// FALLING -> FERLIN
word = word.replace('ERNG', 'IN');
// POOPED -> PERPERD -> PERPED
word = word.replace('ERPERD', 'ERPED');
// MEME -> MAHM -> MERM
word = word.replace('MAHM', 'MERM');
// Keep Y as first character
// YES -> ERS -> YERS
if (originalWord.charAt(0) == 'Y') {
word = 'Y' + word;
}
// Reduce duplicate letters
word = word.replace(/[^\w\s]|(.)(?=\1)/gi, '');
// YELLOW -> YERLER -> YERLO
if ((originalWord.substr(-3) == 'LOW') && (word.substr(-3) == 'LER')) {
word = word.substr(0, word.length - 3) + 'LO';
}
return word;
};
module.exports = {
translate : translate
};