diff --git a/diff/diff-tests.ts b/diff/diff-tests.ts index 46d367bf68..20d75b38ea 100644 --- a/diff/diff-tests.ts +++ b/diff/diff-tests.ts @@ -49,5 +49,44 @@ function printDiff(diff:jsdiff.IDiffResult[]) { console.log(addLineHeader(" ", part.value)); } }); +} -} \ No newline at end of file +function verifyPatchMethods(oldStr: string, newStr: string, uniDiff: jsdiff.IUniDiff) { + var verifyPatch = jsdiff.parsePatch( + jsdiff.createTwoFilesPatch("oldFile.ts", "newFile.ts", oldStr, newStr, + "old", "new", { context: 1 })); + if (JSON.stringify(verifyPatch) !== JSON.stringify(uniDiff)) { + console.error("Patch did not match uniDiff"); + } +} + +function verifyApplyMethods(oldStr: string, newStr: string, uniDiff: jsdiff.IUniDiff) { + var verifyApply = [ + jsdiff.applyPatch(oldStr, uniDiff), + jsdiff.applyPatch(oldStr, [uniDiff]) + ]; + jsdiff.applyPatches([uniDiff], { + loadFile: (index: number, callback: (err: Error, data: string) => void) => { + callback(undefined, one); + }, + patched: (index: number, content: string) => { + verifyApply.push(content); + }, + complete: (err?: Error) => { + if (err) { + console.error(err); + } + + verifyApply.forEach(result => { + if (result !== newStr) { + console.error("Result did not match newStr"); + } + }); + } + }); +} + +verifyPatchMethods(one, other, uniDiff); +var uniDiff = jsdiff.structuredPatch("oldFile.ts", "newFile.ts", one, other, + "old", "new", { context: 1 }); +verifyApplyMethods(one, other, uniDiff); diff --git a/diff/diff.d.ts b/diff/diff.d.ts index b1f947300b..fc4708c2a1 100644 --- a/diff/diff.d.ts +++ b/diff/diff.d.ts @@ -16,6 +16,22 @@ declare namespace JsDiff { componenets: IDiffResult[]; } + interface IHunk { + oldStart: number; + oldLines: number; + newStart: number; + newLines: number; + lines: string[]; + } + + interface IUniDiff { + oldFileName: string; + newFileName: string; + oldHeader: string; + newHeader: string; + hunks: IHunk[]; + } + class Diff { ignoreWhitespace:boolean; @@ -46,9 +62,21 @@ declare namespace JsDiff { function diffCss(oldStr:string, newStr:string):IDiffResult[]; - function createPatch(fileName:string, oldStr:string, newStr:string, oldHeader:string, newHeader:string):string; + function createPatch(fileName: string, oldStr: string, newStr: string, oldHeader: string, newHeader: string, options?: {context: number}): string; - function applyPatch(oldStr:string, uniDiff:string):string; + function createTwoFilesPatch(oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader: string, newHeader: string, options?: {context: number}): string; + + function structuredPatch(oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader: string, newHeader: string, options?: {context: number}): IUniDiff; + + function applyPatch(oldStr: string, uniDiff: string | IUniDiff | IUniDiff[]): string; + + function applyPatches(uniDiff: IUniDiff[], options: { + loadFile: (index: number, callback: (err: Error, data: string) => void) => void, + patched: (index: number, content: string) => void, + complete: (err?: Error) => void + }): void; + + function parsePatch(diffStr: string, options?: {strict: boolean}): IUniDiff[]; function convertChangesToXML(changes:IDiffResult[]):string;