From 7981ccf270fb7b4aa5a5532c8e7feaea66ffca7d Mon Sep 17 00:00:00 2001 From: Luchankin Konstantin Date: Sat, 1 Apr 2023 11:19:10 +0500 Subject: [PATCH 1/3] isPlainObject optimize --- src/utils/isPlainObject.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/utils/isPlainObject.ts b/src/utils/isPlainObject.ts index 9d1956328a..243f35816c 100644 --- a/src/utils/isPlainObject.ts +++ b/src/utils/isPlainObject.ts @@ -5,10 +5,6 @@ export default function isPlainObject(obj: any): boolean { if (typeof obj !== 'object' || obj === null) return false - let proto = obj - while (Object.getPrototypeOf(proto) !== null) { - proto = Object.getPrototypeOf(proto) - } - - return Object.getPrototypeOf(obj) === proto + const proto = Object.getPrototypeOf(obj) + return Boolean(proto && !Object.getPrototypeOf(proto)) } From 80c44bec58775a2039b51937f65defdeb4fa7203 Mon Sep 17 00:00:00 2001 From: Luchankin Konstantin Date: Sat, 1 Apr 2023 15:08:34 +0500 Subject: [PATCH 2/3] fix Object.create(null), add tests --- src/utils/isPlainObject.ts | 6 +++--- test/utils/isPlainObject.spec.ts | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/utils/isPlainObject.ts b/src/utils/isPlainObject.ts index 243f35816c..9cfa8a9573 100644 --- a/src/utils/isPlainObject.ts +++ b/src/utils/isPlainObject.ts @@ -3,8 +3,8 @@ * @returns True if the argument appears to be a plain object. */ export default function isPlainObject(obj: any): boolean { - if (typeof obj !== 'object' || obj === null) return false + if (!obj) return false - const proto = Object.getPrototypeOf(obj) - return Boolean(proto && !Object.getPrototypeOf(proto)) + const proto = obj.__proto__ + return !proto || !Object.getPrototypeOf(proto) } diff --git a/test/utils/isPlainObject.spec.ts b/test/utils/isPlainObject.spec.ts index 62fe216152..2c984ea8ba 100644 --- a/test/utils/isPlainObject.spec.ts +++ b/test/utils/isPlainObject.spec.ts @@ -10,7 +10,11 @@ describe('isPlainObject', () => { expect(isPlainObject(new Date())).toBe(false) expect(isPlainObject([1, 2, 3])).toBe(false) expect(isPlainObject(null)).toBe(false) + expect(isPlainObject('')).toBe(false) + expect(isPlainObject(true)).toBe(false) expect(isPlainObject(undefined)).toBe(false) + expect(isPlainObject(false)).toBe(false) expect(isPlainObject({ x: 1, y: 2 })).toBe(true) + expect(isPlainObject(Object.create(null))).toBe(true) }) }) From e7c1c7ede090a82e0c2aa8408449b2e52afd3642 Mon Sep 17 00:00:00 2001 From: Luchankin Konstantin Date: Sat, 1 Apr 2023 16:23:14 +0500 Subject: [PATCH 3/3] isPlainObject early return upd --- src/utils/isPlainObject.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/isPlainObject.ts b/src/utils/isPlainObject.ts index 9cfa8a9573..2767112254 100644 --- a/src/utils/isPlainObject.ts +++ b/src/utils/isPlainObject.ts @@ -3,7 +3,7 @@ * @returns True if the argument appears to be a plain object. */ export default function isPlainObject(obj: any): boolean { - if (!obj) return false + if (typeof obj !== 'object' || obj === null) return false const proto = obj.__proto__ return !proto || !Object.getPrototypeOf(proto)