Skip to content
Merged
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
35 changes: 24 additions & 11 deletions src/utils/shallowEqual.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
const hasOwn = Object.prototype.hasOwnProperty

export default function shallowEqual(a, b) {
if (a === b) return true
function is(x, y) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y
} else {
return x !== x && y !== y
}
}

export default function shallowEqual(objA, objB) {
if (is(objA, objB)) return true

let countA = 0
let countB = 0

for (let key in a) {
if (hasOwn.call(a, key) && a[key] !== b[key]) return false
countA++
if (typeof objA !== 'object' || objA === null ||
typeof objB !== 'object' || objB === null) {
return false
}

for (let key in b) {
if (hasOwn.call(b, key)) countB++
const keysA = Object.keys(objA)
const keysB = Object.keys(objB)

if (keysA.length !== keysB.length) return false

for (let i = 0; i < keysA.length; i++) {
if (!hasOwn.call(objB, keysA[i]) ||
!is(objA[keysA[i]], objB[keysA[i]])) {
return false
}
}

return countA === countB
return true
}