15 lines
545 B
TypeScript
15 lines
545 B
TypeScript
/** Own a JSON-like input at the call boundary; never retain consumer objects. */
|
|
export function snapshotOrgChartDataset<T>(input: T): T {
|
|
const copy = structuredClone(input);
|
|
const pending: unknown[] = [copy];
|
|
const seen = new WeakSet<object>();
|
|
while (pending.length) {
|
|
const current = pending.pop();
|
|
if (current === null || typeof current !== 'object' || seen.has(current)) continue;
|
|
seen.add(current);
|
|
for (const child of Object.values(current)) pending.push(child);
|
|
Object.freeze(current);
|
|
}
|
|
return copy;
|
|
}
|