Skip to content

Commit

Permalink
Fixed shallowEqual implementation to handle the case when inputs are …
Browse files Browse the repository at this point in the history
…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 facebook#3369
  • Loading branch information
varunrau committed Mar 11, 2015
1 parent 91b4564 commit 6cd004f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/utils/__tests__/shallowEqual-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions src/utils/shallowEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6cd004f

Please sign in to comment.