Skip to content

Commit 7c99b4f

Browse files
committed
Fixed deep cloning of objects.
1 parent 9699b6b commit 7c99b4f

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/lib/justClone.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@ export function clone<T>(obj: T): T {
2929
// @ts-expect-error Known type
3030
return RegExp(obj.source, (obj as RegExp).flags);
3131
}
32-
if (type == 'Array') {
33-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
34-
const result = [] as any;
32+
if (type == 'Array' || type == 'Object') {
33+
const result = type == 'Object' ? Object.create(Object.getPrototypeOf(obj)) : [];
34+
3535
for (const key in obj) {
3636
result[key] = clone(obj[key]);
3737
}
38+
3839
return result;
3940
}
40-
if (type == 'Object') {
41-
return Object.assign(Object.create(Object.getPrototypeOf(obj)), obj);
42-
}
4341

4442
// primitives and non-supported objects (e.g. functions) land here
4543
return obj;

0 commit comments

Comments
 (0)