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