or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

array-diffing.mdcharacter-diffing.mdcss-diffing.mdcustom-diffing.mdformat-conversion.mdindex.mdjson-diffing.mdline-diffing.mdpatch-application.mdpatch-creation.mdpatch-utilities.mdsentence-diffing.mdword-diffing.md
tile.json

tessl/npm-diff

A JavaScript text diff implementation based on the Myers algorithm for comparing text at different granularities.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/diff@7.0.x

To install, run

npx @tessl/cli install tessl/npm-diff@7.0.0

index.mddocs/

diff

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.

Package Information

  • Package Name: diff
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install diff

Core Imports

import { 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 -->

Basic Usage

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);

Architecture

The diff library is built around several key components:

  • Base Diff Algorithm: Core Myers algorithm implementation with customizable tokenization
  • Text Diff Functions: Specialized functions for different text granularities (chars, words, lines, sentences)
  • Content-Specific Diffs: Tailored implementations for CSS and JSON content
  • Patch System: Complete unified diff patch creation, parsing, and application
  • Conversion Utilities: Export capabilities for XML and Google diff-match-patch formats

Capabilities

Character Diffing

Character-by-character text comparison with Unicode support and case-insensitive options.

function diffChars(oldStr, newStr, options);

Character Diffing

Word Diffing

Word-level text comparison with intelligent whitespace handling and international text support.

function diffWords(oldStr, newStr, options);
function diffWordsWithSpace(oldStr, newStr, options);

Word Diffing

Line Diffing

Line-by-line text comparison with configurable whitespace and newline handling.

function diffLines(oldStr, newStr, options);

Line Diffing

Sentence Diffing

Sentence-level text comparison for natural language processing applications.

function diffSentences(oldStr, newStr, options);

Sentence Diffing

Array Diffing

Generic array comparison with customizable equality functions.

function diffArrays(oldArr, newArr, options);

Array Diffing

CSS Diffing

Specialized diffing for CSS content with CSS-aware tokenization.

function diffCss(oldStr, newStr, options);

CSS Diffing

JSON Diffing

JSON object comparison with pretty-printing and custom serialization options.

function diffJson(oldObj, newObj, options);

JSON Diffing

Patch Creation

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);

Patch Creation

Patch Application

Apply unified diff patches to source text with fuzzy matching and error handling.

function applyPatch(source, patch, options);
function applyPatches(patches, options);

Patch Application

Patch Utilities

Parse, manipulate, and convert diff patches between different formats.

function parsePatch(diffStr);
function reversePatch(patch);
function merge(mine, theirs, base);

Patch Utilities

Format Conversion

Convert diff results to XML and Google diff-match-patch formats.

function convertChangesToXML(changes);
function convertChangesToDMP(changes);

Format Conversion

Custom Diffing

Create custom diff implementations with configurable tokenization and comparison logic.

function Diff();

Custom Diffing

Universal Options

All diff functions support these common options:

  • callback: Function for async computation
  • maxEditLength: Number limiting maximum edit distance
  • timeout: Number specifying timeout in milliseconds
  • oneChangePerToken: Boolean for granular change objects

Change Objects

All 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
}

Types

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;
}