From 05c20f1aa0081495c60d059bd5ad9c6ce430671f Mon Sep 17 00:00:00 2001 From: Vic Winberry Date: Mon, 24 Apr 2023 11:15:47 +1200 Subject: [PATCH] fix bugs that occur in the presence of Array polyfills --- src/__tests__/utils/cloneObject.test.ts | 19 +++++++++++++++++++ src/__tests__/utils/unset.test.ts | 21 +++++++++++++++++++++ src/utils/cloneObject.ts | 6 ++++-- src/utils/unset.ts | 2 +- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/__tests__/utils/cloneObject.test.ts b/src/__tests__/utils/cloneObject.test.ts index 609cfcaac45..7a909fd401a 100644 --- a/src/__tests__/utils/cloneObject.test.ts +++ b/src/__tests__/utils/cloneObject.test.ts @@ -114,4 +114,23 @@ describe('clone', () => { other: 'string', }); }); + + describe('in presence of Array polyfills', () => { + beforeAll(() => { + // @ts-expect-error + Array.prototype.somePolyfill = () => 123; + }); + + it('should skip polyfills while cloning', () => { + const data = [1]; + const copy = cloneObject(data); + + expect(Object.hasOwn(copy, 'somePolyfill')).toBe(false); + }); + + afterAll(() => { + // @ts-expect-error + delete Array.prototype.somePolyfill; + }); + }); }); diff --git a/src/__tests__/utils/unset.test.ts b/src/__tests__/utils/unset.test.ts index 8ced221c919..10d4c6542b1 100644 --- a/src/__tests__/utils/unset.test.ts +++ b/src/__tests__/utils/unset.test.ts @@ -311,4 +311,25 @@ describe('unset', () => { expect(test.test.root).toBeDefined(); }); }); + + describe('in presence of Array polyfills', () => { + beforeAll(() => { + // @ts-expect-error + Array.prototype.somePolyfill = () => 123; + }); + + it('should delete empty arrays', () => { + const data = { + prop: [], + }; + unset(data, 'prop.0'); + + expect(data.prop).toBeUndefined(); + }); + + afterAll(() => { + // @ts-expect-error + delete Array.prototype.somePolyfill; + }); + }); }); diff --git a/src/utils/cloneObject.ts b/src/utils/cloneObject.ts index 3fb8a6c8832..bde7a8accde 100644 --- a/src/utils/cloneObject.ts +++ b/src/utils/cloneObject.ts @@ -16,11 +16,13 @@ export default function cloneObject(data: T): T { ) { copy = isArray ? [] : {}; - if (!Array.isArray(data) && !isPlainObject(data)) { + if (!isArray && !isPlainObject(data)) { copy = data; } else { for (const key in data) { - copy[key] = cloneObject(data[key]); + if (Object.hasOwn(data, key)) { + copy[key] = cloneObject(data[key]); + } } } } else { diff --git a/src/utils/unset.ts b/src/utils/unset.ts index 766309b1d02..a90cd7e3794 100644 --- a/src/utils/unset.ts +++ b/src/utils/unset.ts @@ -17,7 +17,7 @@ function baseGet(object: any, updatePath: (string | number)[]) { function isEmptyArray(obj: unknown[]) { for (const key in obj) { - if (!isUndefined(obj[key])) { + if (Object.hasOwn(obj, key) && !isUndefined(obj[key])) { return false; } }