MCPcopy Create free account
hub / github.com/PageLeay/celestial-runtime / stringify

Function stringify

lib.commonjs/utils/errors.js:15–59  ·  view source on GitHub ↗
(value, seen)

Source from the content-addressed store, hash-verified

13const _version_js_1 = require("../_version.js");
14const properties_js_1 = require("./properties.js");
15function stringify(value, seen) {
16 if (value == null) {
17 return "null";
18 }
19 if (seen == null) {
20 seen = new Set();
21 }
22 if (typeof (value) === "object") {
23 if (seen.has(value)) {
24 return "[Circular]";
25 }
26 seen.add(value);
27 }
28 if (Array.isArray(value)) {
29 return "[ " + (value.map((v) => stringify(v, seen))).join(", ") + " ]";
30 }
31 if (value instanceof Uint8Array) {
32 const HEX = "0123456789abcdef";
33 let result = "0x";
34 for (let i = 0; i < value.length; i++) {
35 result += HEX[value[i] >> 4];
36 result += HEX[value[i] & 0xf];
37 }
38 return result;
39 }
40 if (typeof (value) === "object" && typeof (value.toJSON) === "function") {
41 return stringify(value.toJSON(), seen);
42 }
43 switch (typeof (value)) {
44 case "boolean":
45 case "number":
46 case "symbol":
47 return value.toString();
48 case "bigint":
49 return BigInt(value).toString();
50 case "string":
51 return JSON.stringify(value);
52 case "object": {
53 const keys = Object.keys(value);
54 keys.sort();
55 return "{ " + keys.map((k) => `${stringify(k, seen)}: ${stringify(value[k], seen)}`).join(", ") + " }";
56 }
57 }
58 return `[ COULD NOT SERIALIZE ]`;
59}
60/**
61 * Returns true if the %%error%% matches an error thrown by ethers
62 * that matches the error %%code%%.

Callers 1

makeErrorFunction · 0.70

Calls 6

addMethod · 0.45
isArrayMethod · 0.45
joinMethod · 0.45
mapMethod · 0.45
toJSONMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected