forked from WebReflection/flatted
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
292 lines (265 loc) · 8.69 KB
/
index.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
var TeleportJS = (function (Primitive, primitive) { // eslint-disable-line no-unused-vars
var REF_KEY_PREFIX = '_'
var SINGLE_REF = REF_KEY_PREFIX + '0'
var REF_PREFIX = {
undefined: 'u',
number: 'n',
bigint: 'b',
symbol: 's',
Map: 'M',
Set: 'S',
Date: 'D',
RegExp: 'R',
Buffer: 'B',
Int8Array: 'H',
Uint8Array: 'I',
Uint8ClampedArray: 'J',
Int16Array: 'P',
Uint16Array: 'Q',
Int32Array: 'F',
Uint32Array: 'G',
Float32Array: 'K',
Float64Array: 'L'
}
/*!
* ISC License
*
* Copyright (c) 2018, Andrea Giammarchi, @WebReflection
*/
var TeleportJS = {
parse: function parse (text, reviver) {
var input = JSON.parse(text, Primitives).map(primitives)
var len = input.length
var refs = len > 1 ? input[len - 1] : []
var value = input[0]
var $ = reviver || noop
var tmp = typeof value === 'object' && value
? revive(input, refs, new Set(), value, $)
: (value === SINGLE_REF && refs.length ? reviveRefs(refs, 0) : value)
return $.call({ '': tmp }, '', tmp)
},
stringify: function stringify (value, replacer, space) {
for (var
firstRun,
known = new Map(),
knownRefs = new Map(),
refs = [],
input = [],
output = [],
$ = replacer && typeof replacer === typeof input
? function (k, v) {
if (k === '' || replacer.indexOf(k) > -1) return v
}
: (replacer || noop),
i = +set(known, input, $.call({ '': value }, '', value)),
replace = function (key, value) {
var after = $.call(this, key, value)
var refIndex = setRefs(knownRefs, refs, key, after, this)
// bail out if value set via ref
if (refIndex) {
return refIndex
}
if (firstRun) {
firstRun = !firstRun
return value
// this was invoking twice each root object
// return i < 1 ? value : $.call(this, key, value);
}
switch (typeof after) {
case 'object':
if (after === null) return after
// eslint-disable-next-line no-fallthrough
case primitive:
return known.get(after) || set(known, input, after)
}
return after
};
i < input.length; i++
) {
firstRun = true
output[i] = JSON.stringify(input[i], replace, space)
}
refs.length && output.push(JSON.stringify(refs))
return '[' + output.join(',') + ']'
}
}
return TeleportJS
function noop (key, value) {
return value
}
function reviveRefs (refs, index) {
var value = refs[index].substring(1)
switch (refs[index].charAt(0)) {
case REF_PREFIX.undefined:
refs[index] = undefined
break
case REF_PREFIX.number:
refs[index] = Number(value)
break
case REF_PREFIX.bigint:
refs[index] = BigInt(value)
break
case REF_PREFIX.symbol:
refs[index] = Symbol.for(value)
break
case REF_PREFIX.RegExp:
var parts = /\/(.*)\/(.*)/.exec(value)
refs[index] = new RegExp(parts[1], parts[2])
break
case REF_PREFIX.Buffer:
refs[index] = Buffer.from(JSON.parse(value))
break
case REF_PREFIX.Date:
refs[index] = new Date(value)
break
case REF_PREFIX.Map:
refs[index] = new Map(TeleportJS.parse(value))
break
case REF_PREFIX.Set:
refs[index] = new Set(TeleportJS.parse(value))
break
case REF_PREFIX.Int8Array:
refs[index] = new Int8Array(JSON.parse(value))
break
case REF_PREFIX.Uint8Array:
refs[index] = new Uint8Array(JSON.parse(value))
break
case REF_PREFIX.Uint8ClampedArray:
refs[index] = new Uint8ClampedArray(JSON.parse(value))
break
case REF_PREFIX.Int16Array:
refs[index] = new Int16Array(JSON.parse(value))
break
case REF_PREFIX.Uint16Array:
refs[index] = new Uint16Array(JSON.parse(value))
break
case REF_PREFIX.Int32Array:
refs[index] = new Int32Array(JSON.parse(value))
break
case REF_PREFIX.Uint32Array:
refs[index] = new Uint32Array(JSON.parse(value))
break
case REF_PREFIX.Float32Array:
refs[index] = new Float32Array(JSON.parse(value))
break
case REF_PREFIX.Float64Array:
refs[index] = new Float64Array(JSON.parse(value))
break
}
return refs[index]
}
function revive (input, refs, parsed, output, $) {
return Object.keys(output).reduce(
function (output, key) {
var value = output[key]
if (value instanceof Primitive) {
if (value.startsWith(REF_KEY_PREFIX)) {
var index = value.substring(1)
if (refs[index] instanceof Primitive) {
reviveRefs(refs, index)
}
output[key] = refs[index]
return output
}
var tmp = input[value]
if (typeof tmp === 'object' && !parsed.has(tmp)) {
parsed.add(tmp)
output[key] = $.call(output, key, revive(input, refs, parsed, tmp, $))
} else {
output[key] = $.call(output, key, tmp)
}
} else { output[key] = $.call(output, key, value) }
return output
},
output
)
}
function set (known, input, value) {
var index = Primitive(input.push(value) - 1)
known.set(value, index)
return index
}
function setRefs (known, refs, key, value, obj) {
var after
var i
switch (typeof value) {
// case 'boolean': break
// case 'function': break
case 'string':
if (obj[key] instanceof Date) {
after = REF_PREFIX.Date + value
}
break
case 'undefined':
after = REF_PREFIX.undefined
break
case 'number':
if (!Number.isFinite(value)) {
after = REF_PREFIX.number + Primitive(value)
}
break
case 'bigint':
after = REF_PREFIX.bigint + Primitive(value)
break
case 'symbol':
var description = Primitive(value)
after = REF_PREFIX.symbol + description.substring(7, description.length - 1)
break
case 'object':
if (value === null) {
break
} else if (value.type === 'Buffer' && value.data && Buffer.isBuffer(obj[key])) {
after = REF_PREFIX.Buffer + JSON.stringify(value.data)
} else if (value instanceof RegExp) {
after = REF_PREFIX.RegExp + Primitive(value)
} else if (value instanceof Map) {
var m = []
for (i of value.entries()) m.push(i)
after = REF_PREFIX.Map + TeleportJS.stringify(m)
} else if (value instanceof Set) {
var s = []
for (i of value.values()) s.push(i)
after = REF_PREFIX.Set + TeleportJS.stringify(s)
} else if (value instanceof Int8Array) {
after = REF_PREFIX.Int8Array + JSON.stringify(Array.apply([], value))
} else if (value instanceof Uint8Array) {
after = REF_PREFIX.Uint8Array + JSON.stringify(Array.apply([], value))
} else if (value instanceof Uint8ClampedArray) {
after = REF_PREFIX.Uint8ClampedArray + JSON.stringify(Array.apply([], value))
} else if (value instanceof Int16Array) {
after = REF_PREFIX.Int16Array + JSON.stringify(Array.apply([], value))
} else if (value instanceof Uint16Array) {
after = REF_PREFIX.Uint16Array + JSON.stringify(Array.apply([], value))
} else if (value instanceof Int32Array) {
after = REF_PREFIX.Int32Array + JSON.stringify(Array.apply([], value))
} else if (value instanceof Uint32Array) {
after = REF_PREFIX.Uint32Array + JSON.stringify(Array.apply([], value))
} else if (value instanceof Float32Array) {
after = REF_PREFIX.Float32Array + JSON.stringify(Array.apply([], value))
} else if (value instanceof Float64Array) {
after = REF_PREFIX.Float64Array + JSON.stringify(Array.apply([], value))
}
break
}
if (!after) {
return
}
var index = known.get(after)
if (index) {
return index
}
index = REF_KEY_PREFIX + Primitive(refs.push(after) - 1)
known.set(after, index)
return index
}
// the two kinds of primitives
// 1. the real one
// 2. the wrapped one
function primitives (value) {
return value instanceof Primitive ? Primitive(value) : value
}
function Primitives (key, value) {
// eslint-disable-next-line valid-typeof
return typeof value === primitive ? new Primitive(value) : value
}
}(String, 'string'))