(obj: unknown, space?: string | number | undefined)
| 28 | const seen = new Set(); |
| 29 | // Serialize object to json, handling reference loops gracefully |
| 30 | export function toJson(obj: unknown, space?: string | number | undefined): string { |
| 31 | seen.clear(); |
| 32 | try { |
| 33 | return JSON.stringify( |
| 34 | obj, |
| 35 | (_key: string, value: unknown) => { |
| 36 | if (typeof value === 'object' && value !== null) { |
| 37 | if (seen.has(value)) { |
| 38 | return; |
| 39 | } |
| 40 | seen.add(value); |
| 41 | } |
| 42 | |
| 43 | if (value instanceof Error) { |
| 44 | return value.toString(); |
| 45 | } |
| 46 | |
| 47 | return value; |
| 48 | }, |
| 49 | space |
| 50 | ); |
| 51 | } finally { |
| 52 | seen.clear(); |
| 53 | } |
| 54 | } |
| 55 | // Clone object using serialization |
| 56 | |
| 57 | export function clone<T>(obj: T): T { |
no test coverage detected
searching dependent graphs…