-
Notifications
You must be signed in to change notification settings - Fork 2
/
shallowCompare.js
51 lines (39 loc) · 1.17 KB
/
shallowCompare.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
//sub and super set
function isArSubset(v,o,key,seq){
for(var i = 0; i < v.length ;i++){
var elem = v[i]
if(o==undefined) {return false};
var elem2 = o.filter(r=>{return r[key] == elem[key]})
// if no matiching object found then fail
if(elem2.length == 0 ) {console.log("Array Gap : " + key + " : " + elem[key]);return false}
// if no matiching object found then fail
if(!isShallowEqual(elem,elem2[0],seq)) return false;
}
return true
}
//sub and super set
function isShallowEqual(v, o, seq) {
var nm = []
for(var key in v){
if(Array.isArray(v[key])){ //Ignore Array for now
if(seq[key]==undefined)
continue;
if(!isArSubset(v[key],o[key],seq[key],seq))
nm.push("Not Matching array : " + key)
continue
}
if(typeof v[key] =="object"){
if (!isShallowEqual(v[key], o[key], seq)) {
nm.push("Not Matching key : " + key)
}
continue;
}
if(!(key in o) || v[key] !== o[key]){
nm.push("Not Matching value : " + key)
}
}
if(nm.length!=0) nm.forEach(m=>{console.log(m)})
return nm.length==0
}
exports.isShallowEqual = isShallowEqual
exports.isArSubset = isArSubset