> For the complete documentation index, see [llms.txt](https://donedeal0.gitbook.io/superdiff/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://donedeal0.gitbook.io/superdiff/features/gettextdiff.md).

# getTextDiff

## IMPORT

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

***

## FORMAT

### Input

```typescript
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

```typescript
type TextDiff = {
  type: "text";
  status: "added" | "deleted" | "equal" | "updated";
  diff: {
    value: string;
    index: number | null;
    previousValue?: string;
    previousIndex: number | null;
    status: "added" | "deleted" | "equal" | "moved" | "updated";
  }[];
};
```

***

## 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)](https://en.wikipedia.org/wiki/Longest_common_subsequence), similar to GitHub diffs.

### Input

```diff
getTextDiff(
- "The brown fox jumped high",
+ "The orange cat has jumped",
{ detectMoves: false, separation: "word" }
);
```

### Output

```diff
{
      type: "text",
+     status: "updated",
      diff: [
        {
          value: 'The',
          index: 0,
          previousIndex: 0,
          status: 'equal',
        },
-       {
-         value: "brown",
-         index: null,
-         previousIndex: 1,
-         status: "deleted",
-       },
-       {
-         value: "fox",
-         index: null,
-         previousIndex: 2,
-         status: "deleted",
-       },
+       {
+         value: "orange",
+         index: 1,
+         previousIndex: null,
+         status: "added",
+       },
+       {
+         value: "cat",
+         index: 2,
+         previousIndex: null,
+         status: "added",
+       },
+       {
+         value: "has",
+         index: 3,
+         previousIndex: null,
+         status: "added",
+       },
        {
          value: "jumped",
          index: 4,
          previousIndex: 3,
          status: "equal",
        },
-       {
-         value: "high",
-         index: null,
-         previousIndex: 4,
-         status: "deleted",
-       }
      ],
}
```

**WITH MOVE DETECTION**

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

**Input**

```diff
getTextDiff(
- "The brown fox jumped high",
+ "The orange cat has jumped",
{ detectMoves: true, separation: "word" }
);
```

**Output**

```diff
{
      type: "text",
+     status: "updated",
      diff: [
        {
          value: 'The',
          index: 0,
          previousIndex: 0,
          status: 'equal',
        },
+       {
+         value: "orange",
+         index: 1,
+         previousValue: "brown",
+         previousIndex: null,
+         status: "updated",
+       },
+       {
+         value: "cat",
+         index: 2,
+         previousValue: "fox",
+         previousIndex: null,
+         status: "updated",
+       },
+       {
+         value: "has",
+         index: 3,
+         previousIndex: null,
+         status: "added",
+       },
+       {
+         value: "jumped",
+         index: 4,
+         previousIndex: 3,
+         status: "moved",
+       },
-       {
-         value: "high",
-         index: null,
-         previousIndex: 4,
-         status: "deleted",
-       }
      ],
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://donedeal0.gitbook.io/superdiff/features/gettextdiff.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
