Neo Zero

diff

Pool

Modern, tree-shakeable diff library - 70% smaller than diff, multiple algorithms, TypeScript-native

$ lpm install @lpm.dev/neo.diff
74 exportsTypeScript

dist

Functions

computeLCS
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)

ParameterTypeDescription
oldSeq
T[]- Old sequence
newSeq
T[]- New sequence
equalsoptional
EqualityFn<T>- Equality comparison function
backtrackLCS
function backtrackLCS<T>(dp: number[][], oldSeq: T[], newSeq: T[], equals?: EqualityFn<T>): Array<{
    oldIndex: number;
    newIndex: number;
}>

Backtrack LCS to find actual common subsequence

ParameterTypeDescription
dp
number[][]- DP table from computeLCS
oldSeq
T[]- Old sequence
newSeq
T[]- New sequence
equalsoptional
EqualityFn<T>- Equality comparison function
buildChangesFromMatches
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

ParameterTypeDescription
oldSeq
T[]- Old sequence
newSeq
T[]- New sequence
matches
Array<{ oldIndex: number; newIndex: number; }>- Matching indices from LCS
mergeConsecutiveChanges
function mergeConsecutiveChanges(changes: DiffChange[]): DiffChange[]

Merge consecutive changes of the same type Combines adjacent additions or deletions into single changes

ParameterTypeDescription
changes
DiffChange[]- Array of diff changes
editDistance
function editDistance(oldStr: string, newStr: string): number

Calculate edit distance (Levenshtein distance) Number of single-character edits needed to transform one string into another

ParameterTypeDescription
oldStr
string- Old string
newStr
string- New string
similarity
function similarity(str1: string, str2: string): number

Calculate similarity score between two strings Returns value between 0 (completely different) and 1 (identical)

ParameterTypeDescription
str1
string- First string
str2
string- Second string
normalizeText
function normalizeText(text: string, ignoreWhitespace?: boolean, ignoreCase?: boolean): string

Normalize text for comparison

ParameterTypeDescription
text
string- Text to normalize
ignoreWhitespaceoptional
boolean- Remove all whitespace
ignoreCaseoptional
boolean- Convert to lowercase

dist/core/myers

Functions

myersDiff
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.

ParameterTypeDescription
oldSeq
T[]- Old sequence
newSeq
T[]- New sequence
equalsoptional
EqualityFn<T>- Equality comparison function
myersDiffString
function myersDiffString(oldStr: string, newStr: string): DiffResult

Myers diff for strings Convenience function that operates on strings instead of arrays.

ParameterTypeDescription
oldStr
string- Old string
newStr
string- New string

dist/core/myers

Functions

myersDiff
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.

ParameterTypeDescription
oldSeq
T[]- Old sequence
newSeq
T[]- New sequence
equalsoptional
EqualityFn<T>- Equality comparison function
myersDiffString
function myersDiffString(oldStr: string, newStr: string): DiffResult

Myers diff for strings Convenience function that operates on strings instead of arrays.

ParameterTypeDescription
oldStr
string- Old string
newStr
string- New string

dist/core/patience

Functions

patienceDiff
function patienceDiff<T>(oldSeq: T[], newSeq: T[], equals?: EqualityFn<T>): DiffResult

Patience diff algorithm for sequences

ParameterTypeDescription
oldSeq
T[]- Old sequence
newSeq
T[]- New sequence
equalsoptional
EqualityFn<T>- Equality comparison function
patienceDiffString
function patienceDiffString(oldStr: string, newStr: string): DiffResult

Patience diff for strings Convenience function that operates on strings instead of arrays

ParameterTypeDescription
oldStr
string- Old string
newStr
string- New string

dist/core/patience

Functions

patienceDiff
function patienceDiff<T>(oldSeq: T[], newSeq: T[], equals?: EqualityFn<T>): DiffResult

Patience diff algorithm for sequences

ParameterTypeDescription
oldSeq
T[]- Old sequence
newSeq
T[]- New sequence
equalsoptional
EqualityFn<T>- Equality comparison function
patienceDiffString
function patienceDiffString(oldStr: string, newStr: string): DiffResult

Patience diff for strings Convenience function that operates on strings instead of arrays

ParameterTypeDescription
oldStr
string- Old string
newStr
string- New string

dist

Functions

computeLCS
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)

