getTextDiff

Compares two texts and returns a structured diff at a character, word, or sentence level.

IMPORT

import { getTextDiff } from "@donedeal0/superdiff";

FORMAT

Input

previousText: string | null | undefined,
currentText: string | null | undefined,
options?: {
    separation?: "character" | "word" | "sentence", // "word" by default
    accuracy?: "normal" | "high", // "normal" by default
    detectMoves?: boolean // false by default
    ignoreCase?: boolean, // false by default
    ignorePunctuation?: boolean, // false by default
    locale?: Intl.Locale | string // undefined by default
  }
  • previousText: the original text.

  • currentText: the current text.

  • options

    • separation whether you want a character, word or sentence based diff.

    • accuracy:

      • normal (default): fastest mode, simple tokenization.

      • high: slower but exact tokenization. Handles all language subtleties (Unicode, emoji, CJK scripts, locale‑aware segmentation when a locale is provided).

    • detectMoves:

      • false (default): optimized for readability. Token moves are ignored so insertions don’t cascade and break equality (recommended for UI diffing).

      • true: semantically precise, but noiser — a single insertion shifts all following tokens, breaking equality.

    • ignoreCase: if true, hello and HELLO are considered equal.

    • ignorePunctuation: if true, hello! and hello are considered equal.

    • locale: the locale of your text. Enables locale‑aware segmentation in high accuracy mode.

Output


USAGE

WITHOUT MOVES DETECTION

This is the default output. Token moves are ignored so insertions don’t cascade and break equality. Updates are rendered as two entries (added + deleted). The algorithm uses longest common subsequence (LCS)arrow-up-right, similar to GitHub diffs.

Input

Output

WITH MOVE DETECTION

If you prefer a semantically precise diff, activate the detectMoves option. Direct token swaps are considered updated.

Input

Output

Last updated