2021-04-12 13:32:25 +00:00
|
|
|
import { differenceWith, isArray, isEqual, isObject, map } from "lodash";
|
2017-01-17 22:45:07 +00:00
|
|
|
|
|
|
|
|
const deepDifference = (obj1, obj2) => {
|
|
|
|
|
const result = {};
|
|
|
|
|
|
|
|
|
|
map(obj1, (value, key) => {
|
|
|
|
|
const obj2Value = obj2[key];
|
|
|
|
|
|
2017-01-18 17:10:37 +00:00
|
|
|
if (isEqual(value, obj2Value)) return;
|
|
|
|
|
|
|
|
|
|
if (isArray(value) && isArray(obj2Value)) {
|
2017-01-20 17:17:51 +00:00
|
|
|
if (!value.length && obj2Value.length) {
|
|
|
|
|
result[key] = value;
|
|
|
|
|
} else {
|
|
|
|
|
const arrayDiff = differenceWith(value, obj2Value, isEqual);
|
2017-01-18 17:10:37 +00:00
|
|
|
|
2017-01-20 17:17:51 +00:00
|
|
|
if (arrayDiff.length) {
|
|
|
|
|
result[key] = arrayDiff;
|
|
|
|
|
}
|
2017-01-18 17:10:37 +00:00
|
|
|
}
|
|
|
|
|
} else if (isObject(value) && isObject(obj2Value)) {
|
2017-01-17 22:45:07 +00:00
|
|
|
result[key] = deepDifference(value, obj2Value);
|
2017-01-18 17:10:37 +00:00
|
|
|
} else {
|
2017-01-17 22:45:07 +00:00
|
|
|
result[key] = value;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default deepDifference;
|