UNPKG

16.9 kBTypeScriptView Raw
1export as namespace Diff;
2
3export type Callback<T> = (value: T) => void;
4
5export interface CallbackOptions<T> {
6 /**
7 * if provided, the diff will be computed in async mode to avoid blocking the event loop while the diff is calculated. The value of the `callback` option should be a function and will be passed the computed diff or patch as its first argument.
8 */
9 callback: Callback<T>;
10}
11
12export interface BaseOptions {
13 /**
14 * `true` to ignore casing difference.
15 * @default false
16 */
17 ignoreCase?: boolean | undefined;
18
19 /**
20 * a number specifying the maximum edit distance to consider between the old and new texts. If the edit distance is higher than this, jsdiff will return `undefined` instead of a diff. You can use this to limit the computational cost of diffing large, very different texts by giving up early if the cost will be huge. Works for functions that return change objects and also for `structuredPatch`, but not other patch-generation functions.
21 */
22 maxEditLength?: number | undefined;
23
24 /**
25 * if `true`, the array of change objects returned will contain one change object per token (e.g. one per line if calling `diffLines`), instead of runs of consecutive tokens that are all added / all removed / all conserved being combined into a single change object.
26 */
27 oneChangePerToken?: boolean | undefined;
28}
29
30export interface WordsOptions extends BaseOptions {
31 /**
32 * `true` to ignore leading and trailing whitespace. This is the same as `diffWords()`.
33 */
34 ignoreWhitespace?: boolean | undefined;
35
36 /**
37 * An optional [`Intl.Segmenter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter) object (which must have a `granularity` of `'word'`) for `diffWords` to use to split the text into words.
38 *
39 * By default, `diffWords` does not use an `Intl.Segmenter`, just some regexes for splitting text into words. This will tend to give worse results than `Intl.Segmenter` would, but ensures the results are consistent across environments; `Intl.Segmenter` behaviour is only loosely specced and the implementations in browsers could in principle change dramatically in future. If you want to use `diffWords` with an `Intl.Segmenter` but ensure it behaves the same whatever environment you run it in, use an `Intl.Segmenter` polyfill instead of the JavaScript engine's native `Intl.Segmenter` implementation.
40 *
41 * Using an `Intl.Segmenter` should allow better word-level diffing of non-English text than the default behaviour. For instance, `Intl.Segmenter`s can generally identify via built-in dictionaries which sequences of adjacent Chinese characters form words, allowing word-level diffing of Chinese. By specifying a language when instantiating the segmenter (e.g. `new Intl.Segmenter('sv', {granularity: 'word'})`) you can also support language-specific rules, like treating Swedish's colon separated contractions (like *k:a* for *kyrka*) as single words; by default this would be seen as two words separated by a colon.
42 */
43 intlSegmenter?: Intl.Segmenter | undefined;
44}
45
46export interface LinesOptions extends BaseOptions {
47 /**
48 * `true` to ignore a missing newline character at the end of the last line when comparing it to other lines. (By default, the line `'b\n'` in text `'a\nb\nc'` is not considered equal to the line `'b'` in text `'a\nb'`; this option makes them be considered equal.) Ignored if `ignoreWhitespace` or `newlineIsToken` are also true.
49 */
50 ignoreNewlineAtEof?: boolean | undefined;
51
52 /**
53 * `true` to ignore leading and trailing whitespace. This is the same as `diffTrimmedLines()`.
54 */
55 ignoreWhitespace?: boolean | undefined;
56
57 /**
58 * `true` to treat newline characters as separate tokens. This allows for changes to the newline structure
59 * to occur independently of the line content and to be treated as such. In general this is the more
60 * human friendly form of `diffLines()` and `diffLines()` is better suited for patches and other computer
61 * friendly output.
62 */
63 newlineIsToken?: boolean | undefined;
64}
65
66export interface JsonOptions extends LinesOptions {
67 /**
68 * Replacer used to stringify the properties of the passed objects.
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
80export interface ArrayOptions<TLeft, TRight> extends BaseOptions {
81 /**
82 * Comparator for custom equality checks.
83 */
84 comparator?: ((left: TLeft, right: TRight) => boolean) | undefined;
85}
86
87export 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
95export 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
123export 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
129export 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
145export interface ArrayChange<T> {
146 value: T[];
147 count?: number | undefined;
148 added?: boolean | undefined;
149 removed?: boolean | undefined;
150}
151
152export 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
161export interface Hunk {
162 oldStart: number;
163 oldLines: number;
164 newStart: number;
165 newLines: number;
166 lines: string[];
167}
168
169export interface BestPath {
170 newPos: number;
171 components: Change[];
172}
173
174export 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 */
208export function diffChars(oldStr: string, newStr: string, options?: BaseOptions): Change[];
209export 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 */
220export function diffWords(oldStr: string, newStr: string, options?: WordsOptions): Change[];
221export 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 */
232export function diffWordsWithSpace(
233 oldStr: string,
234 newStr: string,
235 options?: WordsOptions,
236): Change[];
237export 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 */
248export function diffLines(oldStr: string, newStr: string, options?: LinesOptions): Change[];
249export 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 */
260export function diffTrimmedLines(oldStr: string, newStr: string, options?: LinesOptions): Change[];
261export 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 */
272export function diffSentences(oldStr: string, newStr: string, options?: BaseOptions): Change[];
273export 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 */
284export function diffCss(oldStr: string, newStr: string, options?: BaseOptions): Change[];
285export 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 */
297export function diffJson(
298 oldObj: string | object,
299 newObj: string | object,
300 options?: JsonOptions,
301): Change[];
302export 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 */
313export 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 */
329export 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;
338export 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 */
358export function createPatch(
359 fileName: string,
360 oldStr: string,
361 newStr: string,
362 oldHeader?: string,
363 newHeader?: string,
364 options?: PatchOptions,
365): string;
366export 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 */
387export 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;
396export 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 */
412export 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 */
431export 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 */
438export function parsePatch(uniDiff: string): ParsedDiff[];
439
440/**
441 * Converts a list of changes to a serialized XML format.
442 */
443export function convertChangesToXML(changes: Change[]): string;
444
445/**
446 * Converts a list of changes to [DMP](http://code.google.com/p/google-diff-match-patch/wiki/API) format.
447 */
448export function convertChangesToDMP(changes: Change[]): Array<[1 | 0 | -1, string]>;
449
450export function merge(mine: string, theirs: string, base: string): ParsedDiff;
451
452/**
453 * Returns a new structured patch which when applied will undo the original `patch`.
454 * `patch` may be either a single structured patch object (as returned by `structuredPatch`) or an array of them (as returned by `parsePatch`).
455 */
456export function reversePatch(patch: ParsedDiff | ParsedDiff[]): ParsedDiff;
457
458export function canonicalize(obj: any, stack: any[], replacementStack: any[]): any;
459
460/**
461 * creates a unified diff patch.
462 * patch may be either a single structured patch object (as returned by structuredPatch)
463 * or an array of them (as returned by parsePatch).
464 */
465export function formatPatch(patch: ParsedDiff | ParsedDiff[]): string;
466
\No newline at end of file