MCPcopy Create free account
hub / github.com/dipscope/TypeManager.TS / stringify

Function stringify

src/functions/json-stringify.ts:31–110  ·  view source on GitHub ↗
(parent: any, key: string, node: any, level: number)

Source from the content-addressed store, hash-verified

29 };
30
31 const stringify = (parent: any, key: string, node: any, level: number): string =>
32 {
33 if (node && node.toJSON && typeof node.toJSON === 'function')
34 {
35 node = node.toJSON();
36 }
37
38 if (replacer)
39 {
40 node = replacer.call(parent, key, node);
41 }
42
43 if (node === undefined)
44 {
45 return '';
46 }
47
48 if (node === null)
49 {
50 return 'null';
51 }
52
53 if (typeof node === 'number')
54 {
55 return isFinite(node) ? String(node) : 'null';
56 }
57
58 if (typeof node !== 'object')
59 {
60 return JSON.stringify(node);
61 }
62
63 if (seen.has(node))
64 {
65 return 'null';
66 }
67
68 seen.add(node);
69
70 const indent = spacing ? '\n' + indentify(level + 1) : '';
71 const closingIndent = spacing ? '\n' + indentify(level) : '';
72 const parts = new Array<string>();
73
74 if (Array.isArray(node))
75 {
76 for (let i = 0; i < node.length; i++)
77 {
78 const value = stringify(node, String(i), node[i], level + 1) || 'null';
79
80 parts.push(`${indent}${value}`);
81 }
82
83 const result = `[${parts.join(',')}${closingIndent}]`;
84
85 seen.delete(node);
86
87 return result;
88 }

Callers 1

jsonStringifyFunction · 0.85

Calls 3

indentifyFunction · 0.85
stringifyMethod · 0.80
sortMethod · 0.65

Tested by

no test coverage detected