30 lines
875 B
JavaScript
30 lines
875 B
JavaScript
import { isDate, isEmptyObject, isObject, hasOwnProperty, makeObjectWithoutPrototype } from './utils.js';
|
|
|
|
const updatedDiff = (lhs, rhs) => {
|
|
if (lhs === rhs) return {};
|
|
|
|
if (!isObject(lhs) || !isObject(rhs)) return rhs;
|
|
|
|
if (isDate(lhs) || isDate(rhs)) {
|
|
if (lhs.valueOf() == rhs.valueOf()) return {};
|
|
return rhs;
|
|
}
|
|
|
|
return Object.keys(rhs).reduce((acc, key) => {
|
|
if (hasOwnProperty(lhs, key)) {
|
|
const difference = updatedDiff(lhs[key], rhs[key]);
|
|
|
|
// If the difference is empty, and the lhs is an empty object or the rhs is not an empty object
|
|
if (isEmptyObject(difference) && !isDate(difference) && (isEmptyObject(lhs[key]) || !isEmptyObject(rhs[key])))
|
|
return acc; // return no diff
|
|
|
|
acc[key] = difference;
|
|
return acc;
|
|
}
|
|
|
|
return acc;
|
|
}, makeObjectWithoutPrototype());
|
|
};
|
|
|
|
export default updatedDiff;
|