From 6cd004f20ae6e2eb6168f853214c6880934ae4c9 Mon Sep 17 00:00:00 2001 From: Varun Rau Date: Tue, 10 Mar 2015 18:03:51 -0700 Subject: [PATCH] Fixed shallowEqual implementation to handle the case when inputs are not objects. If either argument is not an object and unequal, then shallowEqual should return false. If only one argument is an object, then shallowEqual should return false. Fixes #3369 --- src/utils/__tests__/shallowEqual-test.js | 18 ++++++++++++++++++ src/utils/shallowEqual.js | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/src/utils/__tests__/shallowEqual-test.js b/src/utils/__tests__/shallowEqual-test.js index 522c62cda8..5b6a9304d3 100644 --- a/src/utils/__tests__/shallowEqual-test.js +++ b/src/utils/__tests__/shallowEqual-test.js @@ -41,6 +41,24 @@ describe('shallowEqual', function() { ).toBe(true); }); + it('returns false if arguments are not objects and not equal', function() { + expect( + shallowEqual( + 1, + 2 + ) + ).toBe(false); + }); + + it('returns false if only one argument is not an object', function() { + expect( + shallowEqual( + 1, + {} + ) + ).toBe(false); + }) + it('returns false if first argument has too many keys', function() { expect( shallowEqual( diff --git a/src/utils/shallowEqual.js b/src/utils/shallowEqual.js index 8b35d0d4dc..8339d2dd5f 100644 --- a/src/utils/shallowEqual.js +++ b/src/utils/shallowEqual.js @@ -27,6 +27,10 @@ function shallowEqual(objA, objB) { return false; } + if (typeof objA !== 'object' || typeof objB !== 'object') { + return false; + } + var key; // Test for A's keys different from B. for (key in objA) {