ParameterTypeDescription
oldSeq
T[]- Old sequence
newSeq
T[]- New sequence
equalsoptional
EqualityFn<T>- Equality comparison function
backtrackLCS
function backtrackLCS<T>(dp: number[][], oldSeq: T[], newSeq: T[], equals?: EqualityFn<T>): Array<{
    oldIndex: number;
    newIndex: number;
}>

Backtrack LCS to find actual common subsequence

ParameterTypeDescription
dp
number[][]- DP table from computeLCS
oldSeq
T[]- Old sequence
newSeq
T[]- New sequence
equalsoptional
EqualityFn<T>- Equality comparison function
buildChangesFromMatches
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

ParameterTypeDescription
oldSeq
T[]- Old sequence
newSeq
T[]- New sequence
matches
Array<{ oldIndex: number; newIndex: number; }>- Matching indices from LCS
mergeConsecutiveChanges
function mergeConsecutiveChanges(changes: DiffChange[]): DiffChange[]

Merge consecutive changes of the same type Combines adjacent additions or deletions into single changes

ParameterTypeDescription
changes
DiffChange[]- Array of diff changes
editDistance
function editDistance(oldStr: string, newStr: string): number

Calculate edit distance (Levenshtein distance) Number of single-character edits needed to transform one string into another

ParameterTypeDescription
oldStr
string- Old string
newStr
string- New string
similarity
function similarity(str1: string, str2: string): number

Calculate similarity score between two strings Returns value between 0 (completely different) and 1 (identical)

ParameterTypeDescription
str1
string- First string
str2
string- Second string
normalizeText
function normalizeText(text: string, ignoreWhitespace?: boolean, ignoreCase?: boolean): string

Normalize text for comparison

ParameterTypeDescription
text
string- Text to normalize
ignoreWhitespaceoptional
boolean- Remove all whitespace
ignoreCaseoptional
boolean- Convert to lowercase

dist/patch

Functions

createPatch
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.

ParameterTypeDescription
oldStr
string- Original string
newStr
string- New string
oldFileNameoptional
string- Optional old file name
newFileNameoptional
string- Optional new file name
optionsoptional
PatchOptions- Patch options
createTwoFilesPatch
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)

ParameterTypeDescription
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
formatPatch
function formatPatch(patch: ParsedPatch, oldHeader?: string, newHeader?: string): string

Format a parsed patch as unified diff string

ParameterTypeDescription
patch
ParsedPatch- Parsed patch
oldHeaderoptional
string- Optional old file header
newHeaderoptional
string- Optional new file header
applyPatch
function applyPatch(source: string, patch: ParsedPatch | string, options?: {
    fuzzFactor?: number;
}): string | null

Apply patch result to a string

ParameterTypeDescription
source
string- Original string
patch
ParsedPatch | string- Parsed patch or patch string
optionsoptional
{ fuzzFactor?: number; }- Application options
applyPatches
function applyPatches(source: string, patches: (ParsedPatch | string)[], options?: {
    fuzzFactor?: number;
}): string | null

Apply multiple patches to a source

ParameterTypeDescription
source
string- Original string
patches
(ParsedPatch | string)[]- Array of patches
optionsoptional
{ fuzzFactor?: number; }- Application options
parsePatch
function parsePatch(patchContent: string): ParsedPatch[]

Parse a unified diff patch string Parses patches in unified diff format (git diff output).

ParameterTypeDescription
patchContent
string- Patch string
parseMultiplePatches
function parseMultiplePatches(content: string): ParsedPatch[]

Parse multiple patch files Parses a string containing multiple patch files.

ParameterTypeDescription
content
string- Content with multiple patches
validatePatch
function validatePatch(patch: ParsedPatch): boolean

Validate a parsed patch Checks if a patch is valid and can be applied.

ParameterTypeDescription
patch
ParsedPatch- Parsed patch

dist/patch

Functions

createPatch
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.

ParameterTypeDescription
oldStr
string- Original string
newStr
string- New string
oldFileNameoptional
string- Optional old file name
newFileNameoptional
string- Optional new file name
optionsoptional
PatchOptions- Patch options
createTwoFilesPatch
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)

ParameterTypeDescription
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
formatPatch
function formatPatch(patch: ParsedPatch, oldHeader?: string, newHeader?: string): string

Format a parsed patch as unified diff string

ParameterTypeDescription
patch
ParsedPatch- Parsed patch
oldHeaderoptional
string- Optional old file header
newHeaderoptional
string- Optional new file header
applyPatch
function applyPatch(source: string, patch: ParsedPatch | string, options?: {
    fuzzFactor?: number;
}): string | null

Apply patch result to a string

ParameterTypeDescription
source
string- Original string
patch
ParsedPatch | string- Parsed patch or patch string
optionsoptional
{ fuzzFactor?: number; }- Application options
applyPatches
function applyPatches(source: string, patches: (ParsedPatch | string)[], options?: {
    fuzzFactor?: number;
}): string | null

Apply multiple patches to a source

ParameterTypeDescription
source
string- Original string
patches
(ParsedPatch | string)[]- Array of patches
optionsoptional
{ fuzzFactor?: number; }- Application options
parsePatch
function parsePatch(patchContent: string): ParsedPatch[]

Parse a unified diff patch string Parses patches in unified diff format (git diff output).

ParameterTypeDescription
patchContent
string- Patch string
parseMultiplePatches
function parseMultiplePatches(content: string): ParsedPatch[]

Parse multiple patch files Parses a string containing multiple patch files.

ParameterTypeDescription
content
string- Content with multiple patches
validatePatch
function validatePatch(patch: ParsedPatch): boolean

Validate a parsed patch Checks if a patch is valid and can be applied.

ParameterTypeDescription
patch
ParsedPatch- Parsed patch

dist/sentence-BdGQrRfg

Functions

diffLines
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.

ParameterTypeDescription
oldText
string- Original text
newText
string- New text
optionsoptional
DiffOptions- Diff options
diffLinesPatiently
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

ParameterTypeDescription
oldText
string- Original text
newText
string- New text
optionsoptional
DiffOptions- Diff options
diffLinesHistogram
function diffLinesHistogram(oldText: string, newText: string, options?: DiffOptions): DiffResult

Diff two texts line by line using Histogram algorithm Balanced between speed and output quality

ParameterTypeDescription
oldText
string- Original text
newText
string- New text
optionsoptional
DiffOptions- Diff options
diffWords
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.

ParameterTypeDescription
oldText
string- Original text
newText
string- New text
optionsoptional
DiffOptions- Diff options
diffChars
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.

ParameterTypeDescription
oldText
string- Original text
newText
string- New text
optionsoptional
DiffOptions- Diff options
diffSentences
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.

ParameterTypeDescription
oldText
string- Original text
newText
string- New text
optionsoptional
DiffOptions- Diff options

dist/sentence-DG45kMPQ

Functions

diffLines
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.

ParameterTypeDescription
oldText
string- Original text
newText
string- New text
optionsoptional
DiffOptions- Diff options
diffLinesPatiently
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

ParameterTypeDescription
oldText
string- Original text
newText
string- New text
optionsoptional
DiffOptions- Diff options
diffLinesHistogram
function diffLinesHistogram(oldText: string, newText: string, options?: DiffOptions): DiffResult

Diff two texts line by line using Histogram algorithm Balanced between speed and output quality

ParameterTypeDescription
oldText
string- Original text
newText
string- New text
optionsoptional
DiffOptions- Diff options
diffWords
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.

ParameterTypeDescription
oldText
string- Original text
newText
string- New text
optionsoptional
DiffOptions- Diff options
diffChars
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.

ParameterTypeDescription
oldText
string- Original text
newText
string- New text
optionsoptional
DiffOptions- Diff options
diffSentences
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.

ParameterTypeDescription
oldText
string- Original text
newText
string- New text
optionsoptional
DiffOptions- Diff options

dist/structured

Functions

diffJson
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.

ParameterTypeDescription
oldValue
unknown- Original value
newValue
unknown- New value
optionsoptional
DiffOptions- Diff options
diffJsonString
function diffJsonString(oldJson: string, newJson: string, options?: DiffOptions): DiffResult

Diff two JSON strings Parses the JSON strings and diffs them.

ParameterTypeDescription
oldJson
string- Original JSON string
newJson
string- New JSON string
optionsoptional
DiffOptions- Diff options
diffArrays
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.

ParameterTypeDescription
oldArray
T[]- Original array
newArray
T[]- New array
equalsoptional
EqualityFn<T>- Optional custom equality function
diffPrimitiveArrays
function diffPrimitiveArrays<T extends string | number | boolean>(oldArray: T[], newArray: T[]): DiffResult

Diff two arrays of primitives (shorthand)

