14 lines
383 B
TypeScript
14 lines
383 B
TypeScript
export function safeStringify(obj) {
|
|
const seen = new WeakSet();
|
|
|
|
return JSON.stringify(obj, (key, value) => {
|
|
if (typeof value === "object" && value !== null) {
|
|
if (seen.has(value)) {
|
|
return `[Circular ${key}]`; // Label circular reference with the key
|
|
}
|
|
seen.add(value);
|
|
}
|
|
return value;
|
|
});
|
|
}
|