toss / es-toolkit

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.
https://es-toolkit.slash.page
Other
7.1k stars 329 forks source link

Bug: cloneDeep does not respect instances of classes (affects compat/omit and compat/pick) #801

Closed eturino closed 2 weeks ago

eturino commented 2 weeks ago

this happens with both cloneDeep and compat.cloneDeep.

It is then affecting other functions like omit.

it('should work with custom classes', () => {
    class CustomTest {
      constructor(public value: number) {}
      myFunc() {
        return `value is ${this.value}`;
      }
    }

    const object = new CustomTest(1);
    const cloned = cloneDeep(object);

    expect(cloned).not.toBe(object);
    expect(cloned).toEqual(object);
    expect(cloned.myFunc()).toBe(object.myFunc()); // <- Error: TypeError: cloned.myFunc is not a function
    expect(cloned.myFunc()).toEqual('value is 1');

    const nested = { customTest: new CustomTest(1) };

    const clonedNested = cloneDeep(nested);

    expect(clonedNested).not.toBe(nested);
    expect(clonedNested).toEqual(nested);
    expect(clonedNested.customTest.myFunc()).toBe(nested.customTest.myFunc());
    expect(clonedNested.customTest.myFunc()).toEqual('value is 1');
  });
D-Sketon commented 2 weeks ago

maybe same as https://github.com/toss/es-toolkit/issues/777, and https://github.com/toss/es-toolkit/pull/794 can solve it

injae-kim commented 2 weeks ago

Fix PR: https://github.com/toss/es-toolkit/pull/794

raon0211 commented 2 weeks ago

This was fixed in es-toolkit v1.27.0 :)

eturino commented 2 weeks ago

thanks a lot! congratulations on the impressive cycle time 😆