-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (29 loc) · 1.09 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
const genDiff = (path, first, second) => ({
path, firstType: typeof first, secondType: typeof second, firstValue: first, secondValue: second,
});
const compareObjects = (first, second, compareValues = false) => {
const diffList = [];
const checkKeys = (firstObj, secondObj, path = '') => Object.keys(firstObj).map((key) => {
const join = path === '' ? '' : '.';
const newPath = `${path}${join}${key}`;
const firstType = typeof firstObj[key];
if (key in secondObj) {
const secondType = typeof secondObj[key];
if (firstType === secondType) {
if (firstType === 'object') {
checkKeys(firstObj[key], secondObj[key], newPath);
} else if (compareValues && firstObj[key] !== secondObj[key]) {
diffList.push(genDiff(newPath, firstObj[key], secondObj[key]));
}
} else {
diffList.push(genDiff(newPath, firstObj[key], secondObj[key]));
}
} else {
diffList.push(genDiff(newPath, firstObj[key], secondObj[key]));
}
return true;
});
checkKeys(first, second);
return diffList;
};
module.exports = compareObjects;