Skip to content

nonoroazoro/diff-match-patch-typescript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

141 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

diff-match-patch-typescript

TypeScript port of google/diff-match-patch.

GitHub CI GitHub License NPM Downloads

Installation

npm i diff-match-patch-typescript

Usage

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

DiffMatchPatch Class Configuration

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.

Diff functions

Match functions

  • match_main - Locates the best instance of a pattern in text near a given location.

Patch functions

Related

Contributors