(
value?: Json | YjsContentData | YjsContentData[],
maxDepth = 2,
depth = 0,
seen = new WeakSet<
JsonObject | Json[] | Exclude<YjsContentData, string | number | boolean>
>()
)
| 23 | type YjsContentData = number | object | boolean | Array<unknown> | string; |
| 24 | |
| 25 | export 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); |
no test coverage detected