Skip to content

shallowEqual: bail if either argument is falsey #3317

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

Merged
merged 1 commit into from
Mar 10, 2015
Merged
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
71 changes: 71 additions & 0 deletions src/utils/__tests__/shallowEqual-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright 2014-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails react-core
*/

'use strict';

require('mock-modules')
.dontMock('shallowEqual');

var shallowEqual;

describe('shallowEqual', function() {

beforeEach(function() {
shallowEqual = require('shallowEqual');
});

it('returns false if either argument is null', function() {
expect(shallowEqual(null, {})).toBe(false);
expect(shallowEqual({}, null)).toBe(false);
});

it('returns true if both arguments are null or undefined', function() {
expect(shallowEqual(null, null)).toBe(true);
expect(shallowEqual(undefined, undefined)).toBe(true);
});

it('returns true if arguments are shallow equal', function() {
expect(
shallowEqual(
{a: 1, b: 2, c: 3},
{a: 1, b: 2, c: 3}
)
).toBe(true);
});

it('returns false if first argument has too many keys', function() {
expect(
shallowEqual(
{a: 1, b: 2, c: 3},
{a: 1, b: 2}
)
).toBe(false);
});

it('returns false if second argument has too many keys', function() {
expect(
shallowEqual(
{a: 1, b: 2},
{a: 1, b: 2, c: 3}
)
).toBe(false);
});

it('returns false if arguments are not shallow equal', function() {
expect(
shallowEqual(
{a: 1, b: 2, c: {}},
{a: 1, b: 2, c: {}}
)
).toBe(false);
});

});
5 changes: 5 additions & 0 deletions src/utils/shallowEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ function shallowEqual(objA, objB) {
if (objA === objB) {
return true;
}

if (!objA || !objB) {
return false;
}

var key;
// Test for A's keys different from B.
for (key in objA) {
Expand Down