Modules@robinpath/diff
diff

@robinpath/diff

0.1.1Node.jsPublic

Text and data diffing: line, word, character, object, and array diffs with unified output

Diff

Text and data diffing: line, word, character, object, and array diffs with unified output

Package: @robinpath/diff | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the diff module when you need to:

  • Diff two strings line by line -- Use diff.lines to perform this operation
  • Diff two strings character by character -- Use diff.chars to perform this operation
  • Diff two strings word by word -- Use diff.words to perform this operation
  • Diff two objects -- Use diff.objects to perform this operation
  • Diff two arrays -- Use diff.arrays to perform this operation

Quick Reference

FunctionDescriptionReturns
linesDiff two strings line by lineArray of {type, value}
charsDiff two strings character by characterArray of {type, value}
wordsDiff two strings word by wordArray of {type, value}
objectsDiff two objectsArray of {path, type, oldValue?, newValue?}
arraysDiff two arrays{added, removed, common}
patchApply a line diff to produce the new stringPatched string
unifiedGenerate unified diff format (like git diff)Unified diff string
isEqualDeep equality checkTrue if deeply equal
statsGet diff statistics from a diff result{additions, deletions, unchanged}

Functions

lines

Diff two strings line by line

Module: diff | Returns: array -- Array of {type, value}

diff.lines $old $new
ParameterTypeRequiredDescription
astringYesOriginal text
bstringYesModified text

chars

Diff two strings character by character

Module: diff | Returns: array -- Array of {type, value}

diff.chars "cat" "car"
ParameterTypeRequiredDescription
astringYesOriginal
bstringYesModified

words

Diff two strings word by word

Module: diff | Returns: array -- Array of {type, value}

diff.words $old $new
ParameterTypeRequiredDescription
astringYesOriginal
bstringYesModified

objects

Diff two objects

Module: diff | Returns: array -- Array of {path, type, oldValue?, newValue?}

diff.objects $obj1 $obj2
ParameterTypeRequiredDescription
aobjectYesOriginal object
bobjectYesModified object

arrays

Diff two arrays

Module: diff | Returns: object -- {added, removed, common}

diff.arrays $arr1 $arr2
ParameterTypeRequiredDescription
aarrayYesOriginal array
barrayYesModified array

patch

Apply a line diff to produce the new string

Module: diff | Returns: string -- Patched string

diff.patch $lineDiff
ParameterTypeRequiredDescription
diffarrayYesDiff result from lines()

unified

Generate unified diff format (like git diff)

Module: diff | Returns: string -- Unified diff string

diff.unified $old $new
ParameterTypeRequiredDescription
astringYesOriginal text
bstringYesModified text

isEqual

Deep equality check

Module: diff | Returns: boolean -- True if deeply equal

diff.isEqual $a $b
ParameterTypeRequiredDescription
aanyYesFirst value
banyYesSecond value

stats

Get diff statistics from a diff result

Module: diff | Returns: object -- {additions, deletions, unchanged}

diff.stats $lineDiff
ParameterTypeRequiredDescription
diffarrayYesDiff result

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Lines and validate result"
do
  set $result as diff.lines $old $new
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step Diff workflow

Chain multiple diff operations together.

@desc "Lines, chars, and more"
do
  set $r_lines as diff.lines $old $new
  set $r_chars as diff.chars "cat" "car"
  set $r_words as diff.words $old $new
  print "All operations complete"
enddo

2. Safe lines with validation

Check results before proceeding.

@desc "Lines and validate result"
do
  set $result as diff.lines $old $new
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/diff

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size4.5 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

utilities