UNPKG

bbo

Version:

bbo is a utility library of zero dependencies for javascript.

45 lines (36 loc) 1.21 kB
import isObject from './is_object.js'; import './get_tag.js'; import './is_array.js'; import './is_string.js'; import './is_map.js'; import './is_set.js'; import isDate from './is_date.js'; import isEmpty from './is_empty.js'; import properObject from './proper_object.js'; var objectDiff = (lhs, rhs) => { if (lhs === rhs) return {}; // equal return no diff if (!isObject(lhs) || !isObject(rhs)) return rhs; // return updated rhs var l = properObject(lhs); var r = properObject(rhs); var deletedValues = Object.keys(l).reduce((acc, key) => { return r.hasOwnProperty(key) ? acc : { ...acc, [key]: undefined }; }, {}); if (isDate(l) || isDate(r)) { // eslint-disable-next-line eqeqeq if (l.valueOf() == r.valueOf()) return {}; return r; } return Object.keys(r).reduce((acc, key) => { if (!l.hasOwnProperty(key)) return { ...acc, [key]: r[key] }; // return added r key var difference = objectDiff(l[key], r[key]); if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff return { ...acc, [key]: difference }; // return updated key }, deletedValues); }; export default objectDiff;