-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaux.js
44 lines (32 loc) · 1015 Bytes
/
faux.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
const _dict = ["fox", "foox", "fooox", "faux", "foxx", "fooxx", "foxo", "foooox", "foxxxxx", "fauux"];
module.exports = class {
static translate(input, mode) {
if (!mode || mode === "auto") mode = this.checkEncode(input) ? "decode" : "encode";
return this[mode](input);
}
static checkEncode(input) {
return _dict.some((s) => input.startsWith(s) && this.decode(input).trim().length >= 1);
}
static encode(input) {
const inputArray = input.split("");
const output = [];
inputArray.map(letter => {
const encoded = [];
letter.charCodeAt(0).toString().split("").map(code => encoded.push(_dict[code]));
output.push(encoded.join(" "));
});
return output.join("! ");
}
static decode (input) {
const output = [];
const encodedMessage = input.split("! ");
encodedMessage.map(char => {
const decoded = [];
char.split(" ").map(id => {
decoded.push(_dict.indexOf(id));
});
output.push(String.fromCharCode(decoded.join("")));
});
return output.join("");
}
};