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

Function stringify

lib.esm/utils/errors.js:12–56  ·  view source on GitHub ↗
(value, seen)

Source from the content-addressed store, hash-verified

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