kpdecker /jsdiff

A javascript text differencing implementation.

★ 9,223⑂ 557👁 86⑃ 415● 331▤ 0
JavaScriptBSD-3-ClauseCreated 2011-03-29
+1
日增长
9,223
stars
557
forks
86
watchers
415
prs
331
issues
0
releases

README

jsdiff

A JavaScript text differencing implementation. Try it out in the online demo.

Based on the algorithm proposed in
"An O(ND) Difference Algorithm and its Variations" (Myers, 1986).

 Installation
bash
npm install diff --save


 Getting started

 Imports

In an environment where you can use imports, everything you need can be imported directly from diff. e.g.

ESM:


import {diffChars, createPatch} from 'diff';


CommonJS


const {diffChars, createPatch} = require('diff');


If you want to serve jsdiff to a web page without using a module system, you can use dist/diff.js or dist/diff.min.js. These create a global called Diff that contains the entire JsDiff API as its properties.

 Usage

jsdiff's diff functions all take an old text and a new text and perform three steps:

1. Split both texts into arrays of "tokens". What constitutes a token varies; in diffChars, each character is a token, while in diffLines, each line is a token.

2. Find the smallest set of single-token insertions and deletions needed to transform the first array of tokens into the second.

   This step depends upon having some notion of a token from the old array being "equal" to one from the new array, and this notion of equality affects the results. Usually two tokens are equal if === considers them equal, but some of the diff functions use an alternative notion of equality or have options to configure it. For instance, by default diffChars("Foo", "FOOD") will require two deletions (o, o) and three insertions (O, O, D), but diffChars("Foo", "FOOD", {ignoreCase: true}) will require just one insertion (of a D), since ignoreCase causes o and O to be considered equal.

3. Return an array representing the transformation computed in the previous step as a series of change objects. The array is ordered from the start of the input to the end, and each change object represents inserting one or more tokens, deleting one or more tokens, or keeping one or more tokens.

 API

 diffChars(oldStr, newStr[, options]) - diffs two blocks of text, treating each character as a token.

    ("Characters" here means Unicode code points - the elements you get when you loop over a string with a for ... of ... loop.)

    Returns a list of change objects.

    Options
     ignoreCase: If true, the uppercase and lowercase forms of a character are considered equal. Defaults to false.

 diffWords(oldStr, newStr[, options]) - diffs two blocks of text, treating each word and each punctuation mark as a token. Whitespace is ignored when computing the diff (but preserved as far as possible in the final change objects).

    Returns a list of change objects.

    Options
     ignoreCase: Same as in diffChars. Defaults to false.
     intlSegmenter: An optional Intl.Segmenter object (which must have a granularity of 'word') for diffWords to use to split the text into words.

      By default, diffWords does not use an Intl.Segmenter, just some regexes for splitting text into words. This will tend to give wo

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON