diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index cc95854..919147b 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 act correctly", () => { + expect(shallowEqual(1, 2)).toBe(false); + expect(shallowEqual(1, 1)).toBe(true); + expect(shallowEqual("boris", "johnson")).toBe(false); + expect(shallowEqual("boris", "boris")).toBe(true); + }); +}); + describe("shallowEqualObjects", () => { objTests.forEach((test) => { it("should " + test.should, () => { expect(shallowEqualObjects(test.objA, test.objB)).toEqual(test.result); }); }); + + 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(true); + expect(shallowEqualObjects("boris" as any, "johnson" as any)).toBe(false); + expect(shallowEqualObjects("boris" as any, "boris" as any)).toBe(true); + }); }); describe("shallowEqualArrays", () => { @@ -160,4 +176,11 @@ describe("shallowEqualArrays", () => { expect(shallowEqualArrays(test.arrA, test.arrB)).toEqual(test.result); }); }); + + 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(true); + expect(shallowEqualArrays("boris" as any, "johnson" 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;