From 9522b37f7e4f5625f31db4006e4c54efc449041a Mon Sep 17 00:00:00 2001 From: Austen Talbot Date: Wed, 5 Aug 2015 12:21:02 -0700 Subject: [PATCH 1/3] Added type def and test for diff-match-patch library --- diff-match-patch/diff-match-patch-tests.ts | 32 +++++++++++++++ diff-match-patch/diff-match-patch.d.ts | 46 ++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 diff-match-patch/diff-match-patch-tests.ts create mode 100644 diff-match-patch/diff-match-patch.d.ts diff --git a/diff-match-patch/diff-match-patch-tests.ts b/diff-match-patch/diff-match-patch-tests.ts new file mode 100644 index 000000000..f1e5d2d42 --- /dev/null +++ b/diff-match-patch/diff-match-patch-tests.ts @@ -0,0 +1,32 @@ +/// + +import DiffMatchPatch = require("diff-match-patch"); + +var oldValue = "hello world, how are you?"; +var newValue = "hello again world. how have you been?"; + +var diffEngine = new DiffMatchPatch.diff_match_patch(); +var diffs = diffEngine.diff_main(oldValue, newValue); +diffEngine.diff_cleanupSemantic(diffs); + +var changes = ""; +var pattern = ""; + +diffs.forEach(function(diff) { + var operation = diff[0]; // Operation (insert, delete, equal) + var text = diff[1]; // Text of change + + switch (operation) { + case DiffMatchPatch.DIFF_INSERT: + pattern += "I"; + break; + case DiffMatchPatch.DIFF_DELETE: + pattern += "D"; + break; + case DiffMatchPatch.DIFF_EQUAL: + pattern += "E"; + break; + } + + changes += text; +}); diff --git a/diff-match-patch/diff-match-patch.d.ts b/diff-match-patch/diff-match-patch.d.ts new file mode 100644 index 000000000..3a55b7769 --- /dev/null +++ b/diff-match-patch/diff-match-patch.d.ts @@ -0,0 +1,46 @@ +// Type definitions for diff-match-patch v1.0.0 +// Project: https://www.npmjs.com/package/diff-match-patch +// Definitions by: Austen Talbot +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "diff-match-patch" { + interface Diff { + 0: number; + 1: string; + } + + export class DiffMatchPatch { + Diff_Timeout: number; + Diff_EditCost: number; + Match_Threshold: number; + Match_Distance: number; + Patch_DeleteThreshold: number; + Patch_Margin: number; + Match_MaxBits: number; + + diff_main(text1: string, text2: string, opt_checklines?: boolean, opt_deadline?: number): Diff[]; + diff_commonPrefix(text1: string, text2: string): number; + diff_commonSuffix(text1: string, text2: string): number; + diff_cleanupSemantic(diffs: Diff[]): void; + diff_cleanupSemanticLossless(diffs: Diff[]): void; + diff_cleanupEfficiency(diffs: Diff[]): void; + diff_cleanupMerge(diffs: Diff[]): void; + diff_xIndex(diffs: Diff[], loc: number): number; + diff_prettyHtml(diffs: Diff[]): string; + diff_text1(diffs: Diff[]): string; + diff_text2(diffs: Diff[]): string; + diff_levenshtein(diffs: Diff[]): number; + diff_toDelta(diffs: Diff[]): string; + diff_fromDelta(text1: string, delta: string): Diff[]; + + new (): DiffMatchPatch; + } + + export var DIFF_DELETE: number; + export var DIFF_INSERT: number; + export var DIFF_EQUAL: number; + + export var diff_match_patch: { + new (): DiffMatchPatch; + }; +} From 99ee1fcd9635335b2e8d870fe52fd401851a65e7 Mon Sep 17 00:00:00 2001 From: Austen Talbot Date: Mon, 10 Aug 2015 10:00:31 -0700 Subject: [PATCH 2/3] Converted Diff interfect object to type array --- diff-match-patch/diff-match-patch.d.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/diff-match-patch/diff-match-patch.d.ts b/diff-match-patch/diff-match-patch.d.ts index 3a55b7769..b92f24411 100644 --- a/diff-match-patch/diff-match-patch.d.ts +++ b/diff-match-patch/diff-match-patch.d.ts @@ -4,10 +4,7 @@ // Definitions: https://github.com/borisyankov/DefinitelyTyped declare module "diff-match-patch" { - interface Diff { - 0: number; - 1: string; - } + type Diff = [number, string]; export class DiffMatchPatch { Diff_Timeout: number; From b177c999720bd6cc97480b6ad61e072bdeb821f2 Mon Sep 17 00:00:00 2001 From: Austen Talbot Date: Mon, 10 Aug 2015 15:10:25 -0700 Subject: [PATCH 3/3] Updated formatting --- diff-match-patch/diff-match-patch.d.ts | 52 ++++++++++++-------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/diff-match-patch/diff-match-patch.d.ts b/diff-match-patch/diff-match-patch.d.ts index b92f24411..63bf7eaa0 100644 --- a/diff-match-patch/diff-match-patch.d.ts +++ b/diff-match-patch/diff-match-patch.d.ts @@ -1,43 +1,39 @@ // Type definitions for diff-match-patch v1.0.0 // Project: https://www.npmjs.com/package/diff-match-patch -// Definitions by: Austen Talbot +// Definitions by: Asana // Definitions: https://github.com/borisyankov/DefinitelyTyped declare module "diff-match-patch" { type Diff = [number, string]; - export class DiffMatchPatch { - Diff_Timeout: number; - Diff_EditCost: number; - Match_Threshold: number; - Match_Distance: number; - Patch_DeleteThreshold: number; - Patch_Margin: number; - Match_MaxBits: number; + export class diff_match_patch { + static new (): diff_match_patch; - diff_main(text1: string, text2: string, opt_checklines?: boolean, opt_deadline?: number): Diff[]; - diff_commonPrefix(text1: string, text2: string): number; - diff_commonSuffix(text1: string, text2: string): number; - diff_cleanupSemantic(diffs: Diff[]): void; - diff_cleanupSemanticLossless(diffs: Diff[]): void; - diff_cleanupEfficiency(diffs: Diff[]): void; - diff_cleanupMerge(diffs: Diff[]): void; - diff_xIndex(diffs: Diff[], loc: number): number; - diff_prettyHtml(diffs: Diff[]): string; - diff_text1(diffs: Diff[]): string; - diff_text2(diffs: Diff[]): string; - diff_levenshtein(diffs: Diff[]): number; - diff_toDelta(diffs: Diff[]): string; - diff_fromDelta(text1: string, delta: string): Diff[]; + Diff_Timeout: number; + Diff_EditCost: number; + Match_Threshold: number; + Match_Distance: number; + Patch_DeleteThreshold: number; + Patch_Margin: number; + Match_MaxBits: number; - new (): DiffMatchPatch; + diff_main(text1: string, text2: string, opt_checklines?: boolean, opt_deadline?: number): Diff[]; + diff_commonPrefix(text1: string, text2: string): number; + diff_commonSuffix(text1: string, text2: string): number; + diff_cleanupSemantic(diffs: Diff[]): void; + diff_cleanupSemanticLossless(diffs: Diff[]): void; + diff_cleanupEfficiency(diffs: Diff[]): void; + diff_cleanupMerge(diffs: Diff[]): void; + diff_xIndex(diffs: Diff[], loc: number): number; + diff_prettyHtml(diffs: Diff[]): string; + diff_text1(diffs: Diff[]): string; + diff_text2(diffs: Diff[]): string; + diff_levenshtein(diffs: Diff[]): number; + diff_toDelta(diffs: Diff[]): string; + diff_fromDelta(text1: string, delta: string): Diff[]; } export var DIFF_DELETE: number; export var DIFF_INSERT: number; export var DIFF_EQUAL: number; - - export var diff_match_patch: { - new (): DiffMatchPatch; - }; }