-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiakode.js
56 lines (47 loc) · 1.53 KB
/
miakode.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
module.exports = {
object: {
encode(o = {}) {
let e = Object.keys(o).map((k) => `${k}\x01${o[k]}`).join('\x00').split('');
let m = null;
e.forEach((c) => { m = (!m || m > c) ? c.charCodeAt(0) : m; });
e = e.map((c) => String.fromCharCode(c.charCodeAt(0) - m)).join('');
return `${String.fromCharCode(m)}${e}`;
},
decode(s) {
const m = s[0].charCodeAt(0);
let e = s.substring(1).split('');
e = e.map((c) => String.fromCharCode(c.charCodeAt(0) + m)).join('');
const o = {};
e.split('\x00').forEach((l) => {
const p = l.split('\x01');
[, o[p[0]]] = p;
});
return o;
},
},
array: {
encode(a = []) {
let e = a.join('\x00').split('');
let m = null;
e.forEach((c) => { m = (!m || m > c) ? c.charCodeAt(0) : m; });
e = e.map((c) => String.fromCharCode(c.charCodeAt(0) - m)).join('');
return `${String.fromCharCode(m)}${e}`;
},
decode: (s) => s.substring(1).split('')
.map((c) => String.fromCharCode(c.charCodeAt(0) + s[0].charCodeAt(0)))
.join('')
.split('\x00'),
},
string: {
encode(s = '') {
let e = s.split('');
let m = null;
e.forEach((c) => { m = (!m || m > c) ? c.charCodeAt(0) : m; });
e = e.map((c) => String.fromCharCode(c.charCodeAt(0) - m)).join('');
return `${String.fromCharCode(m)}${e}`;
},
decode: (s = '') => s.substring(1).split('')
.map((c) => String.fromCharCode(c.charCodeAt(0) + s[0].charCodeAt(0)))
.join(''),
},
};