UNPKG

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