-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy patharrayDiff.js
53 lines (38 loc) · 1.59 KB
/
arrayDiff.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { isDate, isEmpty, isObject, hasOwnProperty } from './utils.js';
const diff = (lhs, rhs) => {
if (lhs === rhs) return {}; // equal return no diff
if (!isObject(lhs) || !isObject(rhs)) return rhs; // return updated rhs
const l = lhs;
const r = rhs;
const deletedValues = Object.keys(l).reduce((acc, key) => {
return hasOwnProperty(r, key) ? acc : { ...acc, [key]: undefined };
}, {});
if (isDate(l) || isDate(r)) {
if (l.valueOf() == r.valueOf()) return {};
return r;
}
if (Array.isArray(r) && Array.isArray(l)) {
const deletedValues = l.reduce((acc, item, index) => {
return hasOwnProperty(r, index) ? acc.concat(item) : acc.concat(undefined);
}, []);
return r.reduce((acc, rightItem, index) => {
if (!hasOwnProperty(deletedValues, index)) {
return acc.concat(rightItem);
}
const leftItem = l[index];
const difference = diff(rightItem, leftItem);
if (isObject(difference) && isEmpty(difference) && !isDate(difference)) {
delete acc[index];
return acc; // return no diff
}
return acc.slice(0, index).concat(rightItem).concat(acc.slice(index + 1)); // return updated key
}, deletedValues);
}
return Object.keys(r).reduce((acc, key) => {
if (!hasOwnProperty(l, key)) return { ...acc, [key]: r[key] }; // return added r key
const difference = diff(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 diff;