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

Commit 8ab3382

Browse files
lukaleliKent C. Dodds
authored andcommitted
feat(shallowEqual): add shallowEqual function (#89)
* WIP: Cannot meet branches coverage * Fix function and tests to pass the coverage * Reduce cyclomatic complexity
1 parent 8735163 commit 8ab3382

File tree

3 files changed

+175
-1
lines changed

3 files changed

+175
-1
lines changed

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import initArray from './init-array'
32
import reduce from './reduce-to-tally'
43
import flatten from './flatten'
@@ -28,6 +27,7 @@ import searchAndReplace from './search-and-replace'
2827
import sqrt from './sqrt'
2928
import toPower from './to-power'
3029
import mod from './mod'
30+
import shallowEqual from './shallow-equal'
3131
import swapCase from './swap-case'
3232

3333
export {
@@ -60,5 +60,6 @@ export {
6060
sqrt,
6161
toPower,
6262
mod,
63+
shallowEqual,
6364
swapCase,
6465
}

src/shallow-equal.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export default shallowEqual
2+
3+
function haveEqualKeys(a, b) {
4+
const keysA = Object.keys(a)
5+
const keysB = Object.keys(b)
6+
return keysA.length === keysB.length
7+
}
8+
9+
function isNullOrNonObject(obj) {
10+
return typeof obj !== 'object' || obj === null
11+
}
12+
13+
function compareKeys(objA, objB) {
14+
const keysA = Object.keys(objA)
15+
const bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB)
16+
for (let i = 0; i < keysA.length; i++) {
17+
if (!bHasOwnProperty(keysA[i]) || objA[keysA[i]] !== objB[keysA[i]]) {
18+
return false
19+
}
20+
}
21+
22+
return true
23+
}
24+
25+
/**
26+
* This method will shallow compare two objects
27+
*
28+
* @param {Object} objA - first object to compare
29+
* @param {Object} objB - second object to compare
30+
* @returns {boolean} - result of comparison - true if objects are shallow equal
31+
*/
32+
33+
function shallowEqual(objA, objB) {
34+
if (objA === objB) {
35+
return true
36+
}
37+
38+
if (isNullOrNonObject(objA) || isNullOrNonObject(objB) || !haveEqualKeys(objA, objB)) {
39+
return false
40+
}
41+
42+
return compareKeys(objA, objB)
43+
}

test/shallow-equal.test.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import test from 'ava'
2+
import {shallowEqual} from '../src'
3+
4+
test('Compares two equal objects', t => {
5+
const a = {
6+
a: 1,
7+
b: 2,
8+
c: 'test'
9+
}
10+
11+
const b = {
12+
a: 1,
13+
b: 2,
14+
c: 'test'
15+
}
16+
17+
const actual = shallowEqual(a, b)
18+
const expected = true
19+
t.deepEqual(actual, expected)
20+
})
21+
22+
test('Compares two equal objects symmetrically', t => {
23+
const a = {
24+
a: 1,
25+
b: 2,
26+
c: 'test'
27+
}
28+
29+
const b = {
30+
a: 1,
31+
b: 2,
32+
c: 'test'
33+
}
34+
35+
t.deepEqual(shallowEqual(a, b), shallowEqual(b, a))
36+
})
37+
38+
test('Compares two inequal objects', t => {
39+
const a = {
40+
a: 1,
41+
b: 2
42+
}
43+
44+
const b = {
45+
a: 1,
46+
b: 2,
47+
c: 'test'
48+
}
49+
50+
const actual = shallowEqual(a, b)
51+
const expected = false
52+
t.deepEqual(actual, expected)
53+
})
54+
55+
test('Compares two inequal objects symmetrically', t => {
56+
const b = {
57+
a: 1,
58+
b: 2
59+
}
60+
61+
const a = {
62+
a: 1,
63+
b: 3
64+
}
65+
66+
t.deepEqual(shallowEqual(a, b), shallowEqual(b, a))
67+
})
68+
69+
test('Compares only own properties of given objects', t => {
70+
function A() {
71+
this.a = 1
72+
this.b = 2
73+
this.c = 'test'
74+
}
75+
76+
A.prototype.anotherProp = 12345
77+
78+
const a = new A()
79+
const b = {
80+
a: 1,
81+
b: 2,
82+
c: 'test'
83+
}
84+
85+
const actual = shallowEqual(a, b)
86+
const expected = true
87+
t.deepEqual(actual, expected)
88+
})
89+
90+
test('Compares only own properties of given objects symmetrically', t => {
91+
function A() {
92+
this.a = 1
93+
this.b = 2
94+
this.c = 'test'
95+
}
96+
97+
A.prototype.anotherProp = 12345
98+
99+
const a = new A()
100+
const b = {
101+
a: 1,
102+
b: 2,
103+
c: 'test'
104+
}
105+
106+
t.deepEqual(shallowEqual(a, b), shallowEqual(b, a))
107+
})
108+
109+
test('Returns true when the same object is compared to itself', t => {
110+
const a = {
111+
a: 1,
112+
b: 'test'
113+
}
114+
115+
const actual = shallowEqual(a, a)
116+
const expected = true
117+
t.deepEqual(actual, expected)
118+
})
119+
120+
121+
test('Returns false when one of the objects is null', t => {
122+
const a = {
123+
a: 1,
124+
b: 'test'
125+
}
126+
127+
const actual = shallowEqual(a, null)
128+
const expected = false
129+
t.deepEqual(actual, expected)
130+
})

0 commit comments

Comments
 (0)