Skip to content

Commit 62be736

Browse files
🐞 fix(deepEqual): empty array and empty plain object should not be equal (#13533)
* fix(deepEqual): empty array and empty plain object should not be equal deepEqual([], {}) and deepEqual({}, []) incorrectly returned true because both have zero enumerable keys and neither triggered the isEmptyObjectWithCustomPrototype guard ([] is excluded by Array.isArray and {} is excluded by isPlainObject). Add an early-return check: when both objects have zero keys and their array-ness differs, return false. This preserves the intentional sparse-array / plain-object compatibility introduced in #13347 (only non-empty containers need that treatment) while fixing the degenerate empty case. Fixes: deepEqual([], {}) => false (was true) Fixes: deepEqual({ items: [] }, { items: {} }) => false (was true) * Update src/utils/deepEqual.ts --------- Co-authored-by: JSap0914 <[email protected]> Co-authored-by: Bill <[email protected]>
1 parent cee92e2 commit 62be736

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

‎src/__tests__/utils/deepEqual.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,11 @@ describe('deepEqual', () => {
223223
expect(deepEqual(new Set(['test']), new Set(['test']))).toBeFalsy();
224224
expect(deepEqual(new EmptyObject(), new EmptyObject())).toBeFalsy();
225225
});
226+
227+
it('should return false when comparing an empty array with an empty plain object', () => {
228+
expect(deepEqual([], {})).toBeFalsy();
229+
expect(deepEqual({}, [])).toBeFalsy();
230+
expect(deepEqual({ items: [] }, { items: {} })).toBeFalsy();
231+
expect(deepEqual({ items: {} }, { items: [] })).toBeFalsy();
232+
});
226233
});

‎src/utils/deepEqual.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export default function deepEqual(
3737
return Object.is(object1, object2);
3838
}
3939

40+
if (!keys1.length && Array.isArray(object1) !== Array.isArray(object2)) {
41+
return false;
42+
}
43+
4044
const visitedPairs = visited.get(object1);
4145

4246
if (visitedPairs && visitedPairs.has(object2)) {

0 commit comments

Comments
 (0)