diff.ts

Line and intra-line character diffs via greedy Myers over interned sequences, plus hunk grouping and unified-diff text formatting.

view source

Declarations
#

12 declarations

diff_hunks
#

diff.ts view source

(lines: DiffLine[], context_lines?: number): DiffHunk[] import {diff_hunks} from '@fuzdev/fuz_util/diff.js';

Groups a line diff into hunks — changed lines plus context_lines of surrounding context, with overlapping or adjacent windows merged. Returns [] when nothing changed.

lines

type DiffLine[]

context_lines

number of context lines around changes (default: 3)

type number
default 3

returns

DiffHunk[]

diff_lines
#

diff.ts view source

(a: string, b: string, options?: DiffOptions): DiffLine[] import {diff_lines} from '@fuzdev/fuz_util/diff.js';

Generates a line diff between two strings — greedy Myers over interned lines, with each changed region normalized to removes-before-adds. Splits on \n only (\r stays in line text).

a

the original content

type string

b

the updated content

type string

options

default {}

returns

DiffLine[]

diff_segments
#

diff.ts view source

(a: string, b: string, options?: DiffSegmentsOptions): DiffSegments | null import {diff_segments} from '@fuzdev/fuz_util/diff.js';

Computes intra-line changed-character ranges for one paired remove/add line — char-level greedy Myers. Returns null when the pair reads as a rewrite (below min_similarity) or a line exceeds max_length, so callers skip emphasis rather than highlight noise.

a

the removed line's text

type string

b

the added line's text

type string

options

default {}

returns

DiffSegments | null

DiffHunk
#

diff.ts view source

DiffHunk import type {DiffHunk} from '@fuzdev/fuz_util/diff.js';

A group of changed lines with surrounding context. Starts are 1-based; a side with zero lines (pure insertion/deletion at context_lines: 0) starts at the line *before* the hunk on that side, 0 at the very beginning — unified-diff @@ conventions.

a_start

type number

a_count

type number

b_start

type number

b_count

type number

lines

The hunk's lines, shared (not copied) from the input diff.

type Array<DiffLine>

DiffLine
#

diff.ts view source

DiffLine import type {DiffLine} from '@fuzdev/fuz_util/diff.js';

One line of a line diff. Line numbers are 1-based; the absent side of an add/remove is null.

type

type 'same' | 'add' | 'remove'

text

The line's content, without its newline terminator.

type string

a_line

type number | null

b_line

type number | null

no_newline?

Set on the final line of a side that lacks a trailing newline (git's \ No newline at end of file semantics). A terminated and an unterminated line never match, so a newline-only change shows as remove+add of the final line.

type boolean

DiffOptions
#

diff.ts view source

DiffOptions import type {DiffOptions} from '@fuzdev/fuz_util/diff.js';

Options for diff_lines.

max_cost?

Maximum edit cost (Myers D) before the remaining changed region degrades to one whole replace block — bounds worst-case time and memory on unrelated inputs. The result is always a valid diff.

type number

default 2048

DiffSegments
#

diff.ts view source

DiffSegments import type {DiffSegments} from '@fuzdev/fuz_util/diff.js';

Changed-character ranges from an intra-line diff of one paired remove/add line. Ranges are [start, end) offsets into the respective line's text, in order and non-adjacent.

a_ranges

type Array<[number, number]>

b_ranges

type Array<[number, number]>

DiffSegmentsOptions
#

diff.ts view source

DiffSegmentsOptions import type {DiffSegmentsOptions} from '@fuzdev/fuz_util/diff.js';

Options for diff_segments.

max_cost?

See DiffOptions.max_cost.

type number

default 2048

max_length?

Lines longer than this return null (no emphasis) — perf guard.

type number

default 1000

min_similarity?

Minimum Dice similarity (2 * matched_chars / total_chars) for a result — below it the pair is considered a rewrite and null is returned so unrelated lines don't get noise emphasis.

type number

default 0.3

join_gap?

Changed ranges separated by at most this many matched characters merge (per side, with the matched characters absorbed into the range) — avoids fragmented emphasis when a rewrite happens to match stray characters. 0 keeps exact ranges.

type number

default 2

format_diff
#

diff.ts view source

(hunks: DiffHunk[], a_path: string, b_path: string, options?: FormatDiffOptions): string import {format_diff} from '@fuzdev/fuz_util/diff.js';

Formats hunks as unified-diff text with @@ headers. Colors go through print.ts's st seam — plain text until configure_print_colors opts in.

hunks

type DiffHunk[]

a_path

path label for the original content

type string

b_path

path label for the updated content

type string

options

default {}

returns

string

FormatDiffOptions
#

diff.ts view source

FormatDiffOptions import type {FormatDiffOptions} from '@fuzdev/fuz_util/diff.js';

Options for format_diff.

prefix?

Prefix prepended to every output line, e.g. for indentation.

type string

max_lines?

Maximum number of content lines before truncating with a ... (N more lines) marker, 0 for unlimited.

type number

default 50

generate_diff
#

diff.ts view source

(a: string, b: string, path: string, options?: GenerateDiffOptions): string | null import {generate_diff} from '@fuzdev/fuz_util/diff.js';

Generates formatted unified-diff text between two strings — diff_linesdiff_hunksformat_diff. Returns null if either side is binary.

a

type string

b

type string

path

file path used for both labels

type string

options

default {}

returns

string | null

GenerateDiffOptions
#

Depends on
#