MCPcopy Create free account
hub / github.com/liveblocks/liveblocks / stringify

Function stringify

tools/liveblocks-devtools/src/lib/stringify.ts:25–84  ·  view source on GitHub ↗
(
  value?: Json | YjsContentData | YjsContentData[],
  maxDepth = 2,
  depth = 0,
  seen = new WeakSet<
    JsonObject | Json[] | Exclude<YjsContentData, string | number | boolean>
  >()
)

Source from the content-addressed store, hash-verified

23type YjsContentData = number | object | boolean | Array<unknown> | string;
24
25export function stringify(
26 value?: Json | YjsContentData | YjsContentData[],
27 maxDepth = 2,
28 depth = 0,
29 seen = new WeakSet<
30 JsonObject | Json[] | Exclude<YjsContentData, string | number | boolean>
31 >()
32): string {
33 if (Array.isArray(value)) {
34 const isCircular = seen.has(value);
35
36 seen.add(value);
37
38 if (value.length === 0) {
39 return wrapArray();
40 } else if (depth >= maxDepth || isCircular) {
41 return wrapArray(ELLIPSIS);
42 } else {
43 const values = value
44 .map((value) => stringify(value, maxDepth, depth + 1, seen))
45 .join(SEPARATOR);
46
47 return wrapArray(values);
48 }
49 } else if (
50 value instanceof Boolean ||
51 value instanceof Number ||
52 value instanceof String
53 ) {
54 return JSON.stringify(value);
55 } else if (typeof value === "object" && value !== null) {
56 const keys = Object.keys(value);
57 const isCircular = seen.has(value);
58
59 seen.add(value);
60
61 if (keys.length === 0) {
62 return wrapObject();
63 } else if (depth >= maxDepth || isCircular) {
64 return wrapObject(ELLIPSIS);
65 } else {
66 const values = keys
67 .map((key) =>
68 wrapProperty(
69 key,
70 stringify(
71 value[key as keyof typeof value],
72 maxDepth,
73 depth + 1,
74 seen
75 )
76 )
77 )
78 .join(SEPARATOR);
79 return wrapObject(values);
80 }
81 } else {
82 return JSON.stringify(value);

Callers 7

summarizeFunction · 0.90
summarizeYTreeNodeFunction · 0.90
rawPostMethod · 0.85
postMethod · 0.85
sendMessagesFunction · 0.85
fetchAuthEndpointFunction · 0.85
dumpPoolFunction · 0.85

Calls 7

wrapArrayFunction · 0.85
wrapObjectFunction · 0.85
wrapPropertyFunction · 0.85
mapMethod · 0.80
hasMethod · 0.45
addMethod · 0.45
keysMethod · 0.45

Tested by

no test coverage detected