From d57cc8b9da3bce919c3f8e0f6e374d0837c1c05a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20=E2=80=9CTim=E2=80=9D=20Pillard?= Date: Wed, 6 Nov 2024 16:23:10 +0100 Subject: [PATCH 1/2] illustrate incorrect equality check result for non-object input types with failing tests --- src/__tests__/index.test.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index cc95854..830c77c 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -146,12 +146,28 @@ describe("shallowEqual on mixed array/objects", () => { }); }); +describe("shallowEqual on primitive values of any type", () => { + it("should return false", () => { + expect(shallowEqual(1, 2)).toBe(false); + expect(shallowEqual(1, 1)).toBe(false); + expect(shallowEqual("boris", "johnson")).toBe(false); + expect(shallowEqual("boris", "boris")).toBe(false); + }); +}); + describe("shallowEqualObjects", () => { objTests.forEach((test) => { it("should " + test.should, () => { expect(shallowEqualObjects(test.objA, test.objB)).toEqual(test.result); }); }); + + it("should return false for primitive values of any type", () => { + expect(shallowEqualObjects(1 as any, 2 as any)).toBe(false); + expect(shallowEqualObjects(1 as any, 1 as any)).toBe(false); + expect(shallowEqualObjects("boris" as any, "johnson" as any)).toBe(false); + expect(shallowEqualObjects("boris" as any, "boris" as any)).toBe(false); + }); }); describe("shallowEqualArrays", () => { @@ -160,4 +176,11 @@ describe("shallowEqualArrays", () => { expect(shallowEqualArrays(test.arrA, test.arrB)).toEqual(test.result); }); }); + + it("should return false for primitive values of any type", () => { + expect(shallowEqualArrays(1 as any, 2 as any)).toBe(false); + expect(shallowEqualArrays(1 as any, 1 as any)).toBe(false); + expect(shallowEqualArrays("boris" as any, "johnson" as any)).toBe(false); + expect(shallowEqualArrays("boris" as any, "boris" as any)).toBe(false); + }); }); From 6d6b67f4b9c82ee016e6921cfba214fee201ee37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20=E2=80=9CTim=E2=80=9D=20Pillard?= Date: Wed, 6 Nov 2024 16:24:58 +0100 Subject: [PATCH 2/2] suggestion: bail out of shallow comparison if either inputs are not of object type --- src/__tests__/index.test.ts | 18 +++++++++--------- src/arrays.ts | 4 ++++ src/objects.ts | 4 ++++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 830c77c..919147b 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -147,11 +147,11 @@ describe("shallowEqual on mixed array/objects", () => { }); describe("shallowEqual on primitive values of any type", () => { - it("should return false", () => { + it("should act correctly", () => { expect(shallowEqual(1, 2)).toBe(false); - expect(shallowEqual(1, 1)).toBe(false); + expect(shallowEqual(1, 1)).toBe(true); expect(shallowEqual("boris", "johnson")).toBe(false); - expect(shallowEqual("boris", "boris")).toBe(false); + expect(shallowEqual("boris", "boris")).toBe(true); }); }); @@ -162,11 +162,11 @@ describe("shallowEqualObjects", () => { }); }); - it("should return false for primitive values of any type", () => { + it("should act correctly for primitive values of any type", () => { expect(shallowEqualObjects(1 as any, 2 as any)).toBe(false); - expect(shallowEqualObjects(1 as any, 1 as any)).toBe(false); + expect(shallowEqualObjects(1 as any, 1 as any)).toBe(true); expect(shallowEqualObjects("boris" as any, "johnson" as any)).toBe(false); - expect(shallowEqualObjects("boris" as any, "boris" as any)).toBe(false); + expect(shallowEqualObjects("boris" as any, "boris" as any)).toBe(true); }); }); @@ -177,10 +177,10 @@ describe("shallowEqualArrays", () => { }); }); - it("should return false for primitive values of any type", () => { + it("should act correctly for primitive values of any type", () => { expect(shallowEqualArrays(1 as any, 2 as any)).toBe(false); - expect(shallowEqualArrays(1 as any, 1 as any)).toBe(false); + expect(shallowEqualArrays(1 as any, 1 as any)).toBe(true); expect(shallowEqualArrays("boris" as any, "johnson" as any)).toBe(false); - expect(shallowEqualArrays("boris" as any, "boris" as any)).toBe(false); + expect(shallowEqualArrays("boris" as any, "boris" as any)).toBe(true); }); }); diff --git a/src/arrays.ts b/src/arrays.ts index e3ff29c..229fb59 100644 --- a/src/arrays.ts +++ b/src/arrays.ts @@ -12,6 +12,10 @@ export default function shallowEqualArrays( return false; } + if (typeof arrA !== "object" || typeof arrB !== "object") { + return false; + } + const len = arrA.length; if (arrB.length !== len) { diff --git a/src/objects.ts b/src/objects.ts index 1d37da9..4b0c475 100644 --- a/src/objects.ts +++ b/src/objects.ts @@ -12,6 +12,10 @@ export default function shallowEqualObjects( return false; } + if (typeof objA !== "object" || typeof objB !== "object") { + return false; + } + const aKeys = Object.keys(objA); const bKeys = Object.keys(objB); const len = aKeys.length;