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