Skip to content

Commit 573db0b

Browse files
authored
Use shallowEqual from fbjs. (#591)
1 parent e548778 commit 573db0b

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/utils/shallowEqual.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
const hasOwn = Object.prototype.hasOwnProperty
22

3-
export default function shallowEqual(a, b) {
4-
if (a === b) return true
3+
function is(x, y) {
4+
if (x === y) {
5+
return x !== 0 || y !== 0 || 1 / x === 1 / y
6+
} else {
7+
return x !== x && y !== y
8+
}
9+
}
10+
11+
export default function shallowEqual(objA, objB) {
12+
if (is(objA, objB)) return true
513

6-
let countA = 0
7-
let countB = 0
8-
9-
for (let key in a) {
10-
if (hasOwn.call(a, key) && a[key] !== b[key]) return false
11-
countA++
14+
if (typeof objA !== 'object' || objA === null ||
15+
typeof objB !== 'object' || objB === null) {
16+
return false
1217
}
1318

14-
for (let key in b) {
15-
if (hasOwn.call(b, key)) countB++
19+
const keysA = Object.keys(objA)
20+
const keysB = Object.keys(objB)
21+
22+
if (keysA.length !== keysB.length) return false
23+
24+
for (let i = 0; i < keysA.length; i++) {
25+
if (!hasOwn.call(objB, keysA[i]) ||
26+
!is(objA[keysA[i]], objB[keysA[i]])) {
27+
return false
28+
}
1629
}
1730

18-
return countA === countB
31+
return true
1932
}

0 commit comments

Comments
 (0)