MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / walk

Function walk

packages/opencode-atomic-hooks/src/index.ts:18–50  ·  view source on GitHub ↗
(v: unknown, depth: number)

Source from the content-addressed store, hash-verified

16function 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 {

Callers 3

inspectFunction · 0.85
walkMethod · 0.85

Calls 2

hasMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected