diff --git a/src/utils.ts b/src/utils.ts index 7df1c77672b..ab62a0f3c4e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -625,39 +625,18 @@ export function groupBy( ); } -function cloneArray(arr: T[]): T[] { - return arr.map((e) => cloneDeep(e)); -} - -function cloneObject>(obj: T): T { - const clone: Record = {}; - for (const [k, v] of Object.entries(obj)) { - clone[k] = cloneDeep(v); - } - return clone as T; -} - /** * replacement for lodash cloneDeep that preserves type. */ -// TODO: replace with builtin once Node 18 becomes the min version. export function cloneDeep(obj: T): T { - if (typeof obj !== "object" || !obj) { + if (obj === undefined) { return obj; } - if (obj instanceof RegExp) { - return RegExp(obj, obj.flags) as typeof obj; - } - if (obj instanceof Date) { - return new Date(obj) as typeof obj; - } - if (Array.isArray(obj)) { - return cloneArray(obj) as typeof obj; - } - if (obj instanceof Map) { - return new Map(obj.entries()) as typeof obj; + try { + return structuredClone(obj); + } catch (e) { + return obj; } - return cloneObject(obj as Record) as typeof obj; } /**