forked from epoberezkin/fast-deep-equal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.js
60 lines (53 loc) · 1.17 KB
/
json.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
const isArray = Array.isArray;
const hasProp = Object.prototype.hasOwnProperty;
module.exports = function equal(a, b) {
if (a === b) {
return true;
}
if (a && b && typeof a == 'object' && typeof b == 'object') {
if (isArray(a)) {
if (isArray(b)) {
if (a.length != b.length) {
return false;
}
for (let i = a.length; i-- != 0;) {
if (!equal(a[i], b[i])) {
return false;
}
}
return true;
}
return false;
} else if (isArray(b)) {
return false;
}
// a and b have same ownprops
let key = '';
for (key in a) {
if (hasProp.call(a, key)) {
if (!hasProp.call(b, key)) {
return false;
}
}
}
// values of `a` are same as values of `b`
let count = 0;
for (key in a) {
if (hasProp.call(a, key)) {
if (!equal(a[key], b[key])) {
return false;
}
count++;
}
}
// `a` has same number of properties as `b`
for (key in b) {
if (hasProp.call(b, key)) {
count--;
}
}
return count == 0;
}
return a!==a && b!==b;
};