-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetSpeakTranslator.js
62 lines (56 loc) · 1.38 KB
/
leetSpeakTranslator.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
export function leetSpeakEncode(text, format) {
const leetDictionary = {
a: ["4", "@", "/-\\"],
e: ["3", "€", "[-"],
l: ["1", "|", "£"],
s: ["5", "$", "§"],
t: ["7", "+", "†"],
o: ["0", "()", "Ø"],
i: ["1", "!", "|"],
b: ["8", "|3", "ß"],
};
let leetText = text.toLowerCase();
for (const [key, value] of Object.entries(leetDictionary)) {
let replacement = value[0]; // Default to Basic
if (format === "Intermediate") replacement = value[1];
else if (format === "Advanced") replacement = value[2];
const regex = new RegExp(key, "g");
leetText = leetText.replace(regex, replacement);
}
return leetText;
}
export function leetSpeakDecode(text) {
const leetReverseDictionary = {
4: "a",
"@": "a",
"/-\\": "a",
3: "e",
"€": "e",
"[-": "e",
1: "l",
"|": "l",
"£": "l",
5: "s",
$: "s",
"§": "s",
7: "t",
"+": "t",
"†": "t",
0: "o",
"()": "o",
Ø: "o",
"!": "i",
8: "b",
"|3": "b",
ß: "b",
};
let decodedText = text;
for (const [leetChar, normalChar] of Object.entries(leetReverseDictionary)) {
const regex = new RegExp(
leetChar.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"),
"g"
);
decodedText = decodedText.replace(regex, normalChar);
}
return decodedText;
}