forked from JohnEarnest/ok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
60 lines (56 loc) · 1.32 KB
/
convert.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
////////////////////////////////////
//
// A companion to oK which bridges
// the gap between k-values and
// native JavaScript types.
//
// John Earnest
//
////////////////////////////////////
"use strict";
function tok(v) {
if (typeof v == 'number') {
return { t:0, v:v };
}
if (typeof v == 'string') {
var r = [];
for(var z=0;z<v.length;z++) { r[z] = { t:1, v:v.charCodeAt(z) }; }
return { t:3, v:r };
}
if (v instanceof Array) {
var r = [];
for(var z=0;z<v.length;z++) { r[z] = tok(v[z]); }
return { t:3, v:r };
}
if (typeof v == 'object') {
var r = {};
var k = Object.keys(v);
for(var z=0;z<k.length;z++) { r[k[z]] = tok(v[k[z]]); }
return { t:4, v:r };
}
throw new Error("cannot convert '"+v+"' to a K datatype.");
}
function tojs(v) {
if (v.t == 0) {
return v.v;
}
if (v.t == 1) {
return String.fromCharCode(v.v);
}
if (v.t == 3) {
var r = [];
var same = true;
for(var z=0;z<v.v.length;z++) { r[z] = tojs(v.v[z]); same &= v.v[z].t == v.v[0].t; }
if (same && v.v.length != 0 && v.v[0].t == 1) { return r.join(""); }
return r;
}
if (v.t == 4) {
var r = {};
var k = Object.keys(v.v);
for(var z=0;z<k.length;z++) { r[k[z]] = tojs(v.v[k[z]]); }
return r;
}
throw new Error("cannot convert '"+JSON.stringify(v)+"' to a JavaScript datatype.");
}
this.tok = tok;
this.tojs = tojs;