From 7c99b4f9995019e59ec8f96ffce692ce14e94bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=B6derlund?= Date: Mon, 16 Dec 2024 19:50:58 +0100 Subject: [PATCH] Fixed deep cloning of objects. --- src/lib/justClone.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/lib/justClone.ts b/src/lib/justClone.ts index f09e6dbc..99fa4486 100644 --- a/src/lib/justClone.ts +++ b/src/lib/justClone.ts @@ -29,17 +29,15 @@ export function clone(obj: T): T { // @ts-expect-error Known type return RegExp(obj.source, (obj as RegExp).flags); } - if (type == 'Array') { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const result = [] as any; + if (type == 'Array' || type == 'Object') { + const result = type == 'Object' ? Object.create(Object.getPrototypeOf(obj)) : []; + for (const key in obj) { result[key] = clone(obj[key]); } + return result; } - if (type == 'Object') { - return Object.assign(Object.create(Object.getPrototypeOf(obj)), obj); - } // primitives and non-supported objects (e.g. functions) land here return obj;