-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.ts
16 lines (14 loc) · 622 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import type { ParsedElementInfo } from "@streamparser/json/utils/types/parsedElementInfo.js";
export function cloneParsedElementInfo(
parsedElementInfo: ParsedElementInfo,
): ParsedElementInfo {
const { value, key, parent, stack } = parsedElementInfo;
return { value, key, parent: clone(parent), stack: clone(stack) };
}
function clone<T>(obj: T): T {
// Only objects are passed by reference and must be cloned
if (typeof obj !== "object") return obj;
// Solve arrays with empty positions
if (Array.isArray(obj) && obj.filter((i) => i).length === 0) return obj;
return JSON.parse(JSON.stringify(obj));
}