-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathadded.ts
35 lines (29 loc) · 947 Bytes
/
added.ts
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
import type { DiffAddedType } from "./types";
import {
isEmpty,
isObject,
hasOwnProperty,
makeObjectWithoutPrototype,
} from "./utils";
const addedDiff = <T, U>(lhs: T, rhs: U): DiffAddedType<T, U> => {
if (
(lhs as unknown) === (rhs as unknown) ||
!isObject(lhs) ||
!isObject(rhs)
)
return {} as DiffAddedType<T, U>;
return Object.keys(rhs).reduce((acc, key) => {
if (hasOwnProperty(lhs, key)) {
const difference = addedDiff(
(lhs as Record<string, unknown>)[key],
(rhs as Record<string, unknown>)[key]
);
if (isObject(difference) && isEmpty(difference)) return acc;
acc[key] = difference;
return acc;
}
acc[key] = (rhs as Record<string, unknown>)[key];
return acc;
}, makeObjectWithoutPrototype()) as DiffAddedType<T, U>;
};
export default addedDiff;