Skip to content

About

UTF-16-native text diff, match and patch for JavaScript (Google DMP compatible, Unicode-hardened fork of @sanity/diff-match-patch)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📝 textdiffnext

UTF-16-native diff, match and patch for JavaScript

version license tests Node.js format

A hardened fork of @sanity/diff-match-patch, with patch coordinates that match JavaScript string indices.

Features · Measured performance · Install · Usage · Contracts


Why this fork exists

diff-match-patch and its TypeScript fork @sanity/diff-match-patch express patch coordinates as UTF-8 byte offsets. JavaScript strings are indexed in UTF-16 code units. The Sanity fork bridged the two with a byte-counting and index-translation layer that runs on every patch operation, and that layer is where the recorded failures come from: patch offset clamping on long multi-hunk files, cumulative drift on large JSON documents, and Failed to determine byte offset exceptions on astral text.

textdiffnext removes that layer. Patch objects, patch text, and the public API all speak UTF-16 code-unit coordinates — the same units as string.length and slice().

Problem What this package does
Patch coordinates drift from JavaScript string indices Coordinates are UTF-16 code units end to end; no byte-to-code-unit translation runs during make, apply, or serialize.
Lone surrogate halves crash encodeURI during patch serialization Isolated surrogate code units are emitted as %uXXXX and parsed back losslessly.
Applying a patch to shifted text silently clamps or corrupts Bitap fuzzy matching works on code-unit positions; the recorded 45 KB and 40 KB corruption cases reconstruct exactly.
Patch text must interoperate with existing tooling The wire format is Google DMP patch text (stringifyPatches / parsePatch), including @@ -start,length +start,length @@ headers.

What it adds

  • One coordinate model: Patch exposes start1, start2, length1, and length2 as zero-based UTF-16 code units, in patch objects and in patch text.
  • Unicode hardening: isolated surrogate halves round-trip as %uXXXX instead of throwing in encodeURI.
  • Fuzzy application that respects the model: the Bitap matcher works at code-unit positions, so a shifted target is found by search rather than by index arithmetic.
  • Google DMP wire format: patch text stays interoperable with existing diff-match-patch tooling.
  • Upstream algorithm, unchanged: the Myers bisect, semantic cleanup, and Bitap matcher are the upstream implementations. What changed is the coordinate model and the Unicode handling around it.
  • No runtime dependencies: ESM, CommonJS, and TypeScript declarations.

This is a fork, not a rewrite. It does not add a new diff algorithm, and it does not convert Sanity byte-coordinate patches for you — a byte-coordinate patch needs an explicit conversion based on the original source text. Patch text containing split surrogates is not Google-compatible, because Google DMP rejects the %uXXXX extension.

Architecture

textdiffnext pipeline: source text, UTF-16 coordinate model, diff, apply, and wire format

Measured performance

The repository benchmark log records one exploratory run on a single machine (macOS arm64, Bun 1.4.4) using the full patch pipeline (makePatches + applyPatches) on a 45 KB multi-hunk TypeScript file:

Implementation p50 p95
textdiffnext 7.12 ms 19.91 ms
@sanity/diff-match-patch 3.2.1 8.64 ms 23.92 ms

The same run recorded exact reconstruction on all 15 corpus scenarios, where the Sanity fork corrupted the 45 KB code and 40 KB JSON cases and threw on astral-heavy text. Correctness, not speed, is the reason to use this package; the timing difference is a secondary observation from one run with warm-up discarded.

Full patch pipeline timing

These are single-machine, single-run figures. They are not a throughput guarantee and have no confidence intervals. See BENCHMARK-RESULTS.md and REGRESSION-RESULTS.md.

Install

npm install textdiffnext

Requires Node.js 22 or later. The package ships ESM, CommonJS, and TypeScript declarations, and has no runtime dependencies.

Usage

import { makeDiff, makePatches, stringifyPatches, parsePatch, applyPatches } from 'textdiffnext';

const source = 'The quick brown fox';
const target = 'The quick red fox';

const diffs = makeDiff(source, target);
const patches = makePatches(source, target);

const patchText = stringifyPatches(patches);      // portable patch text
const [result, applied] = applyPatches(parsePatch(patchText), source);

// result === target, applied.every(Boolean) === true

CommonJS works the same way:

const { makeDiff, makePatches, applyPatches } = require('textdiffnext');

Fuzzy application is available when the target text has shifted:

import { makePatches, applyPatches } from 'textdiffnext';

const patches = makePatches('aaaa\nbbbb\ncccc', 'aaaa\nBBBB\ncccc');
const [result, applied] = applyPatches(patches, 'header line\naaaa\nbbbb\ncccc');

API

Export Description
makeDiff(textA, textB, options?) Compute diff tuples [type, text] using DIFF_DELETE, DIFF_INSERT, DIFF_EQUAL.
makePatches(textA, textB, options?) Build Patch[] from two texts, or from an existing Diff[].
applyPatches(patches, text, options?) Apply patches, returning [text, boolean[]]; the flags report which patches matched.
parsePatch(patchText) Parse Google DMP patch text into Patch[].
stringifyPatches(patches) Serialize Patch[] to Google DMP patch text.
match(text, pattern, searchLocation, options?) Bitap fuzzy match; returns the best match index or -1.
cleanupSemantic(diffs) / cleanupEfficiency(diffs) Upstream diff cleanup passes.
xIndex(text1, text2) Locate the shared middle segment used by the bisect step.
adjustIndiciesToUcs2(patches, base, options?) Deprecated beta helper kept for source compatibility; do not use it on textdiffnext patches (see below).

Patch exposes start1, start2, length1, and length2 as zero-based UTF-16 code-unit coordinates. The legacy utf8Start1 / utf8Start2 / utf8Length1 / utf8Length2 fields remain as optional deprecated mirrors holding the same UTF-16 values — they are not byte offsets. applyPatches accepts allowExceedingIndices for call-signature compatibility and ignores it.

Coordinate and format contracts

  • Coordinates: UTF-16 code units, zero-based, for both patch objects and patch text.
  • Patch text: Google DMP format, not Unified Diff. @@ -start,length +start,length @@ headers count code units, not lines; nonzero span starts are the zero-based offset plus one.
  • Surrogates: isolated surrogate halves serialize as %uXXXX. Google DMP rejects that extension, so patch text containing split surrogates is not Google-compatible even when the source text was valid Unicode.
  • Legacy byte clients: no implicit byte-to-code-unit conversion is performed. Byte-coordinate patches from Sanity need an explicit conversion based on the original source text.

Details and tested limits: API-COMPATIBILITY.md and UNICODE-SERIALIZATION-CONTRACT.md.

Development

bun install --frozen-lockfile
bun test                 # 44 tests
bun run build            # dist/index.js, dist/index.cjs, dist/types
bun run test:consumer    # tarball + isolated ESM/CJS/types consumers

The build requires Bun 1.4.4 or later; earlier Bun releases do not resolve .js import specifiers to .ts sources, which silently produces empty bundles.

Research artifacts

The feasibility study behind this fork — upstream snapshots, issue reproductions, Unicode semantics, competitor matrix, algorithm evaluation, and architecture options — is indexed in research/README.md. Those documents are engineering records, not package documentation.

Changelog and license

See CHANGELOG.md. Licensed under Apache-2.0. Derived from @sanity/diff-match-patch 3.2.1, itself a TypeScript fork of Google diff-match-patch. Upstream attribution is in THIRD-PARTY-NOTICES.md.

About

UTF-16-native text diff, match and patch for JavaScript (Google DMP compatible, Unicode-hardened fork of @sanity/diff-match-patch)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages