diff
PoolModern, tree-shakeable diff library - 70% smaller than diff, multiple algorithms, TypeScript-native
dist
Functions
function computeLCS<T>(oldSeq: T[], newSeq: T[], equals?: EqualityFn<T>): number[][]
Longest Common Subsequence (LCS) using dynamic programming Time complexity: O(n * m) Space complexity: O(n * m)
| Parameter | Type | Description |
|---|---|---|
oldSeq | T[] | - Old sequence |
newSeq | T[] | - New sequence |
equalsoptional | EqualityFn<T> | - Equality comparison function |
function backtrackLCS<T>(dp: number[][], oldSeq: T[], newSeq: T[], equals?: EqualityFn<T>): Array<{
oldIndex: number;
newIndex: number;
}>Backtrack LCS to find actual common subsequence
| Parameter | Type | Description |
|---|---|---|
dp | number[][] | - DP table from computeLCS |
oldSeq | T[] | - Old sequence |
newSeq | T[] | - New sequence |
equalsoptional | EqualityFn<T> | - Equality comparison function |
function buildChangesFromMatches<T>(oldSeq: T[], newSeq: T[], matches: Array<{
oldIndex: number;
newIndex: number;
}>): DiffChange[]Convert edit operations to DiffChange array Merges consecutive operations of the same type into single changes
| Parameter | Type | Description |
|---|---|---|
oldSeq | T[] | - Old sequence |
newSeq | T[] | - New sequence |
matches | Array<{
oldIndex: number;
newIndex: number;
}> | - Matching indices from LCS |
function mergeConsecutiveChanges(changes: DiffChange[]): DiffChange[]
Merge consecutive changes of the same type Combines adjacent additions or deletions into single changes
| Parameter | Type | Description |
|---|---|---|
changes | DiffChange[] | - Array of diff changes |
function editDistance(oldStr: string, newStr: string): number
Calculate edit distance (Levenshtein distance) Number of single-character edits needed to transform one string into another
| Parameter | Type | Description |
|---|---|---|
oldStr | string | - Old string |
newStr | string | - New string |
function similarity(str1: string, str2: string): number
Calculate similarity score between two strings Returns value between 0 (completely different) and 1 (identical)
| Parameter | Type | Description |
|---|---|---|
str1 | string | - First string |
str2 | string | - Second string |
function normalizeText(text: string, ignoreWhitespace?: boolean, ignoreCase?: boolean): string
Normalize text for comparison
| Parameter | Type | Description |
|---|---|---|
text | string | - Text to normalize |
ignoreWhitespaceoptional | boolean | - Remove all whitespace |
ignoreCaseoptional | boolean | - Convert to lowercase |
dist/core/myers
Functions
function myersDiff<T>(oldSeq: T[], newSeq: T[], equals?: EqualityFn<T>): DiffResult
Myers diff algorithm for sequences Finds the shortest edit script to transform oldSeq into newSeq.
| Parameter | Type | Description |
|---|---|---|
oldSeq | T[] | - Old sequence |
newSeq | T[] | - New sequence |
equalsoptional | EqualityFn<T> | - Equality comparison function |
function myersDiffString(oldStr: string, newStr: string): DiffResult
Myers diff for strings Convenience function that operates on strings instead of arrays.
| Parameter | Type | Description |
|---|---|---|
oldStr | string | - Old string |
newStr | string | - New string |
dist/core/myers
Functions
function myersDiff<T>(oldSeq: T[], newSeq: T[], equals?: EqualityFn<T>): DiffResult
Myers diff algorithm for sequences Finds the shortest edit script to transform oldSeq into newSeq.
| Parameter | Type | Description |
|---|---|---|
oldSeq | T[] | - Old sequence |
newSeq | T[] | - New sequence |
equalsoptional | EqualityFn<T> | - Equality comparison function |
function myersDiffString(oldStr: string, newStr: string): DiffResult
Myers diff for strings Convenience function that operates on strings instead of arrays.
| Parameter | Type | Description |
|---|---|---|
oldStr | string | - Old string |
newStr | string | - New string |
dist/core/patience
Functions
function patienceDiff<T>(oldSeq: T[], newSeq: T[], equals?: EqualityFn<T>): DiffResult
Patience diff algorithm for sequences
| Parameter | Type | Description |
|---|---|---|
oldSeq | T[] | - Old sequence |
newSeq | T[] | - New sequence |
equalsoptional | EqualityFn<T> | - Equality comparison function |
function patienceDiffString(oldStr: string, newStr: string): DiffResult
Patience diff for strings Convenience function that operates on strings instead of arrays
| Parameter | Type | Description |
|---|---|---|
oldStr | string | - Old string |
newStr | string | - New string |
dist/core/patience
Functions
function patienceDiff<T>(oldSeq: T[], newSeq: T[], equals?: EqualityFn<T>): DiffResult
Patience diff algorithm for sequences
| Parameter | Type | Description |
|---|---|---|
oldSeq | T[] | - Old sequence |
newSeq | T[] | - New sequence |
equalsoptional | EqualityFn<T> | - Equality comparison function |
function patienceDiffString(oldStr: string, newStr: string): DiffResult
Patience diff for strings Convenience function that operates on strings instead of arrays
| Parameter | Type | Description |
|---|---|---|
oldStr | string | - Old string |
newStr | string | - New string |
dist
Functions
function computeLCS<T>(oldSeq: T[], newSeq: T[], equals?: EqualityFn<T>): number[][]
Longest Common Subsequence (LCS) using dynamic programming Time complexity: O(n * m) Space complexity: O(n * m)
| Parameter | Type | Description |
|---|---|---|
oldSeq | T[] | - Old sequence |
newSeq | T[] | - New sequence |
equalsoptional | EqualityFn<T> | - Equality comparison function |
function backtrackLCS<T>(dp: number[][], oldSeq: T[], newSeq: T[], equals?: EqualityFn<T>): Array<{
oldIndex: number;
newIndex: number;
}>Backtrack LCS to find actual common subsequence
| Parameter | Type | Description |
|---|---|---|
dp | number[][] | - DP table from computeLCS |
oldSeq | T[] | - Old sequence |
newSeq | T[] | - New sequence |
equalsoptional | EqualityFn<T> | - Equality comparison function |
function buildChangesFromMatches<T>(oldSeq: T[], newSeq: T[], matches: Array<{
oldIndex: number;
newIndex: number;
}>): DiffChange[]Convert edit operations to DiffChange array Merges consecutive operations of the same type into single changes
| Parameter | Type | Description |
|---|---|---|
oldSeq | T[] | - Old sequence |
newSeq | T[] | - New sequence |
matches | Array<{
oldIndex: number;
newIndex: number;
}> | - Matching indices from LCS |
function mergeConsecutiveChanges(changes: DiffChange[]): DiffChange[]
Merge consecutive changes of the same type Combines adjacent additions or deletions into single changes
| Parameter | Type | Description |
|---|---|---|
changes | DiffChange[] | - Array of diff changes |
function editDistance(oldStr: string, newStr: string): number
Calculate edit distance (Levenshtein distance) Number of single-character edits needed to transform one string into another
| Parameter | Type | Description |
|---|---|---|
oldStr | string | - Old string |
newStr | string | - New string |
function similarity(str1: string, str2: string): number
Calculate similarity score between two strings Returns value between 0 (completely different) and 1 (identical)
| Parameter | Type | Description |
|---|---|---|
str1 | string | - First string |
str2 | string | - Second string |
function normalizeText(text: string, ignoreWhitespace?: boolean, ignoreCase?: boolean): string
Normalize text for comparison
| Parameter | Type | Description |
|---|---|---|
text | string | - Text to normalize |
ignoreWhitespaceoptional | boolean | - Remove all whitespace |
ignoreCaseoptional | boolean | - Convert to lowercase |
dist/patch
Functions
function createPatch(oldStr: string, newStr: string, oldFileName?: string, newFileName?: string, options?: PatchOptions): ParsedPatch
Create a patch from two strings Generates a unified diff patch that can be applied with `patch` command.
| Parameter | Type | Description |
|---|---|---|
oldStr | string | - Original string |
newStr | string | - New string |
oldFileNameoptional | string | - Optional old file name |
newFileNameoptional | string | - Optional new file name |
optionsoptional | PatchOptions | - Patch options |
function createTwoFilesPatch(oldStr: string, newStr: string, oldPath: string, newPath: string, oldHeader?: string, newHeader?: string, options?: PatchOptions): string
Create a two-file patch (with file headers)
| Parameter | Type | Description |
|---|---|---|
oldStr | string | - Original string |
newStr | string | - New string |
oldPath | string | - Old file path |
newPath | string | - New file path |
oldHeaderoptional | string | - Optional old file header |
newHeaderoptional | string | - Optional new file header |
optionsoptional | PatchOptions | - Patch options |
function formatPatch(patch: ParsedPatch, oldHeader?: string, newHeader?: string): string
Format a parsed patch as unified diff string
| Parameter | Type | Description |
|---|---|---|
patch | ParsedPatch | - Parsed patch |
oldHeaderoptional | string | - Optional old file header |
newHeaderoptional | string | - Optional new file header |
function applyPatch(source: string, patch: ParsedPatch | string, options?: {
fuzzFactor?: number;
}): string | nullApply patch result to a string
| Parameter | Type | Description |
|---|---|---|
source | string | - Original string |
patch | ParsedPatch | string | - Parsed patch or patch string |
optionsoptional | {
fuzzFactor?: number;
} | - Application options |
function applyPatches(source: string, patches: (ParsedPatch | string)[], options?: {
fuzzFactor?: number;
}): string | nullApply multiple patches to a source
| Parameter | Type | Description |
|---|---|---|
source | string | - Original string |
patches | (ParsedPatch | string)[] | - Array of patches |
optionsoptional | {
fuzzFactor?: number;
} | - Application options |
function parsePatch(patchContent: string): ParsedPatch[]
Parse a unified diff patch string Parses patches in unified diff format (git diff output).
| Parameter | Type | Description |
|---|---|---|
patchContent | string | - Patch string |
function parseMultiplePatches(content: string): ParsedPatch[]
Parse multiple patch files Parses a string containing multiple patch files.
| Parameter | Type | Description |
|---|---|---|
content | string | - Content with multiple patches |
function validatePatch(patch: ParsedPatch): boolean
Validate a parsed patch Checks if a patch is valid and can be applied.
| Parameter | Type | Description |
|---|---|---|
patch | ParsedPatch | - Parsed patch |
dist/patch
Functions
function createPatch(oldStr: string, newStr: string, oldFileName?: string, newFileName?: string, options?: PatchOptions): ParsedPatch
Create a patch from two strings Generates a unified diff patch that can be applied with `patch` command.
| Parameter | Type | Description |
|---|---|---|
oldStr | string | - Original string |
newStr | string | - New string |
oldFileNameoptional | string | - Optional old file name |
newFileNameoptional | string | - Optional new file name |
optionsoptional | PatchOptions | - Patch options |
function createTwoFilesPatch(oldStr: string, newStr: string, oldPath: string, newPath: string, oldHeader?: string, newHeader?: string, options?: PatchOptions): string
Create a two-file patch (with file headers)
| Parameter | Type | Description |
|---|---|---|
oldStr | string | - Original string |
newStr | string | - New string |
oldPath | string | - Old file path |
newPath | string | - New file path |
oldHeaderoptional | string | - Optional old file header |
newHeaderoptional | string | - Optional new file header |
optionsoptional | PatchOptions | - Patch options |
function formatPatch(patch: ParsedPatch, oldHeader?: string, newHeader?: string): string
Format a parsed patch as unified diff string
| Parameter | Type | Description |
|---|---|---|
patch | ParsedPatch | - Parsed patch |
oldHeaderoptional | string | - Optional old file header |
newHeaderoptional | string | - Optional new file header |
function applyPatch(source: string, patch: ParsedPatch | string, options?: {
fuzzFactor?: number;
}): string | nullApply patch result to a string
| Parameter | Type | Description |
|---|---|---|
source | string | - Original string |
patch | ParsedPatch | string | - Parsed patch or patch string |
optionsoptional | {
fuzzFactor?: number;
} | - Application options |
function applyPatches(source: string, patches: (ParsedPatch | string)[], options?: {
fuzzFactor?: number;
}): string | nullApply multiple patches to a source
| Parameter | Type | Description |
|---|---|---|
source | string | - Original string |
patches | (ParsedPatch | string)[] | - Array of patches |
optionsoptional | {
fuzzFactor?: number;
} | - Application options |
function parsePatch(patchContent: string): ParsedPatch[]
Parse a unified diff patch string Parses patches in unified diff format (git diff output).
| Parameter | Type | Description |
|---|---|---|
patchContent | string | - Patch string |
function parseMultiplePatches(content: string): ParsedPatch[]
Parse multiple patch files Parses a string containing multiple patch files.
| Parameter | Type | Description |
|---|---|---|
content | string | - Content with multiple patches |
function validatePatch(patch: ParsedPatch): boolean
Validate a parsed patch Checks if a patch is valid and can be applied.
| Parameter | Type | Description |
|---|---|---|
patch | ParsedPatch | - Parsed patch |
dist/sentence-BdGQrRfg
Functions
function diffLines(oldText: string, newText: string, options?: DiffOptions): DiffResult
Diff two texts line by line This is the most commonly used diff function, comparing text line-by-line. Used by Git, testing frameworks, and most diff tools.
| Parameter | Type | Description |
|---|---|---|
oldText | string | - Original text |
newText | string | - New text |
optionsoptional | DiffOptions | - Diff options |
function diffLinesPatiently(oldText: string, newText: string, options?: DiffOptions): DiffResult
Diff two texts line by line using Patience algorithm Produces more readable diffs, especially for code with moved sections
| Parameter | Type | Description |
|---|---|---|
oldText | string | - Original text |
newText | string | - New text |
optionsoptional | DiffOptions | - Diff options |
function diffLinesHistogram(oldText: string, newText: string, options?: DiffOptions): DiffResult
Diff two texts line by line using Histogram algorithm Balanced between speed and output quality
| Parameter | Type | Description |
|---|---|---|
oldText | string | - Original text |
newText | string | - New text |
optionsoptional | DiffOptions | - Diff options |
function diffWords(oldText: string, newText: string, options?: DiffOptions): DiffResult
Diff two texts word by word Useful for comparing sentences or paragraphs where you want to see which words changed, not which characters.
| Parameter | Type | Description |
|---|---|---|
oldText | string | - Original text |
newText | string | - New text |
optionsoptional | DiffOptions | - Diff options |
function diffChars(oldText: string, newText: string, options?: DiffOptions): DiffResult
Diff two texts character by character Finest-grained diff showing exactly which characters changed. Most useful for short strings or when you need precise differences.
| Parameter | Type | Description |
|---|---|---|
oldText | string | - Original text |
newText | string | - New text |
optionsoptional | DiffOptions | - Diff options |
function diffSentences(oldText: string, newText: string, options?: DiffOptions): DiffResult
Diff two texts sentence by sentence Useful for comparing paragraphs or documents where you want to see which sentences changed.
| Parameter | Type | Description |
|---|---|---|
oldText | string | - Original text |
newText | string | - New text |
optionsoptional | DiffOptions | - Diff options |
dist/sentence-DG45kMPQ
Functions
function diffLines(oldText: string, newText: string, options?: DiffOptions): DiffResult
Diff two texts line by line This is the most commonly used diff function, comparing text line-by-line. Used by Git, testing frameworks, and most diff tools.
| Parameter | Type | Description |
|---|---|---|
oldText | string | - Original text |
newText | string | - New text |
optionsoptional | DiffOptions | - Diff options |
function diffLinesPatiently(oldText: string, newText: string, options?: DiffOptions): DiffResult
Diff two texts line by line using Patience algorithm Produces more readable diffs, especially for code with moved sections
| Parameter | Type | Description |
|---|---|---|
oldText | string | - Original text |
newText | string | - New text |
optionsoptional | DiffOptions | - Diff options |
function diffLinesHistogram(oldText: string, newText: string, options?: DiffOptions): DiffResult
Diff two texts line by line using Histogram algorithm Balanced between speed and output quality
| Parameter | Type | Description |
|---|---|---|
oldText | string | - Original text |
newText | string | - New text |
optionsoptional | DiffOptions | - Diff options |
function diffWords(oldText: string, newText: string, options?: DiffOptions): DiffResult
Diff two texts word by word Useful for comparing sentences or paragraphs where you want to see which words changed, not which characters.
| Parameter | Type | Description |
|---|---|---|
oldText | string | - Original text |
newText | string | - New text |
optionsoptional | DiffOptions | - Diff options |
function diffChars(oldText: string, newText: string, options?: DiffOptions): DiffResult
Diff two texts character by character Finest-grained diff showing exactly which characters changed. Most useful for short strings or when you need precise differences.
| Parameter | Type | Description |
|---|---|---|
oldText | string | - Original text |
newText | string | - New text |
optionsoptional | DiffOptions | - Diff options |
function diffSentences(oldText: string, newText: string, options?: DiffOptions): DiffResult
Diff two texts sentence by sentence Useful for comparing paragraphs or documents where you want to see which sentences changed.
| Parameter | Type | Description |
|---|---|---|
oldText | string | - Original text |
newText | string | - New text |
optionsoptional | DiffOptions | - Diff options |
dist/structured
Functions
function diffJson(oldValue: unknown, newValue: unknown, options?: DiffOptions): DiffResult
Diff two JSON-serializable values Stringifies the values with pretty-printing and diffs line by line. Useful for comparing configuration objects, API responses, etc.
| Parameter | Type | Description |
|---|---|---|
oldValue | unknown | - Original value |
newValue | unknown | - New value |
optionsoptional | DiffOptions | - Diff options |
function diffJsonString(oldJson: string, newJson: string, options?: DiffOptions): DiffResult
Diff two JSON strings Parses the JSON strings and diffs them.
| Parameter | Type | Description |
|---|---|---|
oldJson | string | - Original JSON string |
newJson | string | - New JSON string |
optionsoptional | DiffOptions | - Diff options |
function diffArrays<T>(oldArray: T[], newArray: T[], equals?: EqualityFn<T>): DiffResult
Diff two arrays element by element Uses Myers algorithm to find the minimal edit script. Supports custom equality function for complex element types.
| Parameter | Type | Description |
|---|---|---|
oldArray | T[] | - Original array |
newArray | T[] | - New array |
equalsoptional | EqualityFn<T> | - Optional custom equality function |
function diffPrimitiveArrays<T extends string | number | boolean>(oldArray: T[], newArray: T[]): DiffResult
Diff two arrays of primitives (shorthand)
| Parameter | Type | Description |
|---|---|---|
oldArray | T[] | - Original array |
newArray | T[] | - New array |
function diffObjects(oldObj: Record<string, unknown>, newObj: Record<string, unknown>, path?: (string | number)[]): StructuredDiffChange[]
Diff two objects and return structured changes Returns an array of changes with operations: - CREATE: Property was added - REMOVE: Property was removed - CHANGE: Property value changed This is similar to the microdiff package but with deep comparison support.
| Parameter | Type | Description |
|---|---|---|
oldObj | Record<string, unknown> | - Original object |
newObj | Record<string, unknown> | - New object |
pathoptional | (string | number)[] | - Current path (for recursion) |
function applyChanges(obj: Record<string, unknown>, changes: StructuredDiffChange[]): Record<string, unknown>
Apply structured changes to an object Takes an array of changes and applies them to create a new object.
| Parameter | Type | Description |
|---|---|---|
obj | Record<string, unknown> | - Original object |
changes | StructuredDiffChange[] | - Array of changes to apply |
dist/structured
Functions
function diffJson(oldValue: unknown, newValue: unknown, options?: DiffOptions): DiffResult
Diff two JSON-serializable values Stringifies the values with pretty-printing and diffs line by line. Useful for comparing configuration objects, API responses, etc.
| Parameter | Type | Description |
|---|---|---|
oldValue | unknown | - Original value |
newValue | unknown | - New value |
optionsoptional | DiffOptions | - Diff options |
function diffJsonString(oldJson: string, newJson: string, options?: DiffOptions): DiffResult
Diff two JSON strings Parses the JSON strings and diffs them.
| Parameter | Type | Description |
|---|---|---|
oldJson | string | - Original JSON string |
newJson | string | - New JSON string |
optionsoptional | DiffOptions | - Diff options |
function diffArrays<T>(oldArray: T[], newArray: T[], equals?: EqualityFn<T>): DiffResult
Diff two arrays element by element Uses Myers algorithm to find the minimal edit script. Supports custom equality function for complex element types.
| Parameter | Type | Description |
|---|---|---|
oldArray | T[] | - Original array |
newArray | T[] | - New array |
equalsoptional | EqualityFn<T> | - Optional custom equality function |
function diffPrimitiveArrays<T extends string | number | boolean>(oldArray: T[], newArray: T[]): DiffResult
Diff two arrays of primitives (shorthand)
| Parameter | Type | Description |
|---|---|---|
oldArray | T[] | - Original array |
newArray | T[] | - New array |
function diffObjects(oldObj: Record<string, unknown>, newObj: Record<string, unknown>, path?: (string | number)[]): StructuredDiffChange[]
Diff two objects and return structured changes Returns an array of changes with operations: - CREATE: Property was added - REMOVE: Property was removed - CHANGE: Property value changed This is similar to the microdiff package but with deep comparison support.
| Parameter | Type | Description |
|---|---|---|
oldObj | Record<string, unknown> | - Original object |
newObj | Record<string, unknown> | - New object |
pathoptional | (string | number)[] | - Current path (for recursion) |
function applyChanges(obj: Record<string, unknown>, changes: StructuredDiffChange[]): Record<string, unknown>
Apply structured changes to an object Takes an array of changes and applies them to create a new object.
| Parameter | Type | Description |
|---|---|---|
obj | Record<string, unknown> | - Original object |
changes | StructuredDiffChange[] | - Array of changes to apply |
dist/text
Functions
function tokenizeLines(text: string): string[]
Tokenize text into lines Preserves line endings in the tokens for accurate reconstruction
| Parameter | Type | Description |
|---|---|---|
text | string | - Text to tokenize |
function tokenizeWords(text: string): string[]
Tokenize text into words Preserves whitespace as separate tokens for accurate reconstruction
| Parameter | Type | Description |
|---|---|---|
text | string | - Text to tokenize |
function tokenizeChars(text: string): string[]
Tokenize text into characters Handles Unicode properly using spread operator
| Parameter | Type | Description |
|---|---|---|
text | string | - Text to tokenize |
function tokenizeSentences(text: string): string[]
Tokenize text into sentences Splits on sentence-ending punctuation (., !, ?)
| Parameter | Type | Description |
|---|---|---|
text | string | - Text to tokenize |
function stripNewline(line: string): string
Remove trailing newline from a line Useful when you want to compare line content without line endings
| Parameter | Type | Description |
|---|---|---|
line | string | - Line with possible newline |
function joinTokens(tokens: string[]): string
Join tokens back into text Simple utility to reconstruct text from tokens
| Parameter | Type | Description |
|---|---|---|
tokens | string[] | - Array of tokens |
dist/text
Functions
function tokenizeLines(text: string): string[]
Tokenize text into lines Preserves line endings in the tokens for accurate reconstruction
| Parameter | Type | Description |
|---|---|---|
text | string | - Text to tokenize |
function tokenizeWords(text: string): string[]
Tokenize text into words Preserves whitespace as separate tokens for accurate reconstruction
| Parameter | Type | Description |
|---|---|---|
text | string | - Text to tokenize |
function tokenizeChars(text: string): string[]
Tokenize text into characters Handles Unicode properly using spread operator
| Parameter | Type | Description |
|---|---|---|
text | string | - Text to tokenize |
function tokenizeSentences(text: string): string[]
Tokenize text into sentences Splits on sentence-ending punctuation (., !, ?)
| Parameter | Type | Description |
|---|---|---|
text | string | - Text to tokenize |
function stripNewline(line: string): string
Remove trailing newline from a line Useful when you want to compare line content without line endings
| Parameter | Type | Description |
|---|---|---|
line | string | - Line with possible newline |
function joinTokens(tokens: string[]): string
Join tokens back into text Simple utility to reconstruct text from tokens
| Parameter | Type | Description |
|---|---|---|
tokens | string[] | - Array of tokens |
Taxes calculated at checkout based on your location.