diff-match-patch-typescript
TypeScript port of google/diff-match-patch.
Installation
``
bash
npm i diff-match-patch-typescript
`
Usage
`
typescript
import { DiffMatchPatch, DiffOperation } from "diff-match-patch-typescript";
const dmp = new DiffMatchPatch();
// Diff
const diffs = dmp.diff_main("Hello World", "Hello TypeScript");
// [
// [DiffOperation.DIFF_EQUAL, "Hello "],
// [DiffOperation.DIFF_DELETE, "World"],
// [DiffOperation.DIFF_INSERT, "TypeScript"]
// ]
// Match
const position = dmp.match_main("Hello World", "World", 0); // 6
// Patch
const patches = dmp.patch_make("Hello World", "Hello TypeScript");
const [newText, results] = dmp.patch_apply(patches, "Hello World");
// newText: "Hello TypeScript"
// results: [true]
`
API Reference
$3
| Property | Type | Default | Description |
| -------- | ---- | ------- | ----------- |
| diffTimeout
| number
| 1.0
| Seconds to compute a diff before giving up (0 = infinity). |
| diffEditCost
| number
| 4
| Cost of an empty edit operation. |
| matchThreshold
| number
| 0.5
| Threshold for match (0.0 = perfect, 1.0 = loose). |
| matchDistance
| number
| 1000
| How far to search for a match. |
| patchDeleteThreshold
| number
| 0.5
| Threshold for patch deletion matching. |
| patchMargin
| number
| 4` | Chunk size for context length. |
$3
-
diff_main - Computes the differences between two texts.
-
diff_commonPrefix - Determines the common prefix length of two strings.
-
diff_commonSuffix - Determines the common suffix length of two strings.
-
diff_cleanupSemantic - Reduces edits by eliminating semantically trivial equalities.
-
diff_cleanupSemanticLossless - Optimizes diff boundaries on logical boundaries.
-
diff_cleanupEfficiency - Reduces edits by eliminating operationally trivial equalities.
-
diff_cleanupMerge - Reorders and merges like edit sections.
-
diff_xIndex - Locates the position of a chunk in the second sequence.
-
diff_prettyHtml - Converts a diff array into a pretty HTML report.
-
diff_text1 - Computes the source text from a diff array.
-
diff_text2 - Computes the destination text from a diff array.
-
diff_levenshtein - Computes the Levenshtein distance.
-
diff_toDelta - Converts a diff array into a delta string.
-
diff_fromDelta - Computes a diff array from a text and delta string.
-
diff_linesToChars - Splits two texts into an array of strings for line-mode diffing.
-
diff_charsToLines - Converts character-indexed diffs back to line-indexed diffs.
$3
-
match_main - Locates the best instance of a pattern in text near a given location.
$3
-
patch_make - Creates an array of patch objects from texts or diffs.
-
patch_apply - Applies patches to text, returns new text and success array.
-
patch_deepCopy - Creates a deep copy of a patch array.
-
patch_addPadding - Adds padding to patch start and end.
-
patch_splitMax - Splits large patches into smaller ones.
-
patch_toText - Converts patches to a textual representation.
-
patch_fromText - Parses a textual representation into patch objects.
Related
-
google/diff-match-patch - The original library by Google.