@@ -24,42 +24,158 @@ export interface Delta {
24
24
[ key : number ] : any ;
25
25
}
26
26
27
- export interface DiffContext {
27
+ export class Context {
28
+ nested : boolean ;
29
+ exiting ?: boolean ;
30
+ options : Config ;
31
+ parent ?: PatchContext ;
32
+ childName ?: string ;
33
+ children ?: PatchContext [ ] ;
34
+ root ?: PatchContext ;
35
+ next ?: PatchContext ;
36
+ nextAfterChildren ?: PatchContext ;
37
+ hasResult : boolean ;
38
+ setResult ( result : any ) : Context ;
39
+ exit ( ) : Context ;
40
+ }
41
+
42
+ export class PatchContext extends Context {
43
+ pipe : "patch" ;
44
+ left : any ;
45
+ delta : Delta ;
46
+ }
47
+
48
+ export class DiffContext extends Context {
49
+ pipe : "diff" ;
28
50
left : any ;
29
51
right : any ;
30
52
}
31
53
54
+ export class ReverseContext extends Context {
55
+ pipe : "reverse" ;
56
+ delta : Delta ;
57
+ }
58
+
59
+ type FilterContext = PatchContext | DiffContext | ReverseContext ;
60
+
61
+ /**
62
+ * A plugin which can modify the diff(), patch() or reverse() operations
63
+ */
64
+ export interface Filter < TContext extends FilterContext > {
65
+ /**
66
+ * A function which is called at each stage of the operation and can update the context to modify the result
67
+ * @param context The current state of the operation
68
+ */
69
+ ( context : TContext ) : void ;
70
+
71
+ /**
72
+ * A unique name which can be used to insert other filters before/after, or remove/replace this filter
73
+ */
74
+ filterName : string ;
75
+ }
76
+
77
+ /**
78
+ * A collection of Filters run on each diff(), patch() or reverse() operation
79
+ */
80
+ export class Pipe < TContext extends FilterContext > {
81
+ /**
82
+ * Append one or more filters to the existing list
83
+ */
84
+ append ( ...filters : Filter < TContext > [ ] ) : void ;
85
+
86
+ /**
87
+ * Prepend one or more filters to the existing list
88
+ */
89
+ prepend ( ...filters : Filter < TContext > [ ] ) : void ;
90
+
91
+ /**
92
+ * Add one ore more filters after the specified filter
93
+ * @param filterName The name of the filter to insert before
94
+ * @param filters Filters to be inserted
95
+ */
96
+ after ( filterName : string , ...filters : Filter < TContext > [ ] ) : void ;
97
+
98
+ /**
99
+ * Add one ore more filters before the specified filter
100
+ * @param filterName The name of the filter to insert before
101
+ * @param filters Filters to be inserted
102
+ */
103
+ before ( filterName : string , ...filters : Filter < TContext > [ ] ) : void ;
104
+
105
+ /**
106
+ * Replace the specified filter with one ore more filters
107
+ * @param filterName The name of the filter to replace
108
+ * @param filters Filters to be inserted
109
+ */
110
+ replace ( filterName : string , ...filters : Filter < TContext > [ ] ) : void ;
111
+
112
+ /**
113
+ * Remove the filter with the specified name
114
+ * @param filterName The name of the filter to remove
115
+ */
116
+ remove ( filterName : string ) : void ;
117
+
118
+ /**
119
+ * Remove all filters from this pipe
120
+ */
121
+ clear ( ) : void ;
122
+
123
+ /**
124
+ * Return array of ordered filter names for this pipe
125
+ */
126
+ list ( ) : void ;
127
+ }
128
+
129
+ export class Processor {
130
+ constructor ( options ?: Config ) ;
131
+
132
+ pipes : {
133
+ patch : Pipe < PatchContext > ;
134
+ diff : Pipe < DiffContext > ;
135
+ reverse : Pipe < ReverseContext > ;
136
+ } ;
137
+ }
138
+
32
139
export interface Config {
33
140
// used to match objects when diffing arrays, by default only === operator is used
34
- objectHash ?: ( item : any ) => string ;
141
+ objectHash ?: ( item : any , index : number ) => string ;
142
+
35
143
arrays ?: {
36
144
// default true, detect items moved inside the array (otherwise they will be registered as remove+add)
37
145
detectMove : boolean ,
38
146
// default false, the value of items moved is not included in deltas
39
147
includeValueOnMove : boolean ,
40
148
} ;
149
+
41
150
textDiff ?: {
42
151
// default 60, minimum string length (left and right sides) to use text diff algorythm: google-diff-match-patch
43
152
minLength : number ,
44
153
} ;
45
- /*
46
- this optional function can be specified to ignore object properties (eg. volatile data)
47
- name: property name, present in either context.left or context.right objects
48
- context: the diff context (has context.left and context.right objects)
49
- */
154
+
155
+ /**
156
+ * this optional function can be specified to ignore object properties (eg. volatile data)
157
+ * @param name property name, present in either context.left or context.right objects
158
+ * @param context the diff context (has context.left and context.right objects)
159
+ */
160
+ /**
161
+ *
162
+ */
50
163
propertyFilter ?: ( name : string , context : DiffContext ) => boolean ;
51
- /*
52
- default false. if true, values in the obtained delta will be cloned (using jsondiffpatch.clone by default),
53
- to ensure delta keeps no references to left or right objects. this becomes useful if you're diffing and patching
54
- the same objects multiple times without serializing deltas.
55
164
56
- instead of true, a function can be specified here to provide a custom clone(value)
165
+ /**
166
+ * default false. if true, values in the obtained delta will be cloned (using jsondiffpatch.clone by default),
167
+ * to ensure delta keeps no references to left or right objects. this becomes useful if you're diffing and patching
168
+ * the same objects multiple times without serializing deltas.
169
+ *
170
+ * instead of true, a function can be specified here to provide a custom clone(value)
57
171
*/
58
172
cloneDiffValues ?: boolean | ( ( value : any ) => any ) ;
59
173
}
60
174
61
175
export class DiffPatcher {
62
- constructor ( options ?: any ) ;
176
+ constructor ( options ?: Config ) ;
177
+
178
+ processor : Processor ;
63
179
64
180
clone : ( value : any ) => any ;
65
181
diff : ( left : any , right : any ) => Delta | undefined ;
0 commit comments