(v: unknown, depth: number)
| 16 | function inspect(value: unknown, maxStringLen = 500, maxDepth = 6): string { |
| 17 | const seen = new WeakSet(); |
| 18 | function walk(v: unknown, depth: number): unknown { |
| 19 | if (depth > maxDepth) return "[max depth]"; |
| 20 | if (v === null || v === undefined) return v; |
| 21 | if (typeof v === "string") |
| 22 | return v.length > maxStringLen |
| 23 | ? v.substring(0, maxStringLen) + `...[${v.length} chars]` |
| 24 | : v; |
| 25 | if (typeof v === "number" || typeof v === "boolean") return v; |
| 26 | if (typeof v === "function") return `[function ${v.name || "anonymous"}]`; |
| 27 | if (typeof v === "symbol") return v.toString(); |
| 28 | if (typeof v === "bigint") return v.toString(); |
| 29 | if (v instanceof Date) return v.toISOString(); |
| 30 | if (v instanceof RegExp) return v.toString(); |
| 31 | if (v instanceof Error) |
| 32 | return { name: v.name, message: v.message, stack: v.stack }; |
| 33 | if (typeof v === "object") { |
| 34 | if (seen.has(v as object)) return "[circular]"; |
| 35 | seen.add(v as object); |
| 36 | if (Array.isArray(v)) |
| 37 | return v.length > 50 |
| 38 | ? [ |
| 39 | ...v.slice(0, 50).map((x) => walk(x, depth + 1)), |
| 40 | `...[${v.length} items]`, |
| 41 | ] |
| 42 | : v.map((x) => walk(x, depth + 1)); |
| 43 | const out: Record<string, unknown> = {}; |
| 44 | for (const key of Object.keys(v as Record<string, unknown>)) { |
| 45 | out[key] = walk((v as Record<string, unknown>)[key], depth + 1); |
| 46 | } |
| 47 | return out; |
| 48 | } |
| 49 | return String(v); |
| 50 | } |
| 51 | try { |
| 52 | return JSON.stringify(walk(value, 0), null, 2); |
| 53 | } catch { |
no test coverage detected