This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shallowEqual): add shallowEqual function (#89)
* WIP: Cannot meet branches coverage * Fix function and tests to pass the coverage * Reduce cyclomatic complexity
- Loading branch information
Showing
3 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export default shallowEqual | ||
|
||
function haveEqualKeys(a, b) { | ||
const keysA = Object.keys(a) | ||
const keysB = Object.keys(b) | ||
return keysA.length === keysB.length | ||
} | ||
|
||
function isNullOrNonObject(obj) { | ||
return typeof obj !== 'object' || obj === null | ||
} | ||
|
||
function compareKeys(objA, objB) { | ||
const keysA = Object.keys(objA) | ||
const bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB) | ||
for (let i = 0; i < keysA.length; i++) { | ||
if (!bHasOwnProperty(keysA[i]) || objA[keysA[i]] !== objB[keysA[i]]) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
/** | ||
* This method will shallow compare two objects | ||
* | ||
* @param {Object} objA - first object to compare | ||
* @param {Object} objB - second object to compare | ||
* @returns {boolean} - result of comparison - true if objects are shallow equal | ||
*/ | ||
|
||
function shallowEqual(objA, objB) { | ||
if (objA === objB) { | ||
return true | ||
} | ||
|
||
if (isNullOrNonObject(objA) || isNullOrNonObject(objB) || !haveEqualKeys(objA, objB)) { | ||
return false | ||
} | ||
|
||
return compareKeys(objA, objB) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import test from 'ava' | ||
import {shallowEqual} from '../src' | ||
|
||
test('Compares two equal objects', t => { | ||
const a = { | ||
a: 1, | ||
b: 2, | ||
c: 'test' | ||
} | ||
|
||
const b = { | ||
a: 1, | ||
b: 2, | ||
c: 'test' | ||
} | ||
|
||
const actual = shallowEqual(a, b) | ||
const expected = true | ||
t.deepEqual(actual, expected) | ||
}) | ||
|
||
test('Compares two equal objects symmetrically', t => { | ||
const a = { | ||
a: 1, | ||
b: 2, | ||
c: 'test' | ||
} | ||
|
||
const b = { | ||
a: 1, | ||
b: 2, | ||
c: 'test' | ||
} | ||
|
||
t.deepEqual(shallowEqual(a, b), shallowEqual(b, a)) | ||
}) | ||
|
||
test('Compares two inequal objects', t => { | ||
const a = { | ||
a: 1, | ||
b: 2 | ||
} | ||
|
||
const b = { | ||
a: 1, | ||
b: 2, | ||
c: 'test' | ||
} | ||
|
||
const actual = shallowEqual(a, b) | ||
const expected = false | ||
t.deepEqual(actual, expected) | ||
}) | ||
|
||
test('Compares two inequal objects symmetrically', t => { | ||
const b = { | ||
a: 1, | ||
b: 2 | ||
} | ||
|
||
const a = { | ||
a: 1, | ||
b: 3 | ||
} | ||
|
||
t.deepEqual(shallowEqual(a, b), shallowEqual(b, a)) | ||
}) | ||
|
||
test('Compares only own properties of given objects', t => { | ||
function A() { | ||
this.a = 1 | ||
this.b = 2 | ||
this.c = 'test' | ||
} | ||
|
||
A.prototype.anotherProp = 12345 | ||
|
||
const a = new A() | ||
const b = { | ||
a: 1, | ||
b: 2, | ||
c: 'test' | ||
} | ||
|
||
const actual = shallowEqual(a, b) | ||
const expected = true | ||
t.deepEqual(actual, expected) | ||
}) | ||
|
||
test('Compares only own properties of given objects symmetrically', t => { | ||
function A() { | ||
this.a = 1 | ||
this.b = 2 | ||
this.c = 'test' | ||
} | ||
|
||
A.prototype.anotherProp = 12345 | ||
|
||
const a = new A() | ||
const b = { | ||
a: 1, | ||
b: 2, | ||
c: 'test' | ||
} | ||
|
||
t.deepEqual(shallowEqual(a, b), shallowEqual(b, a)) | ||
}) | ||
|
||
test('Returns true when the same object is compared to itself', t => { | ||
const a = { | ||
a: 1, | ||
b: 'test' | ||
} | ||
|
||
const actual = shallowEqual(a, a) | ||
const expected = true | ||
t.deepEqual(actual, expected) | ||
}) | ||
|
||
|
||
test('Returns false when one of the objects is null', t => { | ||
const a = { | ||
a: 1, | ||
b: 'test' | ||
} | ||
|
||
const actual = shallowEqual(a, null) | ||
const expected = false | ||
t.deepEqual(actual, expected) | ||
}) |