-
Notifications
You must be signed in to change notification settings - Fork 4
/
check.js
33 lines (30 loc) · 1.11 KB
/
check.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const isObjectShallowEqual = (value, target) => {
if (value === target) return true
const valueKeyList = Object.keys(value)
const targetKeyList = Object.keys(target)
if (valueKeyList.length !== targetKeyList.length) return false
for (let index = 0, indexMax = valueKeyList.length; index < indexMax; index++) {
const valueKey = valueKeyList[ index ]
const targetKey = targetKeyList[ index ]
if (!Object.prototype.hasOwnProperty.call(target, valueKey) || value[ valueKey ] !== target[ targetKey ]) return false
}
return true
}
const isArrayShallowEqual = (value, target) => value === target || (
value.length === target.length &&
value.every((v, i) => v === target[ i ])
)
// only good for full array (no holes) like arguments
const isCompactArrayShallowEqual = (value, target) => {
if (value === target) return true
if (value.length !== target.length) return false
for (let index = 0, indexMax = value.length; index < indexMax; index++) {
if (value[ index ] !== target[ index ]) return false
}
return true
}
export {
isObjectShallowEqual,
isArrayShallowEqual,
isCompactArrayShallowEqual
}