A JavaScript text differencing implementation based on the Myers diff algorithm. Provides comprehensive text comparison capabilities at different granularities (characters, words, lines, sentences) with support for CSS and JSON content, plus utilities for creating, applying, and manipulating unified diff patches.
npm install diffimport { Diff, diffChars, diffWords, diffLines, diffJson, applyPatch, createPatch } from "diff";For CommonJS:
const { Diff, diffChars, diffWords, diffLines, diffJson, applyPatch, createPatch } = require("diff");For browser:
<script src="diff.js"></script>
<!-- Functions available on global Diff object -->import { diffChars, diffWords, diffLines, createPatch, applyPatch } from "diff";
// Character-level diff
const charDiff = diffChars("abc", "axc");
console.log(charDiff);
// [
// { value: "a", count: 1 },
// { value: "b", removed: true, count: 1 },
// { value: "x", added: true, count: 1 },
// { value: "c", count: 1 }
// ]
// Word-level diff
const wordDiff = diffWords("hello world", "hello universe");
// Line-level diff
const lineDiff = diffLines("line1\nline2", "line1\nmodified line2");
// Create and apply patches
const patch = createPatch("file.txt", "old content", "new content");
const result = applyPatch("old content", patch);The diff library is built around several key components:
Character-by-character text comparison with Unicode support and case-insensitive options.
function diffChars(oldStr, newStr, options);Word-level text comparison with intelligent whitespace handling and international text support.
function diffWords(oldStr, newStr, options);
function diffWordsWithSpace(oldStr, newStr, options);Line-by-line text comparison with configurable whitespace and newline handling.
function diffLines(oldStr, newStr, options);Sentence-level text comparison for natural language processing applications.
function diffSentences(oldStr, newStr, options);Generic array comparison with customizable equality functions.
function diffArrays(oldArr, newArr, options);Specialized diffing for CSS content with CSS-aware tokenization.
function diffCss(oldStr, newStr, options);JSON object comparison with pretty-printing and custom serialization options.
function diffJson(oldObj, newObj, options);Create unified diff patches from text comparisons with full context control.
function createPatch(fileName, oldStr, newStr, oldHeader, newHeader, options);
function createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options);
function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options);Apply unified diff patches to source text with fuzzy matching and error handling.
function applyPatch(source, patch, options);
function applyPatches(patches, options);Parse, manipulate, and convert diff patches between different formats.
function parsePatch(diffStr);
function reversePatch(patch);
function merge(mine, theirs, base);Convert diff results to XML and Google diff-match-patch formats.
function convertChangesToXML(changes);
function convertChangesToDMP(changes);Create custom diff implementations with configurable tokenization and comparison logic.
function Diff();All diff functions support these common options:
callback: Function for async computationmaxEditLength: Number limiting maximum edit distancetimeout: Number specifying timeout in millisecondsoneChangePerToken: Boolean for granular change objectsAll diff functions return arrays of change objects with these properties:
interface ChangeObject {
value: string; // The text content
added?: boolean; // True if content was added
removed?: boolean; // True if content was removed
count: number; // Number of tokens
}interface DiffOptions {
callback?: (result: ChangeObject[]) => void;
maxEditLength?: number;
timeout?: number;
oneChangePerToken?: boolean;
ignoreCase?: boolean;
}
interface PatchOptions {
context?: number;
ignoreWhitespace?: boolean;
stripTrailingCr?: boolean;
newlineIsToken?: boolean;
}
interface ApplyPatchOptions {
fuzzFactor?: number;
autoConvertLineEndings?: boolean;
compareLine?: (lineNumber: number, line: string, operation: string, patchContent: string) => boolean;
}