-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshowShallowDiff.js
35 lines (32 loc) · 1.44 KB
/
showShallowDiff.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
'use strict';
// return true if there's a difference, false if not. Show any differences.
// if a property is an object and the property has an id field, show the difference of that -> but don't go deeper into properties.
function showShallowDiff(obj1, obj2) {
const args=global.args||{};
var differences=false;
if (!args.diff) return;
Object.keys(obj1).forEach(key => {
if (obj1[key] && typeof obj1[key] === "object" && obj1[key]!==null) { // it's an object
if(obj2[key] && typeof obj2[key]==='object' && obj2[key]!== null) { // it's an object too
if (obj1[key].id !== obj2[key].id) {
args.diff && console.error(key + '.id', obj1[key].id, "!==", obj2[key].id);
differences=true;
}
} else {
args.diff && console.error(key + obj1[key], "!==", obj2[key], "not an object");
differences=true;
}
} else { // obj1[key] is not an object
if (typeof obj2[key] === "undefined" && typeof obj1[key] !== 'undefined') {
args.diff && console.error(key + ':', obj1[key], "!==", obj2[key]);
differences=true;
}
if (obj1[key] !== obj2[key]){
args.diff && console.error(key + ':', obj1[key], "!==", obj2[key]);
differences=true;
}
}
})
return differences;
}
module.exports=showShallowDiff;