UNPKG

3.15 kBTypeScriptView Raw
1declare function deepDiff<LHS, RHS = LHS>(
2 lhs: LHS,
3 rhs: RHS,
4 prefilter?: deepDiff.PreFilter<LHS, RHS>,
5): Array<deepDiff.Diff<LHS, RHS>> | undefined;
6declare function deepDiff<LHS, RHS = LHS>(
7 lhs: LHS,
8 rhs: RHS,
9 prefilter?: deepDiff.PreFilter<LHS, RHS>,
10 acc?: deepDiff.Accumulator<LHS, RHS>,
11): deepDiff.Accumulator<LHS, RHS>;
12declare namespace deepDiff {
13 interface DiffNew<RHS> {
14 kind: "N";
15 path?: any[] | undefined;
16 rhs: RHS;
17 }
18
19 interface DiffDeleted<LHS> {
20 kind: "D";
21 path?: any[] | undefined;
22 lhs: LHS;
23 }
24
25 interface DiffEdit<LHS, RHS = LHS> {
26 kind: "E";
27 path?: any[] | undefined;
28 lhs: LHS;
29 rhs: RHS;
30 }
31
32 interface DiffArray<LHS, RHS = LHS> {
33 kind: "A";
34 path?: any[] | undefined;
35 index: number;
36 item: Diff<LHS, RHS>;
37 }
38
39 type Diff<LHS, RHS = LHS> = DiffNew<RHS> | DiffDeleted<LHS> | DiffEdit<LHS, RHS> | DiffArray<LHS, RHS>;
40
41 type PreFilterFunction = (path: any[], key: any) => boolean;
42
43 interface PreFilterObject<LHS, RHS = LHS> {
44 prefilter?(path: any[], key: any): boolean;
45 normalize?(currentPath: any, key: any, lhs: LHS, rhs: RHS): [LHS, RHS] | undefined;
46 }
47 type PreFilter<LHS, RHS = LHS> = PreFilterFunction | PreFilterObject<LHS, RHS>;
48
49 interface Accumulator<LHS, RHS = LHS> {
50 push(diff: Diff<LHS, RHS>): void;
51 length: number;
52 }
53
54 type Observer<LHS, RHS = LHS> = (diff: Diff<LHS, RHS>) => void;
55
56 type Filter<LHS, RHS = LHS> = (target: LHS, source: RHS, change: Diff<LHS, RHS>) => boolean;
57
58 function diff<LHS, RHS = LHS>(
59 lhs: LHS,
60 rhs: RHS,
61 prefilter?: PreFilter<LHS, RHS>,
62 ): Array<Diff<LHS, RHS>> | undefined;
63 function diff<LHS, RHS = LHS>(
64 lhs: LHS,
65 rhs: RHS,
66 prefilter?: PreFilter<LHS, RHS>,
67 acc?: Accumulator<LHS, RHS>,
68 ): Accumulator<LHS, RHS>;
69 function orderIndependentDiff<LHS, RHS = LHS>(
70 lhs: LHS,
71 rhs: RHS,
72 prefilter?: PreFilter<LHS, RHS>,
73 ): Array<Diff<LHS, RHS>> | undefined;
74 function orderIndependentDiff<LHS, RHS = LHS>(
75 lhs: LHS,
76 rhs: RHS,
77 prefilter?: PreFilter<LHS, RHS>,
78 acc?: Accumulator<LHS, RHS>,
79 ): Accumulator<LHS, RHS>;
80 function observableDiff<LHS, RHS = LHS>(
81 lhs: LHS,
82 rhs: RHS,
83 observer?: Observer<LHS, RHS>,
84 prefilter?: PreFilter<LHS, RHS>,
85 orderIndependent?: boolean,
86 ): Array<Diff<LHS, RHS>>;
87 function orderIndependentDeepDiff<LHS, RHS = LHS>(
88 lhs: LHS,
89 rhs: RHS,
90 changes: Array<Diff<LHS, RHS>>,
91 prefilter: PreFilter<LHS, RHS>,
92 path: any[],
93 key: any,
94 stack: any[],
95 ): void;
96 function orderIndepHash(object: any): number;
97 function applyDiff<LHS, RHS = LHS>(target: LHS, source: RHS, filter?: Filter<LHS, RHS>): void;
98 function applyChange<LHS>(target: LHS, source: any, change: Diff<LHS, any>): void;
99 function revertChange<LHS>(target: LHS, source: any, change: Diff<LHS, any>): void;
100}
101
102export = deepDiff;