Skip to content
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.

Commit

Permalink
feat(shallowEqual): add shallowEqual function (#89)
Browse files Browse the repository at this point in the history
* WIP: Cannot meet branches coverage

* Fix function and tests to pass the coverage

* Reduce cyclomatic complexity
  • Loading branch information
lukaleli authored and Kent C. Dodds committed May 11, 2017
1 parent 8735163 commit 8ab3382
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import initArray from './init-array'
import reduce from './reduce-to-tally'
import flatten from './flatten'
Expand Down Expand Up @@ -28,6 +27,7 @@ import searchAndReplace from './search-and-replace'
import sqrt from './sqrt'
import toPower from './to-power'
import mod from './mod'
import shallowEqual from './shallow-equal'
import swapCase from './swap-case'

export {
Expand Down Expand Up @@ -60,5 +60,6 @@ export {
sqrt,
toPower,
mod,
shallowEqual,
swapCase,
}
43 changes: 43 additions & 0 deletions src/shallow-equal.js
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)
}
130 changes: 130 additions & 0 deletions test/shallow-equal.test.js
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)
})

0 comments on commit 8ab3382

Please sign in to comment.