-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
index.js
28 lines (19 loc) · 883 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
import { isDate, isEmpty, isObject } from '../utils';
const diff = (lhs, rhs) => {
if (lhs === rhs) return {}; // equal return no diff
if (!isObject(lhs) || !isObject(rhs)) return rhs; // return updated rhs
const deletedValues = Object.keys(lhs).reduce((acc, key) => {
return rhs.hasOwnProperty(key) ? acc : { ...acc, [key]: undefined };
}, {});
if (isDate(lhs) || isDate(rhs)) {
if (lhs.toString() == rhs.toString()) return {};
return rhs;
}
return Object.keys(rhs).reduce((acc, key) => {
if (!lhs.hasOwnProperty(key)) return { ...acc, [key]: rhs[key] }; // return added rhs key
const difference = diff(lhs[key], rhs[key]);
if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff
return { ...acc, [key]: difference }; // return updated key
}, deletedValues);
};
export default diff;