Skip to content
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
9 changes: 8 additions & 1 deletion src/isEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@ function isEqual(obj1: any, obj2: any, shallow = false): boolean {
return true;
}
if (a && b && typeof a === 'object' && typeof b === 'object') {
if (Array.isArray(b)) {
return false;
}
const keys = Object.keys(a);
if (keys.length !== Object.keys(b).length) {
return false;
}
return keys.every(key => deepEqual(a[key], b[key], newLevel));
return keys.every(
key =>
Object.prototype.hasOwnProperty.call(b, key) &&
deepEqual(a[key], b[key], newLevel),
);
}
// other
return false;
Expand Down
18 changes: 18 additions & 0 deletions src/test/isEqual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ describe('isEqual', () => {
expect(valueIsEqual).toBe(false);
});

it('should not equal when keys differ but counts match', () => {
expect(isEqual({ a: undefined }, { b: 1 })).toBe(false);
expect(isEqual({ a: 1, b: undefined }, { a: 1, c: 2 })).toBe(false);
expect(isEqual({ a: undefined }, { b: undefined })).toBe(false);
expect(isEqual({ a: { b: undefined } }, { a: { c: 1 } })).toBe(false);
});

it('should not equal an object and an array', () => {
expect(isEqual({}, [])).toBe(false);
expect(isEqual({ 0: 1 }, [1])).toBe(false);
expect(isEqual([1], { 0: 1 })).toBe(false);
expect(isEqual({ a: {} }, { a: [] })).toBe(false);
});

it('should equal objects with undefined values on the same keys', () => {
expect(isEqual({ a: undefined, b: 1 }, { b: 1, a: undefined })).toBe(true);
});

it('should not equal 6', () => {
obj.obj = obj;
const obj2 = { a: 1, b: 2, c: [1, 2], obj: null };
Expand Down