-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathparse.ts
More file actions
133 lines (126 loc) · 3.83 KB
/
parse.ts
File metadata and controls
133 lines (126 loc) · 3.83 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {
HOLE,
INFINITY_NEG,
INFINITY_POS,
NAN,
NULL,
UNDEFINED,
ZERO_NEG,
} from "./constants.ts";
// deno-lint-ignore no-explicit-any
export type CustomParser = Record<string, (value: any) => unknown>;
export function parse<T = unknown>(
value: string,
custom?: CustomParser | undefined,
): T {
const data = JSON.parse(value);
if (!Array.isArray(data)) return unpack([], [], data, custom) as T;
const hydrated = new Array(data.length);
unpack(data, hydrated, 0, custom);
return hydrated[0];
}
function unpack(
arr: unknown[],
hydrated: unknown[],
idx: number,
custom: CustomParser | undefined,
): unknown {
switch (idx) {
case UNDEFINED:
return undefined;
case NULL:
return null;
case NAN:
return NaN;
case INFINITY_POS:
return Infinity;
case INFINITY_NEG:
return -Infinity;
case ZERO_NEG:
return -0;
}
if (idx in hydrated) return hydrated[idx];
const current = arr[idx];
if (typeof current === "number") {
return hydrated[idx] = current;
} else if (
typeof current === "string" || typeof current === "boolean" ||
current === null
) {
return hydrated[idx] = current;
} else if (Array.isArray(current)) {
if (current.length > 0 && typeof current[0] === "string") {
const name = current[0];
if (custom !== undefined && name in custom) {
const fn = custom[name];
const ref = current[1];
const value = unpack(arr, hydrated, ref, custom);
return hydrated[idx] = fn(value);
}
switch (name) {
case "BigInt":
return hydrated[idx] = BigInt(current[1]);
case "Date":
return hydrated[idx] = new Date(current[1]);
case "RegExp":
return hydrated[idx] = new RegExp(current[1], current[2]);
case "Set": {
const set = new Set();
for (let i = 0; i < current[1].length; i++) {
const ref = current[1][i];
set.add(unpack(arr, hydrated, ref, custom));
}
return hydrated[idx] = set;
}
case "Map": {
const set = new Map();
for (let i = 0; i < current[1].length; i++) {
const refKey = current[1][i++];
const key = unpack(arr, hydrated, refKey, custom);
const refValue = current[1][i];
const value = unpack(arr, hydrated, refValue, custom);
set.set(key, value);
}
return hydrated[idx] = set;
}
case "Uint8Array":
// TODO(iuioiua): use `Uint8Array.prototype.fromBase64()` once
// available in Deno (https://github.com/denoland/deno/issues/25051)
// and sufficiently supported in browsers
// (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/fromBase64#browser_compatibility)
return hydrated[idx] = decodeBase64(current[1]);
}
} else {
const actual = new Array(current.length);
hydrated[idx] = actual;
for (let i = 0; i < current.length; i++) {
const ref = current[i];
if (ref === HOLE) {
continue;
} else {
actual[i] = unpack(arr, hydrated, ref, custom);
}
}
return actual;
}
} else if (typeof current === "object") {
const actual: Record<string, unknown> = {};
hydrated[idx] = actual;
const keys = Object.keys(current);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
// deno-lint-ignore no-explicit-any
const ref = (current as any)[key];
actual[key] = unpack(arr, hydrated, ref, custom);
}
return actual;
}
}
function decodeBase64(base64: string): Uint8Array {
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes;
}