generic-diff
Diff arrays or array-like objects, such as strings. Based on "An O(ND) Difference Algorithm and its Variations" (Myers, 1986).
diff( a, b [, eql] )
Diffs the array-like objects a
and b
, returning a summary of edits required to turn a
into b
. Defaults to using strict equality (===
) to compare items in a
and b
. A comparison function, eql
, can optionally be used for more nuanced comparisons; the signature of this function is (item from a, item from b) => Boolean
.
The “summary of changes” is an array of objects with three properties: items
, an array of one or more items from a
or b
, and boolean properties added
and removed
, indicating whether the item(s) should be added or removed from a
, respectively. For example, if we’re diffing the strings abc
and abd
, the summary of changes would look like:
items: 'a' 'b' added: false removed: false items: 'c' added: false removed: true items: 'd' added: true removed: false
Example
Diff two strings, creating an HTML representation of their differences:
var diff = var changes = changes = changes console// 'fal<del>afe</del>l<ins>acy</ins>'
For a slightly more involved example, this gist demonstrates how to diff two files and produce an output similar to the UNIX diff
command.
LICENSE
MIT