forked from vuejs/devtools-v6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer.js
88 lines (84 loc) · 2.29 KB
/
transfer.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
function encode (data, replacer, list, seen) {
var stored, key, value, i, l
var seenIndex = seen.get(data)
if (seenIndex != null) {
return seenIndex
}
var index = list.length
var proto = Object.prototype.toString.call(data)
if (proto === '[object Object]') {
stored = {}
seen.set(data, index)
list.push(stored)
var keys = Object.keys(data)
for (i = 0, l = keys.length; i < l; i++) {
key = keys[i]
value = data[key]
if (replacer) value = replacer.call(data, key, value)
stored[key] = encode(value, replacer, list, seen)
}
} else if (proto === '[object Array]') {
stored = []
seen.set(data, index)
list.push(stored)
for (i = 0, l = data.length; i < l; i++) {
value = data[i]
if (replacer) value = replacer.call(data, i, value)
stored[i] = encode(value, replacer, list, seen)
}
} else {
list.push(data)
}
return index
}
function decode (list, reviver) {
var i = list.length
var j, k, data, key, value, proto
while (i--) {
data = list[i]
proto = Object.prototype.toString.call(data)
if (proto === '[object Object]') {
var keys = Object.keys(data)
for (j = 0, k = keys.length; j < k; j++) {
key = keys[j]
value = list[data[key]]
if (reviver) value = reviver.call(data, key, value)
data[key] = value
}
} else if (proto === '[object Array]') {
for (j = 0, k = data.length; j < k; j++) {
value = list[data[j]]
if (reviver) value = reviver.call(data, j, value)
data[j] = value
}
}
}
}
export function stringify (data, replacer, space) {
try {
return arguments.length === 1
? JSON.stringify(data)
: JSON.stringify(data, replacer, space)
} catch (e) {
return stringifyStrict(data, replacer, space)
}
}
export function parse (data, reviver) {
var hasCircular = /^\s/.test(data)
if (!hasCircular) {
return arguments.length === 1
? JSON.parse(data)
: JSON.parse(data, reviver)
} else {
var list = JSON.parse(data)
decode(list, reviver)
return list[0]
}
}
export function stringifyStrict (data, replacer, space) {
var list = []
encode(data, replacer, list, new Map())
return space
? ' ' + JSON.stringify(list, null, space)
: ' ' + JSON.stringify(list)
}