-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (26 loc) · 839 Bytes
/
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
function compareJSON(obj1, obj2) {
const changes = {
added: {},
removed: {},
modified: {}
};
function findChanges(o1, o2, path = '') {
for (let key in o1) {
if (!o2.hasOwnProperty(key)) {
changes.removed[path + key] = o1[key];
} else if (typeof o1[key] === 'object' && typeof o2[key] === 'object') {
findChanges(o1[key], o2[key], `${path}${key}.`);
} else if (o1[key] !== o2[key]) {
changes.modified[path + key] = { old: o1[key], new: o2[key] };
}
}
for (let key in o2) {
if (!o1.hasOwnProperty(key)) {
changes.added[path + key] = o2[key];
}
}
}
findChanges(obj1, obj2);
return changes;
}
module.exports = compareJSON;