-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (47 loc) · 1.43 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
// Largely taken from https://github.com/mathiasbynens/rot.
const rot = (charRot, numRot, str) => {
var numbers = "0123456789";
var lowercase = "abcdefghijklmnopqrstuvwxyz";
var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var regexNumber = /[0-9]/;
var regexLowercase = /[a-z]/;
var regexUppercase = /[A-Z]/;
str = String(str);
if (charRot < 0) {
charRot += 26;
}
if (numRot < 0) {
numRot += 10;
}
var length = str.length; // note: no need to account for astral symbols
var index = -1;
var result = "";
var character;
var currentPosition;
var shiftedPosition;
while (++index < length) {
character = str.charAt(index);
if (regexNumber.test(character)) {
currentPosition = numbers.indexOf(character);
shiftedPosition = (currentPosition + numRot) % 10;
result += numbers.charAt(shiftedPosition);
} else if (regexLowercase.test(character)) {
currentPosition = lowercase.indexOf(character);
shiftedPosition = (currentPosition + charRot) % 26;
result += lowercase.charAt(shiftedPosition);
} else if (regexUppercase.test(character)) {
currentPosition = uppercase.indexOf(character);
shiftedPosition = (currentPosition + charRot) % 26;
result += uppercase.charAt(shiftedPosition);
} else {
result += character;
}
}
return result;
};
const rot18 = (str) => rot(13, 5, str);
module.exports = {
rot,
encode: rot18,
decode: rot18,
};