ParameterTypeDescription
oldArray
T[]- Original array
newArray
T[]- New array
diffObjects
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.

ParameterTypeDescription
oldObj
Record<string, unknown>- Original object
newObj
Record<string, unknown>- New object
pathoptional
(string | number)[]- Current path (for recursion)
applyChanges
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.

ParameterTypeDescription
obj
Record<string, unknown>- Original object
changes
StructuredDiffChange[]- Array of changes to apply

dist/structured

Functions

diffJson
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.

ParameterTypeDescription
oldValue
unknown- Original value
newValue
unknown- New value
optionsoptional
DiffOptions- Diff options
diffJsonString
function diffJsonString(oldJson: string, newJson: string, options?: DiffOptions): DiffResult

Diff two JSON strings Parses the JSON strings and diffs them.

ParameterTypeDescription
oldJson
string- Original JSON string
newJson
string- New JSON string
optionsoptional
DiffOptions- Diff options
diffArrays
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.

ParameterTypeDescription
oldArray
T[]- Original array
newArray
T[]- New array
equalsoptional
EqualityFn<T>- Optional custom equality function
diffPrimitiveArrays
function diffPrimitiveArrays<T extends string | number | boolean>(oldArray: T[], newArray: T[]): DiffResult

Diff two arrays of primitives (shorthand)

ParameterTypeDescription
oldArray
T[]- Original array
newArray
T[]- New array
diffObjects
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.

ParameterTypeDescription
oldObj
Record<string, unknown>- Original object
newObj
Record<string, unknown>- New object
pathoptional
(string | number)[]- Current path (for recursion)
applyChanges
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.

ParameterTypeDescription
obj
Record<string, unknown>- Original object
changes
StructuredDiffChange[]- Array of changes to apply

dist/text

Functions

tokenizeLines
function tokenizeLines(text: string): string[]

Tokenize text into lines Preserves line endings in the tokens for accurate reconstruction

ParameterTypeDescription
text
string- Text to tokenize
tokenizeWords
function tokenizeWords(text: string): string[]

Tokenize text into words Preserves whitespace as separate tokens for accurate reconstruction

ParameterTypeDescription
text
string- Text to tokenize
tokenizeChars
function tokenizeChars(text: string): string[]

Tokenize text into characters Handles Unicode properly using spread operator

ParameterTypeDescription
text
string- Text to tokenize
tokenizeSentences
function tokenizeSentences(text: string): string[]

Tokenize text into sentences Splits on sentence-ending punctuation (., !, ?)

ParameterTypeDescription
text
string- Text to tokenize
stripNewline
function stripNewline(line: string): string

Remove trailing newline from a line Useful when you want to compare line content without line endings

ParameterTypeDescription
line
string- Line with possible newline
joinTokens
function joinTokens(tokens: string[]): string

Join tokens back into text Simple utility to reconstruct text from tokens

ParameterTypeDescription
tokens
string[]- Array of tokens

dist/text

Functions

tokenizeLines
function tokenizeLines(text: string): string[]

Tokenize text into lines Preserves line endings in the tokens for accurate reconstruction

ParameterTypeDescription
text
string- Text to tokenize
tokenizeWords
function tokenizeWords(text: string): string[]

Tokenize text into words Preserves whitespace as separate tokens for accurate reconstruction

ParameterTypeDescription
text
string- Text to tokenize
tokenizeChars
function tokenizeChars(text: string): string[]

Tokenize text into characters Handles Unicode properly using spread operator

ParameterTypeDescription
text
string- Text to tokenize
tokenizeSentences
function tokenizeSentences(text: string): string[]

Tokenize text into sentences Splits on sentence-ending punctuation (., !, ?)

ParameterTypeDescription
text
string- Text to tokenize
stripNewline
function stripNewline(line: string): string

Remove trailing newline from a line Useful when you want to compare line content without line endings

ParameterTypeDescription
line
string- Line with possible newline
joinTokens
function joinTokens(tokens: string[]): string

Join tokens back into text Simple utility to reconstruct text from tokens

ParameterTypeDescription
tokens
string[]- Array of tokens
Unlimited AccessInstall as many Pool packages as you need.
Fund Real WorkEvery install you run sends revenue directly to the developer who built it.

Taxes calculated at checkout based on your location.

Weekly Installs
3
Version
1.0.0
Published
LicenseMIT
Size703.10 KB
Files47
Node version>= 18
TypeScriptYes