-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringifyBigInt.js
56 lines (52 loc) · 1.44 KB
/
stringifyBigInt.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
/*
* @file stringifyBigInt
* @summary Add ability to serialize BigInt as JSON
*/
JSON.stringifyBigInt = function (obj) {
return JSON.stringify(obj, (key, value) => {
if (typeof value === 'bigint') {
return value.toString() + 'n';
} else {
return value;
}
})
}
JSON.parseBigInt = function (str) {
return JSON.parse(str, (key, value) => {
if (typeof value === 'string' && /^-?\d+n$/.test(value)) {
return BigInt(value.slice(0, -1));
}
return value;
})
}
objAssign = function (to, from) {
if (window.Vue) {
for (let i in from) {
Vue.set(to, i, from[i]);
}
}
else {
Object.assign(to, from);
}
}
rpcToObj = function (rpc_obj, obj) {
if (!obj) {
obj = {};
}
for (let i in rpc_obj) {
if (isNaN(i)) {
// Not always correct, but overall useful
try {
obj[i] = isNaN(rpc_obj[i]) || i.indexOf("name") != -1 || i.indexOf("symbol") != -1
|| (typeof (rpc_obj[i]) == "boolean")
|| (typeof (rpc_obj[i]) == "string" && rpc_obj[i].startsWith("0x"))
|| (typeof (rpc_obj[i]) == "object")
? rpc_obj[i]
: BigInt(rpc_obj[i]);
} catch (e) {
console.log('pcToObj error', rpc_obj[i], typeof(rpc_obj[i]))
}
}
}
return obj;
}