-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisEqualDeep.js
44 lines (35 loc) · 912 Bytes
/
isEqualDeep.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
34
35
36
37
38
39
40
41
42
43
44
import { isArray, isObjectLike } from '../is/is'
import { isEqual } from '../isEqual/isEqual'
import { keys } from '../../object/keys/keys'
import { toStringTag } from '../_/setup'
export const isEqualDeep = (x, y) => {
if (isEqual(x, y)) {
return true
}
return baseIsEqualDeep(x, y)
}
const baseIsEqualDeep = (x, y) => {
if (toStringTag(x) !== toStringTag(y)) {
return false
}
if (isArray(x)) {
return isArrayDeepEqual(x, y)
}
if (isObjectLike(x)) {
return isObjectDeepEqual(x, y)
}
return isEqual(x, y)
}
export const isObjectDeepEqual = (x, y) => {
const xKeys = keys(x)
const yKeys = keys(x)
if (xKeys.length !== yKeys.length) {
return false
}
return xKeys.every(xk => baseIsEqualDeep(x[xk], y[xk]))
}
export const isArrayDeepEqual = (x, y) =>
isArray(x) &&
isArray(y) &&
x.length === y.length &&
x.every((v, i) => baseIsEqualDeep(v, y[i]))