-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrot8000.js
64 lines (50 loc) · 2.08 KB
/
rot8000.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
function rot8000()
{
// these come from the valid-code-point-transitions.json file generated from the c# proj
// this is done bc: 1) don't trust JS's understanging of surrogate pairs and 2) consistency with original rot8000
const valid_code_points =
JSON.parse('{"33":true,"127":false,"161":true,"5760":false,"5761":true,"8192":false,"8203":true,"8232":false,"8234":true,"8239":false,"8240":true,"8287":false,"8288":true,"12288":false,"12289":true,"55296":false,"57344":true}');
const BMP_SIZE = 0x10000;
this.rotlist = {}; // the mapping of char to rotated char
hiddenblocks = [];
var startblock = 0;
for (var key in valid_code_points) {
if (valid_code_points.hasOwnProperty(key)) {
if (valid_code_points[key] == true)
hiddenblocks.push({ start: startblock, end: parseInt(key) - 1 });
else
startblock = parseInt(key);
}
}
var validintlist = []; // list of all valid chars
var currvalid = false;
for (var i = 0; i < BMP_SIZE; i++) {
if (valid_code_points[i] !== undefined) {
currvalid = valid_code_points[i]
}
if (currvalid) validintlist.push(i);
}
var rotatenum = Object.keys(validintlist).length / 2;
// go through every valid char and find its match
for (var i = 0; i < validintlist.length; i++) {
this.rotlist[String.fromCharCode(validintlist[i])] =
String.fromCharCode(validintlist[(i + rotatenum) % (rotatenum * 2)]);
}
this.rotate = function(convstring)
{
var outstring = "";
for (var count = 0; count < convstring.length; count++)
{
// if it is not in the mappings list, just add it directly (no rotation)
if (this.rotlist[convstring[count]] === undefined)
{
outstring += convstring[count];
continue;
}
// otherwise, rotate it and add it to the string
outstring += this.rotlist[convstring[count]];
}
return outstring;
}
}
module.exports = {rot8000};