UNPKG

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