Skip to content

Commit

Permalink
the callback option is now supported by structuredPatch, `createP…
Browse files Browse the repository at this point in the history
…atch`, and `createTwoFilesPatch`

kpdecker/jsdiff#521
  • Loading branch information
ryota-ka committed Oct 8, 2024
1 parent 9c34f30 commit f193aff
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
6 changes: 6 additions & 0 deletions types/diff/diff-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ Diff.diffLines(
oneChangePerToken: true,
},
);
// $ExpectType void
Diff.createPatch("filename", "A", "a", undefined, undefined, {
callback: (value) => {
value; // $ExpectType string
},
});

const diffArraysResult = Diff.diffArrays(["a", "b", "c"], ["a", "c", "d"]);
diffArraysResult.forEach(result => {
Expand Down
52 changes: 39 additions & 13 deletions types/diff/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export as namespace Diff;

export type Callback = (value: Change[]) => void;
export type Callback<T> = (value: T) => void;

export interface CallbackOptions {
export interface CallbackOptions<T> {
/**
* Callback to call with the result instead of returning the result directly.
* 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.
*/
callback: Callback;
callback: Callback<T>;
}

export interface BaseOptions {
Expand Down Expand Up @@ -161,7 +161,7 @@ export class Diff {
diff(
oldString: string,
newString: string,
options?: Callback | (ArrayOptions<any, any> & Partial<CallbackOptions>),
options?: Callback<Change[]> | (ArrayOptions<any, any> & Partial<CallbackOptions<Change[]>>),
): Change[];

pushComponent(components: Change[], added: boolean, removed: boolean): void;
Expand Down Expand Up @@ -195,7 +195,7 @@ export function diffChars(oldStr: string, newStr: string, options?: BaseOptions)
export function diffChars(
oldStr: string,
newStr: string,
options: Callback | (BaseOptions & CallbackOptions),
options: Callback<Change[]> | (BaseOptions & CallbackOptions<Change[]>),
): void;

/**
Expand All @@ -207,7 +207,7 @@ export function diffWords(oldStr: string, newStr: string, options?: WordsOptions
export function diffWords(
oldStr: string,
newStr: string,
options: Callback | (WordsOptions & CallbackOptions),
options: Callback<Change[]> | (WordsOptions & CallbackOptions<Change[]>),
): void;

/**
Expand All @@ -223,7 +223,7 @@ export function diffWordsWithSpace(
export function diffWordsWithSpace(
oldStr: string,
newStr: string,
options: Callback | (WordsOptions & CallbackOptions),
options: Callback<Change[]> | (WordsOptions & CallbackOptions<Change[]>),
): void;

/**
Expand All @@ -235,7 +235,7 @@ export function diffLines(oldStr: string, newStr: string, options?: LinesOptions
export function diffLines(
oldStr: string,
newStr: string,
options: Callback | (LinesOptions & CallbackOptions),
options: Callback<Change[]> | (LinesOptions & CallbackOptions<Change[]>),
): void;

/**
Expand All @@ -247,7 +247,7 @@ export function diffTrimmedLines(oldStr: string, newStr: string, options?: Lines
export function diffTrimmedLines(
oldStr: string,
newStr: string,
options: Callback | (LinesOptions & CallbackOptions),
options: Callback<Change[]> | (LinesOptions & CallbackOptions<Change[]>),
): void;

/**
Expand All @@ -259,7 +259,7 @@ export function diffSentences(oldStr: string, newStr: string, options?: BaseOpti
export function diffSentences(
oldStr: string,
newStr: string,
options: Callback | (BaseOptions & CallbackOptions),
options: Callback<Change[]> | (BaseOptions & CallbackOptions<Change[]>),
): void;

/**
Expand All @@ -271,7 +271,7 @@ export function diffCss(oldStr: string, newStr: string, options?: BaseOptions):
export function diffCss(
oldStr: string,
newStr: string,
options: Callback | (BaseOptions & CallbackOptions),
options: Callback<Change[]> | (BaseOptions & CallbackOptions<Change[]>),
): void;

/**
Expand All @@ -288,7 +288,7 @@ export function diffJson(
export function diffJson(
oldObj: string | object,
newObj: string | object,
options: Callback | (JsonOptions & CallbackOptions),
options: Callback<Change[]> | (JsonOptions & CallbackOptions<Change[]>),
): void;

/**
Expand Down Expand Up @@ -321,6 +321,15 @@ export function createTwoFilesPatch(
newHeader?: string,
options?: PatchOptions,
): string;
export function createTwoFilesPatch(
oldFileName: string,
newFileName: string,
oldStr: string,
newStr: string,
oldHeader?: string,
newHeader?: string,
options?: PatchOptions & CallbackOptions<string>,
): void;

/**
* Creates a unified diff patch.
Expand All @@ -340,6 +349,14 @@ export function createPatch(
newHeader?: string,
options?: PatchOptions,
): string;
export function createPatch(
fileName: string,
oldStr: string,
newStr: string,
oldHeader?: string,
newHeader?: string,
options?: PatchOptions & CallbackOptions<string>,
): void;

/**
* This method is similar to `createTwoFilesPatch()`, but returns a data structure suitable for further processing.
Expand All @@ -362,6 +379,15 @@ export function structuredPatch(
newHeader?: string,
options?: PatchOptions,
): ParsedDiff;
export function structuredPatch(
oldFileName: string,
newFileName: string,
oldStr: string,
newStr: string,
oldHeader?: string,
newHeader?: string,
options?: PatchOptions & CallbackOptions<ParsedDiff>,
): void;

/**
* Applies a unified diff patch.
Expand Down

0 comments on commit f193aff

Please sign in to comment.