Skip to main content
Home

Built and signed on GitHub Actions

Modern JS/TS and Unicode-friendly version of diff-match-patch.

This package works with Node.js, Deno, Bun, BrowsersIt is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
a year ago (0.1.1)

Diff-Match-Patch Unicode JSR

Modern JS/TS and Unicode-friendly version of Neil Fraser’s diff-match-patch. Currently only supports diffing; matching and patching may be added in the future, depending on need.

Usage

diff

diff(before: string, after: string, options?: Partial<DiffOptions>): Diff[]

Diff two strings. Fully Unicode-aware by default.

Pass a segmenter option to customize the units of calculation for the diff (char, line, word, grapheme, sentence, etc).

Example

import { Differ, segmenters } from '@clearlylocal/diff-match-patch-unicode'

const differ = new Differ()

const str1 = 'Hello, world! πŸ’«'
const str2 = 'Goodbye, world! πŸ’©'

// default behavior: UTF-8 char diff
assertDiffsEqual(
    differ.diff(str1, str2),
    [[-1, 'Hell'], [1, 'G'], [0, 'o'], [1, 'odbye'], [0, ', world! '], [-1, 'πŸ’«'], [1, 'πŸ’©']],
)

// word diff with `Intl.Segmenter`
assertDiffsEqual(
    differ.diff(str1, str2, { segmenter: segmenters.word }),
    [[-1, 'Hello'], [1, 'Goodbye'], [0, ', world! '], [-1, 'πŸ’«'], [1, 'πŸ’©']],
)

// pass in a custom `Intl.Segmenter` instance
assertDiffsEqual(
    differ.diff('δΈ€εͺε°θœœθœ‚', 'δΈ€εͺθ€θ™Ž', { segmenter: new Intl.Segmenter('zh-CN', { granularity: 'word' }) }),
    [[0, 'δΈ€εͺ'], [-1, 'ε°θœœθœ‚'], [1, 'θ€θ™Ž']],
)

// line diff
assertDiffsEqual(
    differ.diff(str1, str2, { segmenter: segmenters.line }),
    [[-1, 'Hello, world! πŸ’«'], [1, 'Goodbye, world! πŸ’©']],
)

// custom UTF-16 code-unit diff (equivalent to using `diffCodeUnits` directly... but less performant)
assertDiffsEqual(
    differ.diff(str1, str2, { segmenter: (str) => str.split('') }),
    [[-1, 'Hell'], [1, 'G'], [0, 'o'], [1, 'odbye'], [0, ', world! \ud83d'], [-1, '\udcab'], [1, '\udca9']],
)

Limitations

The maximum number of unique segments (chars, lines, words, graphemes, sentences, code units, etc) is capped at 65535 (0xFFFF), the maximum codepoint in the BMP. In addition, the maximum number of unique segments in the first string is capped at two thirds of that total (43690 or 0xAAAA). This is due to the original algorithm working with JS native UTF-16 strings and using non-UTF-8-aware methods (String.fromCharCode, charAt, substring, indexOf etc.) extensively.

If working with diffs larger than this limit, the last segment of each string will contain all of its remaining text until the end of the input.

Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@clearlylocal/diff-match-patch-unicode

Import symbol

import * as diff_match_patch_unicode from "@clearlylocal/diff-match-patch-unicode";
or

Import directly with a jsr specifier

import * as diff_match_patch_unicode from "jsr:@clearlylocal/diff-match-patch-unicode";

Add Package

pnpm i jsr:@clearlylocal/diff-match-patch-unicode
or (using pnpm 10.8 or older)
pnpm dlx jsr add @clearlylocal/diff-match-patch-unicode

Import symbol

import * as diff_match_patch_unicode from "@clearlylocal/diff-match-patch-unicode";

Add Package

yarn add jsr:@clearlylocal/diff-match-patch-unicode
or (using Yarn 4.8 or older)
yarn dlx jsr add @clearlylocal/diff-match-patch-unicode

Import symbol

import * as diff_match_patch_unicode from "@clearlylocal/diff-match-patch-unicode";

Add Package

vlt install jsr:@clearlylocal/diff-match-patch-unicode

Import symbol

import * as diff_match_patch_unicode from "@clearlylocal/diff-match-patch-unicode";

Add Package

npx jsr add @clearlylocal/diff-match-patch-unicode

Import symbol

import * as diff_match_patch_unicode from "@clearlylocal/diff-match-patch-unicode";

Add Package

bunx jsr add @clearlylocal/diff-match-patch-unicode

Import symbol

import * as diff_match_patch_unicode from "@clearlylocal/diff-match-patch-unicode";