Skip to content

Commit 2e1b820

Browse files
committed
Add typings for Pipe API
1 parent 0c4323e commit 2e1b820

File tree

1 file changed

+133
-17
lines changed

1 file changed

+133
-17
lines changed

src/index.d.ts

Lines changed: 133 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,164 @@
11
export interface Formatter {
2-
format(delta: Delta, original: any): string;
2+
format(delta: Delta, original: any): string;
33
}
44

55
export interface Delta {
66
[key: string]: any;
77
[key: number]: any;
88
}
99

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";
1133
left: any;
1234
right: any;
1335
}
1436

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+
15122
export interface Config {
16123
// 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+
18126
arrays?: {
19127
// default true, detect items moved inside the array (otherwise they will be registered as remove+add)
20128
detectMove: boolean,
21129
// default false, the value of items moved is not included in deltas
22130
includeValueOnMove: boolean,
23131
};
132+
24133
textDiff?: {
25134
// default 60, minimum string length (left and right sides) to use text diff algorythm: google-diff-match-patch
26135
minLength: number,
27136
};
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+
*/
33146
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.
38147

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)
40154
*/
41155
cloneDiffValues?: boolean | ((value: any) => any);
42156
}
43157

44158
export class DiffPatcher {
45-
constructor(options?: any);
159+
constructor(options?: Config);
160+
161+
processor: Processor;
46162

47163
clone: (value: any) => any;
48164
diff: (left: any, right: any) => Delta | undefined;
@@ -52,9 +168,9 @@ export class DiffPatcher {
52168
}
53169

54170
export const formatters: {
55-
annotated: Formatter;
56-
console: Formatter;
57-
html: Formatter;
171+
annotated: Formatter;
172+
console: Formatter;
173+
html: Formatter;
58174
};
59175

60176
export const console: Formatter

0 commit comments

Comments
 (0)