-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
deepEqual.ts
45 lines (35 loc) 路 1007 Bytes
/
deepEqual.ts
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
45
import isObject from '../utils/isObject';
import isDateObject from './isDateObject';
import isPrimitive from './isPrimitive';
export default function deepEqual(object1: any, object2: any) {
if (isPrimitive(object1) || isPrimitive(object2)) {
return object1 === object2;
}
if (isDateObject(object1) && isDateObject(object2)) {
return object1.getTime() === object2.getTime();
}
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);
if (keys1.length !== keys2.length) {
return false;
}
for (const key of keys1) {
const val1 = object1[key];
if (!keys2.includes(key)) {
return false;
}
if (key !== 'ref') {
const val2 = object2[key];
if (
(isDateObject(val1) && isDateObject(val2)) ||
(isObject(val1) && isObject(val2)) ||
(Array.isArray(val1) && Array.isArray(val2))
? !deepEqual(val1, val2)
: val1 !== val2
) {
return false;
}
}
}
return true;
}