( obj: unknown, space?: string | number, )
| 13 | * @returns JSON string with circular references replaced by [Circular] |
| 14 | */ |
| 15 | export function safeJsonStringify( |
| 16 | obj: unknown, |
| 17 | space?: string | number, |
| 18 | ): string { |
| 19 | const seen = new WeakSet(); |
| 20 | return JSON.stringify( |
| 21 | obj, |
| 22 | (key, value) => { |
| 23 | if (typeof value === 'object' && value !== null) { |
| 24 | if (seen.has(value)) { |
| 25 | return '[Circular]'; |
| 26 | } |
| 27 | seen.add(value); |
| 28 | } |
| 29 | return value; |
| 30 | }, |
| 31 | space, |
| 32 | ); |
| 33 | } |
no test coverage detected