forked from denoland/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify.ts
More file actions
189 lines (174 loc) · 5.22 KB
/
stringify.ts
File metadata and controls
189 lines (174 loc) · 5.22 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import {
INFINITY_NEG,
INFINITY_POS,
NAN,
NULL,
UNDEFINED,
ZERO_NEG,
} from "./constants.ts";
import { HOLE } from "./constants.ts";
export type Stringifiers = Record<
string,
// deno-lint-ignore no-explicit-any
(value: any) => { value: any } | undefined
>;
/**
* Serializes the following:
*
* - `null`
* - `undefined`
* - `boolean`
* - `number`
* - `bigint`
* - `string`
* - `array`
* - `object` (no prototypes)
* - `Uint8Array`
* - `Date`
* - `RegExp`
* - `Set`
* - `Map`
*
* Circular references are supported and objects with the same reference are
* serialized only once.
*/
export function stringify(data: unknown, custom?: Stringifiers): string {
const out: string[] = [];
const indexes = new Map<unknown, number>();
const res = serializeInner(out, indexes, data, custom);
if (res < 0) {
return String(res);
}
return `[${out.join(",")}]`;
}
function serializeInner(
out: string[],
indexes: Map<unknown, number>,
value: unknown,
custom: Stringifiers | undefined,
): number {
const seenIdx = indexes.get(value);
if (seenIdx !== undefined) return seenIdx;
if (value === undefined) return UNDEFINED;
if (value === null) return NULL;
if (Number.isNaN(value)) return NAN;
if (value === Infinity) return INFINITY_POS;
if (value === -Infinity) return INFINITY_NEG;
if (value === 0 && 1 / value < 0) return ZERO_NEG;
const idx = out.length;
out.push("");
indexes.set(value, idx);
let str = "";
if (typeof value === "number") {
str += String(value);
} else if (typeof value === "boolean") {
str += String(value);
} else if (typeof value === "bigint") {
str += `["BigInt","${value}"]`;
} else if (typeof value === "string") {
str += JSON.stringify(value);
} else if (Array.isArray(value)) {
str += "[";
for (let i = 0; i < value.length; i++) {
if (i in value) {
str += serializeInner(out, indexes, value[i], custom);
} else {
str += HOLE;
}
if (i < value.length - 1) {
str += ",";
}
}
str += "]";
} else if (typeof value === "object") {
if (custom !== undefined) {
for (const k in custom) {
const fn = custom[k];
if (fn === undefined) continue;
const res = fn(value);
if (res === undefined) continue;
const innerIdx = serializeInner(out, indexes, res.value, custom);
str = `["${k}",${innerIdx}]`;
out[idx] = str;
return idx;
}
}
if (value instanceof Date) {
str += `["Date","${value.toISOString()}"]`;
} else if (value instanceof RegExp) {
str += `["RegExp",${JSON.stringify(value.source)}, "${value.flags}"]`;
} else if (value instanceof Uint8Array) {
str += `["Uint8Array","${b64encode(value.buffer as ArrayBuffer)}"]`;
} else if (value instanceof Set) {
const items = new Array(value.size);
let i = 0;
value.forEach((v) => {
items[i++] = serializeInner(out, indexes, v, custom);
});
str += `["Set",[${items.join(",")}]]`;
} else if (value instanceof Map) {
const items = new Array(value.size * 2);
let i = 0;
value.forEach((v, k) => {
items[i++] = serializeInner(out, indexes, k, custom);
items[i++] = serializeInner(out, indexes, v, custom);
});
str += `["Map",[${items.join(",")}]]`;
} else {
str += "{";
const keys = Object.keys(value);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
str += JSON.stringify(key) + ":";
// deno-lint-ignore no-explicit-any
str += serializeInner(out, indexes, (value as any)[key], custom);
if (i < keys.length - 1) {
str += ",";
}
}
str += "}";
}
} else if (typeof value === "function") {
throw new Error(`Serializing functions is not supported.`);
}
out[idx] = str;
return idx;
}
// deno-fmt-ignore
const base64abc = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d",
"e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "+", "/",
];
/**
* CREDIT: https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
* Encodes a given Uint8Array, ArrayBuffer or string into RFC4648 base64 representation
*/
export function b64encode(buffer: ArrayBuffer): string {
const uint8 = new Uint8Array(buffer);
let result = "",
i;
const l = uint8.length;
for (i = 2; i < l; i += 3) {
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
result += base64abc[((uint8[i - 1] & 0x0f) << 2) | (uint8[i] >> 6)];
result += base64abc[uint8[i] & 0x3f];
}
if (i === l + 1) {
// 1 octet yet to write
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[(uint8[i - 2] & 0x03) << 4];
result += "==";
}
if (i === l) {
// 2 octets yet to write
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
result += base64abc[(uint8[i - 1] & 0x0f) << 2];
result += "=";
}
return result;
}