Skip to content

Commit

Permalink
Fixed deep cloning of objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
ciscoheat committed Dec 16, 2024
1 parent 9699b6b commit 7c99b4f
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/lib/justClone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ export function clone<T>(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;
Expand Down

0 comments on commit 7c99b4f

Please sign in to comment.