-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (47 loc) · 1.29 KB
/
index.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
61
62
'use strict';
// Where b dons contain all of a
function _inspect(a, b) {
if (typeof a !== typeof b) return false;
if (typeof a !== 'object') return a === b;
if (Array.isArray(a) && Array.isArray(b)) {
return a.every(function(aChild) {
if (typeof aChild !== 'object') return b.indexOf(aChild) !== -1;
return b.some(function(bChild) {
return _inspect(aChild, bChild);
});
});
}
if (Object.keys(a).length > Object.keys(b).length) return false;
for (var aKey in a) {
if (!(aKey in b)) return false;
if (!_inspect(a[aKey], b[aKey])) return false;
}
return true;
}
// Where b will contains all of a
function _fullfill(a, b) {
if (typeof a !== typeof b) throw new Error('both variables should have the same type');
if (typeof a !== 'object') return a === b;
if (Array.isArray(a) && Array.isArray(b)) {
return a.forEach(function(aChild) {
if (typeof aChild !== 'object') {
if (b.indexOf(aChild) === -1) b.push(aChild);
return ;
}
return b.some(function(bChild) {
return _fullfill(aChild, bChild);
});
});
}
for (var aKey in a) {
if (!(aKey in b)) {
b[aKey] = a[aKey];
continue ;
}
_fullfill(a[aKey], b[aKey]);
}
}
module.exports = {
inspect: _inspect,
fullfill: _fullfill
};