Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggestion: validate inputs being of type object, early bail-out if necessary #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,28 @@ describe("shallowEqual on mixed array/objects", () => {
});
});

describe("shallowEqual on primitive values of any type", () => {
it("should act correctly", () => {
expect(shallowEqual<any>(1, 2)).toBe(false);
expect(shallowEqual<any>(1, 1)).toBe(true);
expect(shallowEqual<any>("boris", "johnson")).toBe(false);
expect(shallowEqual<any>("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", () => {
Expand All @@ -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);
});
});
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