Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloneDeep): maintain prototype when cloning class instances #794

Merged
merged 8 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions benchmarks/performance/cloneDeep.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const obj = {
object: { a: 1, b: 'es-toolkit' },
date: new Date(),
regex: /abc/g,
instance: new (class Test {
value: 1;
})(),
nested: { a: [1, 2, 3], b: { c: 'es-toolkit' }, d: new Date() },
nested2: { a: { b: { c: { d: { e: { f: { g: 'es-toolkit' } } } } } } },
};
Expand Down
24 changes: 24 additions & 0 deletions src/object/clone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,28 @@ describe('clone', () => {
expect(clonedError.message).toBe(error.message);
expect(clonedError.name).toBe(error.name);
});

it('should clone class instance', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test code is added in clone, not cloneDeep. Let me add a test case for cloneDeep, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There already is a should clone instance test in cloneDeep.spec.ts, so that's why I only added a single test covering this case.

class CustomClass {
value: number;

constructor(value: number) {
this.value = value;
}

getValue() {
return this.value;
}
}

const instance = new CustomClass(123);
const clonedInstance = clone(instance);

expect(clonedInstance).toEqual(instance);
expect(clonedInstance).not.toBe(instance);
expect(clonedInstance).toBeInstanceOf(CustomClass);

expect(clonedInstance.value).toBe(instance.value);
expect(clonedInstance.getValue()).toBe(123);
});
});
25 changes: 25 additions & 0 deletions src/object/cloneDeep.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ describe('cloneDeep', () => {
d,
c: 2,
});
expect(b.getA()).toEqual({ a: 'es-toolkit' });
});

//-------------------------------------------------------------------------------------
Expand Down Expand Up @@ -375,4 +376,28 @@ describe('cloneDeep', () => {
third: 3,
});
});

it('should clone class instance', () => {
class CustomClass {
value: number;

constructor(value: number) {
this.value = value;
}

getValue() {
return this.value;
}
}

const instance = new CustomClass(123);
const clonedInstance = cloneDeep(instance);

expect(clonedInstance).toEqual(instance);
expect(clonedInstance).not.toBe(instance);
expect(clonedInstance).toBeInstanceOf(CustomClass);

expect(clonedInstance.value).toBe(instance.value);
expect(clonedInstance.getValue()).toBe(123);
});
});
3 changes: 2 additions & 1 deletion src/object/cloneDeep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ function cloneDeepImpl<T>(obj: T, stack = new Map<any, any>()): T {
}

if (typeof obj === 'object' && obj !== null) {
const result = {};
const result = Object.create(Object.getPrototypeOf(obj));

stack.set(obj, result);

copyProperties(result, obj, stack);
Expand Down