nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.
49 lines (48 loc) • 2.34 kB
TypeScript
import type { CharDiffResult, DiffResult } from './types';
/**
* * Computes a line-based text diff between two strings using the Longest Common Subsequence (LCS) algorithm.
*
* @remarks
* - Lines are classified as `added`, `removed`, `modified`, or `unchanged`.
* - Detects and pairs similar `removed` and `added` lines as `modified` when their similarity exceeds the threshold.
*
* @param originalText The original (before) text.
* @param modifiedText The modified (after) text.
* @returns A {@link DiffResult} with the list of diff lines and summary statistics.
*
* @example
* const result = computeTextDiff('hello\nworld', 'hello\nearth');
* // result.stats → { linesAdded: 1, linesRemoved: 1, linesChanged: 0, linesUnchanged: 1 }
*
* @example
* const result = computeTextDiff('foo\nbar', 'foo\nbaz\nqux');
* result.stats.linesAdded; // 1
* result.stats.linesChanged; // 1
*/
export declare function computeTextDiff(originalText: string, modifiedText: string): DiffResult;
/**
* * Highlights character-level differences between two strings using the LCS algorithm.
*
* @remarks
* - The function returns two arrays of characters for the original and modified strings.
* - Each character in both strings is annotated with a `highlighted` flag indicating whether it differs from the other string.
*
* @param original The original string to compare from.
* @param modified The modified string to compare to.
* @returns A {@link CharDiffResult} with annotated character arrays for both strings.
*
* @example
* const diff = getCharacterDifferences('cat', 'car');
* diff.original; // [{ text: 'c', highlighted: false }, { text: 'a', highlighted: false }, { text: 't', highlighted: true }]
* diff.modified; // [{ text: 'c', highlighted: false }, { text: 'a', highlighted: false }, { text: 'r', highlighted: true }]
*
* @example
* // When one string is empty, all characters in the other are highlighted
* getCharacterDifferences('', 'hi');
* // { original: [], modified: [{ text: 'h', highlighted: true }, { text: 'i', highlighted: true }] }
*
* @example
* const diff = getCharacterDifferences('hello world', 'hello earth');
* // Characters unique to each string will have highlighted: true
*/
export declare function getCharacterDifferences(original: string, modified: string): CharDiffResult;