From fea39480baf83749978fc94df136d394e8dbecf0 Mon Sep 17 00:00:00 2001 From: Luiz Eduardo Figueiredo Date: Thu, 13 Jun 2024 13:55:19 -0300 Subject: [PATCH 1/3] chore: Add TypeScript declaration file for diff module --- diff.d.ts | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 diff.d.ts diff --git a/diff.d.ts b/diff.d.ts new file mode 100644 index 0000000..74d8fdd --- /dev/null +++ b/diff.d.ts @@ -0,0 +1,90 @@ +// Declare the `diff` namespace. +declare namespace diff { + + // Define the Diff type which represents a tuple [number, string]. + type Diff = [number, string]; + + // Define a class `DiffMatchPatch` which includes the methods for the diff operations. + class DiffMatchPatch { + // Constructor accepts an optional options object. + constructor(options?: DiffMatchPatchOptions); + + // Timeout in seconds for the diff operation before giving up. + Timeout: number; + + // Cost of an empty edit operation. + EditCost: number; + + /** + * Main function to find the differences between two texts. + * @param text1 - The old string to be diffed. + * @param text2 - The new string to be diffed. + * @param opt_checklines - Optional flag to indicate line-level diffing. + * @param opt_deadline - Optional time when the diff should be complete by. + * @returns An array of Diff tuples. + */ + main(text1: string, text2: string, opt_checklines?: boolean, opt_deadline?: number): Diff[]; + + /** + * Computes the differences between two texts assuming no common prefix or suffix. + * @param text1 - The old string to be diffed. + * @param text2 - The new string to be diffed. + * @param checklines - Whether to perform line-level diffing. + * @param deadline - The time by which the diff operation must be complete. + * @returns An array of Diff tuples. + */ + private compute_(text1: string, text2: string, checklines: boolean, deadline: number): Diff[]; + + /** + * Quick line-level diffing on both strings, then re-diff for accuracy. + * @param text1 - The old string to be diffed. + * @param text2 - The new string to be diffed. + * @param deadline - The time by which the diff operation must be complete. + * @returns An array of Diff tuples. + */ + private lineMode_(text1: string, text2: string, deadline: number): Diff[]; + + /** + * Finds the 'middle snake' of a diff, splits the problem in two, and recurses. + * @param text1 - The old string to be diffed. + * @param text2 - The new string to be diffed. + * @param deadline - The time by which the diff operation must be complete. + * @returns An array of Diff tuples. + */ + private bisect_(text1: string, text2: string, deadline: number): Diff[]; + + /** + * Given the location of the 'middle snake', splits the diff in two and recurses. + * @param text1 - The old string to be diffed. + * @param text2 - The new string to be diffed. + * @param x - Index of split point in text1. + * @param y - Index of split point in text2. + * @param deadline - The time by which the diff operation must be complete. + * @returns An array of Diff tuples. + */ + private bisectSplit_(text1: string, text2: string, x: number, y: number, deadline: number): Diff[]; + + /** + * Splits two texts into an array of strings and reduces them to a string of hashes. + * @param text1 - The first string. + * @param text2 - The second string. + * @returns An object containing the encoded text1, encoded text2, and the array of unique strings. + */ + private linesToChars_(text1: string, text2: string): { + chars1: string, + chars2: string, + lineArray: string[] + }; + } + + // Interface for options that can be passed to the DiffMatchPatch constructor. + interface DiffMatchPatchOptions { + timeout?: number; + editCost?: number; + } + + // Constants representing diff operations. + const DIFF_DELETE: number; + const DIFF_INSERT: number; + const DIFF_EQUAL: number; +} From fcc96da58bcb85e8b9f49a56cc049134cd9d5e16 Mon Sep 17 00:00:00 2001 From: Luiz Eduardo Figueiredo Date: Thu, 13 Jun 2024 14:01:30 -0300 Subject: [PATCH 2/3] chore: Add TypeScript declaration file for diff module --- .npmrc | 1 + package.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .npmrc diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..478242b --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +@luizeduard0:registry=https://npm.pkg.github.com \ No newline at end of file diff --git a/package.json b/package.json index e9d82fe..d3bdda5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "text-diff", "version": "1.0.1", - "description": "Compute a diff between two pieces of text with support for same-line diffs, and optionally display a visual diff as HTML. Module made from the text-diff-match-patch library.", + "description": "(With types) Compute a diff between two pieces of text with support for same-line diffs, and optionally display a visual diff as HTML. Module made from the text-diff-match-patch library.", "main": "diff.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" From 4f75ec71ec2ab97e4652310522e31fc976d8b7b7 Mon Sep 17 00:00:00 2001 From: Luiz Eduardo Figueiredo Date: Thu, 13 Jun 2024 14:10:12 -0300 Subject: [PATCH 3/3] chore: Update package name and version in package.json --- diff.d.ts | 2 ++ package.json | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/diff.d.ts b/diff.d.ts index 74d8fdd..57db005 100644 --- a/diff.d.ts +++ b/diff.d.ts @@ -88,3 +88,5 @@ declare namespace diff { const DIFF_INSERT: number; const DIFF_EQUAL: number; } + +declare module 'Diff' diff --git a/package.json b/package.json index d3bdda5..40389f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "text-diff", - "version": "1.0.1", + "name": "text-diff-ts", + "version": "1.0.3", "description": "(With types) Compute a diff between two pieces of text with support for same-line diffs, and optionally display a visual diff as HTML. Module made from the text-diff-match-patch library.", "main": "diff.js", "scripts": {