-
Notifications
You must be signed in to change notification settings - Fork 436
/
utils.js
53 lines (49 loc) · 1.25 KB
/
utils.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
module.exports = {
arraysDiffer: function (a, b) {
var isDifferent = false;
if (a.length !== b.length) {
isDifferent = true;
} else {
a.forEach(function (item, index) {
if (!this.isSame(item, b[index])) {
isDifferent = true;
}
}, this);
}
return isDifferent;
},
objectsDiffer: function (a, b) {
var isDifferent = false;
if (Object.keys(a).length !== Object.keys(b).length) {
isDifferent = true;
} else {
Object.keys(a).forEach(function (key) {
if (!this.isSame(a[key], b[key])) {
isDifferent = true;
}
}, this);
}
return isDifferent;
},
isSame: function (a, b) {
if (typeof a !== typeof b) {
return false;
} else if (Array.isArray(a) && Array.isArray(b)) {
return !this.arraysDiffer(a, b);
} else if (typeof a === 'function') {
return a.toString() === b.toString();
} else if (typeof a === 'object' && a !== null && b !== null) {
return !this.objectsDiffer(a, b);
}
return a === b;
},
find: function (collection, fn) {
for (var i = 0, l = collection.length; i < l; i++) {
var item = collection[i];
if (fn(item)) {
return item;
}
}
return null;
}
};