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)
Package root>README.md
# Diff-Match-Patch Unicode [![JSR](https://jsr.io/badges/@clearlylocal/diff-match-patch-unicode)](https://jsr.io/@clearlylocal/diff-match-patch-unicode) Modern JS/TS and Unicode-friendly version of [Neil Fraser](https://github.com/NeilFraser)’s [`diff-match-patch`](https://github.com/google/diff-match-patch). Currently only supports diffing; matching and patching may be added in the future, depending on need. ## Usage ### `diff` ```ts 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 ```ts 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.