-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (55 loc) · 1.13 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
var _= require('underscore');
module.exports = {
encode: function(message) {
return translateMessage(message, 'encode')
},
decode: function(coded_message) {
return translateMessage(coded_message, 'decode')
}
};
function translateMessage(message, direction) {
var msg = String(message).replace(/[^\w\s]+/g,'').toLowerCase();
for(i = 0; i < msg.length; i++) {
var newValue = getReplacementChar( msg[i], direction )
msg = setCharAt( msg, i, newValue);
}
return msg;
};
function getReplacementChar(character, direction) {
if (character == " ") {
return character;
};
return direction == 'encode' ? xref[character] : (_.invert(xref))[character];
};
function setCharAt(string, index, character) {
if ( index > string.length-1 ) { return string };
return string.substr(0, index) + character + string.substr(index + 1);
};
var xref = {
a: 'f',
b: 'y',
c: 'w',
d: 'p',
e: 'd',
f: 'a',
g: 'u',
h: 'r',
i: 'z',
j: 'q',
k: 't',
l: 'x',
m: 'g',
n: 'm',
o: 'h',
p: 'k',
q: 'b',
r: 'i',
s: 'j',
t: 'n',
u: 's',
v: 'o',
w: 'e',
x: 'v',
y: 'c',
z: 'l',
};