UNPKG

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