1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export as namespace Diff;
|
10 |
|
11 | export type Callback = (err: undefined, value?: Change[]) => void;
|
12 |
|
13 | export interface CallbackOptions {
|
14 | |
15 |
|
16 |
|
17 | callback: Callback;
|
18 | }
|
19 |
|
20 | export interface BaseOptions {
|
21 | |
22 |
|
23 |
|
24 |
|
25 | ignoreCase?: boolean | undefined;
|
26 | }
|
27 |
|
28 | export interface WordsOptions extends BaseOptions {
|
29 | |
30 |
|
31 |
|
32 | ignoreWhitespace?: boolean | undefined;
|
33 | }
|
34 |
|
35 | export interface LinesOptions extends BaseOptions {
|
36 | |
37 |
|
38 |
|
39 | ignoreWhitespace?: boolean | undefined;
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | newlineIsToken?: boolean | undefined;
|
48 | }
|
49 |
|
50 | export interface JsonOptions extends LinesOptions {
|
51 | |
52 |
|
53 |
|
54 | stringifyReplacer?: ((key: string, value: any) => any) | undefined;
|
55 |
|
56 | /**
|
57 | * The value to use when `undefined` values in the passed objects are encountered during stringification.
|
58 | * Will only be used if `stringifyReplacer` option wasn't specified.
|
59 | * @default undefined
|
60 | */
|
61 | undefinedReplacement?: any;
|
62 | }
|
63 |
|
64 | export interface ArrayOptions<TLeft, TRight> extends BaseOptions {
|
65 | /**
|
66 | * Comparator for custom equality checks.
|
67 | */
|
68 | comparator?: ((left: TLeft, right: TRight) => boolean) | undefined;
|
69 | }
|
70 |
|
71 | export interface PatchOptions extends LinesOptions {
|
72 | /**
|
73 | * Describes how many lines of context should be included.
|
74 | * @default 4
|
75 | */
|
76 | context?: number | undefined;
|
77 | }
|
78 |
|
79 | export interface ApplyPatchOptions {
|
80 | /**
|
81 | * Number of lines that are allowed to differ before rejecting a patch.
|
82 | * @default 0
|
83 | */
|
84 | fuzzFactor?: number | undefined;
|
85 |
|
86 | /**
|
87 | * Callback used to compare to given lines to determine if they should be considered equal when patching.
|
88 | * Should return `false` if the lines should be rejected.
|
89 | *
|
90 | * @default strict equality
|
91 | */
|
92 | compareLine?: ((
|
93 | lineNumber: number,
|
94 | line: string,
|
95 | operation: '-' | ' ',
|
96 | patchContent: string
|
97 | ) => boolean) | undefined;
|
98 | }
|
99 |
|
100 | export interface ApplyPatchesOptions extends ApplyPatchOptions {
|
101 | loadFile(index: ParsedDiff, callback: (err: any, data: string) => void): void;
|
102 | patched(index: ParsedDiff, content: string, callback: (err: any) => void): void;
|
103 | complete(err: any): void;
|
104 | }
|
105 |
|
106 | export interface Change {
|
107 | count?: number | undefined;
|
108 | /**
|
109 | * Text content.
|
110 | */
|
111 | value: string;
|
112 | /**
|
113 | * `true` if the value was inserted into the new string.
|
114 | */
|
115 | added?: boolean | undefined;
|
116 | /**
|
117 | * `true` if the value was removed from the old string.
|
118 | */
|
119 | removed?: boolean | undefined;
|
120 | }
|
121 |
|
122 | export interface ArrayChange<T> {
|
123 | value: T[];
|
124 | count?: number | undefined;
|
125 | added?: boolean | undefined;
|
126 | removed?: boolean | undefined;
|
127 | }
|
128 |
|
129 | export interface ParsedDiff {
|
130 | index?: string | undefined;
|
131 | oldFileName?: string | undefined;
|
132 | newFileName?: string | undefined;
|
133 | oldHeader?: string | undefined;
|
134 | newHeader?: string | undefined;
|
135 | hunks: Hunk[];
|
136 | }
|
137 |
|
138 | export interface Hunk {
|
139 | oldStart: number;
|
140 | oldLines: number;
|
141 | newStart: number;
|
142 | newLines: number;
|
143 | lines: string[];
|
144 | // Line Delimiters is only returned by parsePatch()
|
145 | linedelimiters?: string[];
|
146 | }
|
147 |
|
148 | export interface BestPath {
|
149 | newPos: number;
|
150 | componenets: Change[];
|
151 | }
|
152 |
|
153 | export class Diff {
|
154 | diff(
|
155 | oldString: string,
|
156 | newString: string,
|
157 | options?: Callback | (ArrayOptions<any, any> & Partial<CallbackOptions>)
|
158 | ): Change[];
|
159 |
|
160 | pushComponent(components: Change[], added: boolean, removed: boolean): void;
|
161 |
|
162 | extractCommon(
|
163 | basePath: BestPath,
|
164 | newString: string,
|
165 | oldString: string,
|
166 | diagonalPath: number
|
167 | ): number;
|
168 |
|
169 | equals(left: any, right: any): boolean;
|
170 |
|
171 | removeEmpty(array: any[]): any[];
|
172 |
|
173 | castInput(value: any): any;
|
174 |
|
175 | join(chars: string[]): string;
|
176 |
|
177 | tokenize(value: string): any; // return types are string or string[]
|
178 | }
|
179 |
|
180 | /**
|
181 | * Diffs two blocks of text, comparing character by character.
|
182 | *
|
183 | * @returns A list of change objects.
|
184 | */
|
185 | export function diffChars(oldStr: string, newStr: string, options?: BaseOptions): Change[];
|
186 | export function diffChars(
|
187 | oldStr: string,
|
188 | newStr: string,
|
189 | options: Callback | (BaseOptions & CallbackOptions)
|
190 | ): void;
|
191 |
|
192 | /**
|
193 | * Diffs two blocks of text, comparing word by word, ignoring whitespace.
|
194 | *
|
195 | * @returns A list of change objects.
|
196 | */
|
197 | export function diffWords(oldStr: string, newStr: string, options?: WordsOptions): Change[];
|
198 | export function diffWords(
|
199 | oldStr: string,
|
200 | newStr: string,
|
201 | options: Callback | (WordsOptions & CallbackOptions)
|
202 | ): void;
|
203 |
|
204 | /**
|
205 | * Diffs two blocks of text, comparing word by word, treating whitespace as significant.
|
206 | *
|
207 | * @returns A list of change objects.
|
208 | */
|
209 | export function diffWordsWithSpace(
|
210 | oldStr: string,
|
211 | newStr: string,
|
212 | options?: WordsOptions
|
213 | ): Change[];
|
214 | export function diffWordsWithSpace(
|
215 | oldStr: string,
|
216 | newStr: string,
|
217 | options: Callback | (WordsOptions & CallbackOptions)
|
218 | ): void;
|
219 |
|
220 | /**
|
221 | * Diffs two blocks of text, comparing line by line.
|
222 | *
|
223 | * @returns A list of change objects.
|
224 | */
|
225 | export function diffLines(oldStr: string, newStr: string, options?: LinesOptions): Change[];
|
226 | export function diffLines(
|
227 | oldStr: string,
|
228 | newStr: string,
|
229 | options: Callback | (LinesOptions & CallbackOptions)
|
230 | ): void;
|
231 |
|
232 | /**
|
233 | * Diffs two blocks of text, comparing line by line, ignoring leading and trailing whitespace.
|
234 | *
|
235 | * @returns A list of change objects.
|
236 | */
|
237 | export function diffTrimmedLines(oldStr: string, newStr: string, options?: LinesOptions): Change[];
|
238 | export function diffTrimmedLines(
|
239 | oldStr: string,
|
240 | newStr: string,
|
241 | options: Callback | (LinesOptions & CallbackOptions)
|
242 | ): void;
|
243 |
|
244 | /**
|
245 | * Diffs two blocks of text, comparing sentence by sentence.
|
246 | *
|
247 | * @returns A list of change objects.
|
248 | */
|
249 | export function diffSentences(oldStr: string, newStr: string, options?: BaseOptions): Change[];
|
250 | export function diffSentences(
|
251 | oldStr: string,
|
252 | newStr: string,
|
253 | options: Callback | (BaseOptions & CallbackOptions)
|
254 | ): void;
|
255 |
|
256 | /**
|
257 | * Diffs two blocks of text, comparing CSS tokens.
|
258 | *
|
259 | * @returns A list of change objects.
|
260 | */
|
261 | export function diffCss(oldStr: string, newStr: string, options?: BaseOptions): Change[];
|
262 | export function diffCss(
|
263 | oldStr: string,
|
264 | newStr: string,
|
265 | options: Callback | (BaseOptions & CallbackOptions)
|
266 | ): void;
|
267 |
|
268 | /**
|
269 | * Diffs two JSON objects, comparing the fields defined on each. The order of fields, etc does not matter
|
270 | * in this comparison.
|
271 | *
|
272 | * @returns A list of change objects.
|
273 | */
|
274 | export function diffJson(
|
275 | oldObj: string | object,
|
276 | newObj: string | object,
|
277 | options?: JsonOptions
|
278 | ): Change[];
|
279 | export function diffJson(
|
280 | oldObj: string | object,
|
281 | newObj: string | object,
|
282 | options: Callback | (JsonOptions & CallbackOptions)
|
283 | ): void;
|
284 |
|
285 | /**
|
286 | * Diffs two arrays, comparing each item for strict equality (`===`).
|
287 | *
|
288 | * @returns A list of change objects.
|
289 | */
|
290 | export function diffArrays<TOld, TNew>(
|
291 | oldArr: TOld[],
|
292 | newArr: TNew[],
|
293 | options?: ArrayOptions<TOld, TNew>
|
294 | ): Array<ArrayChange<TOld | TNew>>;
|
295 |
|
296 | /**
|
297 | * Creates a unified diff patch.
|
298 | *
|
299 | * @param oldFileName String to be output in the filename section of the patch for the removals.
|
300 | * @param newFileName String to be output in the filename section of the patch for the additions.
|
301 | * @param oldStr Original string value.
|
302 | * @param newStr New string value.
|
303 | * @param oldHeader Additional information to include in the old file header.
|
304 | * @param newHeader Additional information to include in the new file header.
|
305 | */
|
306 | export function createTwoFilesPatch(
|
307 | oldFileName: string,
|
308 | newFileName: string,
|
309 | oldStr: string,
|
310 | newStr: string,
|
311 | oldHeader?: string,
|
312 | newHeader?: string,
|
313 | options?: PatchOptions
|
314 | ): string;
|
315 |
|
316 | /**
|
317 | * Creates a unified diff patch.
|
318 | * Just like `createTwoFilesPatch()`, but with `oldFileName` being equal to `newFileName`.
|
319 | *
|
320 | * @param fileName String to be output in the filename section.
|
321 | * @param oldStr Original string value.
|
322 | * @param newStr New string value.
|
323 | * @param oldHeader Additional information to include in the old file header.
|
324 | * @param newHeader Additional information to include in the new file header.
|
325 | */
|
326 | export function createPatch(
|
327 | fileName: string,
|
328 | oldStr: string,
|
329 | newStr: string,
|
330 | oldHeader?: string,
|
331 | newHeader?: string,
|
332 | options?: PatchOptions
|
333 | ): string;
|
334 |
|
335 | /**
|
336 | * This method is similar to `createTwoFilesPatch()`, but returns a data structure suitable for further processing.
|
337 | * Parameters are the same as `createTwoFilesPatch()`.
|
338 | *
|
339 | * @param oldFileName String to be output in the `oldFileName` hunk property.
|
340 | * @param newFileName String to be output in the `newFileName` hunk property.
|
341 | * @param oldStr Original string value.
|
342 | * @param newStr New string value.
|
343 | * @param oldHeader Additional information to include in the `oldHeader` hunk property.
|
344 | * @param newHeader Additional information to include in the `newHeader` hunk property.
|
345 | * @returns An object with an array of hunk objects.
|
346 | */
|
347 | export function structuredPatch(
|
348 | oldFileName: string,
|
349 | newFileName: string,
|
350 | oldStr: string,
|
351 | newStr: string,
|
352 | oldHeader?: string,
|
353 | newHeader?: string,
|
354 | options?: PatchOptions
|
355 | ): ParsedDiff;
|
356 |
|
357 | /**
|
358 | * Applies a unified diff patch.
|
359 | *
|
360 | * @param patch May be a string diff or the output from the `parsePatch()` or `structuredPatch()` methods.
|
361 | * @returns A string containing new version of provided data. false when failed
|
362 | */
|
363 | export function applyPatch(
|
364 | source: string,
|
365 | patch: string | ParsedDiff | [ParsedDiff],
|
366 | options?: ApplyPatchOptions
|
367 | ): string | false;
|
368 |
|
369 | /**
|
370 | * Applies one or more patches.
|
371 | * This method will iterate over the contents of the patch and apply to data provided through callbacks.
|
372 | *
|
373 | * The general flow for each patch index is:
|
374 | *
|
375 | * 1. `options.loadFile(index, callback)` is called. The caller should then load the contents of the file
|
376 | * and then pass that to the `callback(err, data)` callback. Passing an `err` will terminate further patch execution.
|
377 | * 2. `options.patched(index, content, callback)` is called once the patch has been applied. `content` will be
|
378 | * the return value from `applyPatch()`. When it's ready, the caller should call `callback(err)` callback.
|
379 | * Passing an `err` will terminate further patch execution.
|
380 | * 3. Once all patches have been applied or an error occurs, the `options.complete(err)` callback is made.
|
381 | */
|
382 | export function applyPatches(patch: string | ParsedDiff[], options: ApplyPatchesOptions): void;
|
383 |
|
384 | /**
|
385 | * Parses a patch into structured data.
|
386 | *
|
387 | * @returns A JSON object representation of the a patch, suitable for use with the `applyPatch()` method.
|
388 | */
|
389 | export function parsePatch(diffStr: string, options?: { strict?: boolean | undefined }): ParsedDiff[];
|
390 |
|
391 | /**
|
392 | * Converts a list of changes to a serialized XML format.
|
393 | */
|
394 | export function convertChangesToXML(changes: Change[]): string;
|
395 |
|
396 | /**
|
397 | * Converts a list of changes to [DMP](http:
|
398 | */
|
399 | export function convertChangesToDMP(changes: Change[]): Array<[1 | 0 | -1, string]>;
|
400 |
|
401 | export function merge(mine: string, theirs: string, base: string): ParsedDiff;
|
402 |
|
403 | export function canonicalize(obj: any, stack: any[], replacementStack: any[]): any;
|
404 |
|
\ | No newline at end of file |