From d3dd12701cdae76aa1536c57b0b5f73a2446243a Mon Sep 17 00:00:00 2001 From: vvakame Date: Thu, 10 Apr 2014 18:56:36 +0900 Subject: [PATCH 1/2] add diff/diff.d.ts --- README.md | 1 + diff/diff-tests.ts | 14 ++++++++++++ diff/diff.d.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 diff/diff-tests.ts create mode 100644 diff/diff.d.ts diff --git a/README.md b/README.md index 4e8d6c170..3ef5c4d61 100755 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ List of Definitions * [d3.js](http://d3js.org/) (from TypeScript samples) * [dhtmlxGantt](http://dhtmlx.com/docs/products/dhtmlxGantt) (by [Maksim Kozhukh](http://github.com/mkozhukh)) * [dhtmlxScheduler](http://dhtmlx.com/docs/products/dhtmlxScheduler) (by [Maksim Kozhukh](http://github.com/mkozhukh)) +* [diff](https://github.com/kpdecker/jsdiff) (by [vvakame](http://github.com/vvakame)) * [Dock Spawn](http://dockspawn.com) (by [Drew Noakes](https://drewnoakes.com)) * [docCookies](https://developer.mozilla.org/en-US/docs/Web/API/document.cookie) (by [Jon Egerton](https://github.com/jonegerton)) * [domo](http://domo-js.com/) (by [Steve Fenton](https://github.com/Steve-Fenton)) diff --git a/diff/diff-tests.ts b/diff/diff-tests.ts new file mode 100644 index 000000000..1588f5947 --- /dev/null +++ b/diff/diff-tests.ts @@ -0,0 +1,14 @@ +/// + +import jsdiff = require('diff'); + +var one = 'beep boop'; +var other = 'beep boob blah'; + +var diff = jsdiff.diffChars(one, other); + +diff.forEach(function (part) { + var mark = part.added ? '+' : + part.removed ? '-' : ' '; + console.log(mark + " " + part.value); +}); diff --git a/diff/diff.d.ts b/diff/diff.d.ts new file mode 100644 index 000000000..9ccbb10a8 --- /dev/null +++ b/diff/diff.d.ts @@ -0,0 +1,57 @@ +// Type definitions for diff +// Project: https://github.com/kpdecker/jsdiff +// Definitions by: vvakame +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module JsDiff { + interface IDiffResult { + value: string; + added?: boolean; + removed?: boolean; + } + + interface IBestPath { + newPos: number; + componenets: IDiffResult[]; + } + + class Diff { + ignoreWhitespace:boolean; + + constructor(ignoreWhitespace?:boolean); + + diff(oldString:string, newString:string):IDiffResult[]; + + pushComponent(components:IDiffResult[], value:string, added:boolean, removed:boolean):void; + + extractCommon(basePath:IBestPath, newString:string, oldString:string, diagonalPath:number):number; + + equals(left:string, right:string):boolean; + + join(left:string, right:string):string; + + tokenize(value:string):any; // return types are string or string[] + } + + function diffChars(oldStr:string, newStr:string):IDiffResult[]; + + function diffWords(oldStr:string, newStr:string):IDiffResult[]; + + function diffWordsWithSpace(oldStr:string, newStr:string):IDiffResult[]; + + function diffLines(oldStr:string, newStr:string):IDiffResult[]; + + function diffCss(oldStr:string, newStr:string):IDiffResult[]; + + function createPatch(fileName:string, oldStr:string, newStr:string, oldHeader:string, newHeader:string):string; + + function applyPatch(oldStr:string, uniDiff:string):string; + + function convertChangesToXML(changes:IDiffResult[]):string; + + function convertChangesToDMP(changes:IDiffResult[]):{0: number; 1:string;}[]; +} + +declare module "diff" { + export = JsDiff; +} From 8fd7ae819606ad4a47d15bfdba676b0c0d982e1f Mon Sep 17 00:00:00 2001 From: vvakame Date: Thu, 10 Apr 2014 19:07:43 +0900 Subject: [PATCH 2/2] improve diff-tests.ts --- diff/diff-tests.ts | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/diff/diff-tests.ts b/diff/diff-tests.ts index 1588f5947..46d367bf6 100644 --- a/diff/diff-tests.ts +++ b/diff/diff-tests.ts @@ -12,3 +12,42 @@ diff.forEach(function (part) { part.removed ? '-' : ' '; console.log(mark + " " + part.value); }); + +// -------------------------- + +class LineDiffWithoutWhitespace extends jsdiff.Diff { + tokenize(value:string):any { + return value.split(/^/m); + } + + equals(left:string, right:string):boolean { + return left.trim() === right.trim(); + } +} + +var obj = new LineDiffWithoutWhitespace(true); +var diff = obj.diff(one, other); +printDiff(diff); + +function printDiff(diff:jsdiff.IDiffResult[]) { + function addLineHeader(decorator:string, str:string) { + return str.split("\n").map((line, index, array) => { + if (index === array.length - 1 && line === "") { + return line; + } else { + return decorator + line; + } + }).join("\n"); + } + + diff.forEach((part)=> { + if (part.added) { + console.log(addLineHeader("+", part.value)); + } else if (part.removed) { + console.log(addLineHeader("-", part.value)); + } else { + console.log(addLineHeader(" ", part.value)); + } + }); + +} \ No newline at end of file