Skip to content

Commit

Permalink
suggestion: bail out of shallow comparison if either inputs are not o…
Browse files Browse the repository at this point in the history
…f object type
  • Loading branch information
ziir committed Nov 7, 2024
1 parent d57cc8b commit 6d6b67f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>(1, 2)).toBe(false);
expect(shallowEqual<any>(1, 1)).toBe(false);
expect(shallowEqual<any>(1, 1)).toBe(true);
expect(shallowEqual<any>("boris", "johnson")).toBe(false);
expect(shallowEqual<any>("boris", "boris")).toBe(false);
expect(shallowEqual<any>("boris", "boris")).toBe(true);
});
});

Expand All @@ -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);
});
});

Expand All @@ -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);
});
});
4 changes: 4 additions & 0 deletions src/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export default function shallowEqualObjects<T>(
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;
Expand Down

0 comments on commit 6d6b67f

Please sign in to comment.