The code originally came from project Synchrotron, created by Tony Garnock-Jones. For more detail please visit:
To install node-diff3 as a dependency in your project:
$ npm install --save node-diff3
index.mjs- ES6 module
dist/index.js- UMD module, ES6 syntax
dist/index.es5.js- UMD module, ES5 syntax
Whether you require or import it, it should just work.
const Diff3 = ; // UMD import allconst diff3Merge = diff3Merge; // UMD import named// or; // ES6 import all; // ES6 import named
You can also use node-diff3 directly in a web browser. A good way to do this is to fetch the file from the jsDelivr CDN, which can even deliver minified versions.
The latest versions of many web browsers now support ES6 modules in script tags like this:
Older versions of modern ES6-capable browsers can still load the UMD build:
Or if you need to support even older browsers like Internet Explorer, fetch the ES5 version:
- 3-way diff and merging
- 2-way diff and patching
- Longest Common Sequence (LCS)
3-way diff and merging
# Diff3.diff3Merge(a, o, b, options)
Performs a 3-way diff on buffers
o (original), and
The buffers may be arrays or strings. If strings, they will be split into arrays on whitespace
/\s+/ by default.
The returned result alternates between "ok" and "conflict" blocks.
const o = 'AA' 'ZZ' '00' 'M' '99';const a = 'AA' 'a' 'b' 'c' 'ZZ' 'new' '00' 'a' 'a' 'M' '99';const b = 'AA' 'a' 'd' 'c' 'ZZ' '11' 'M' 'z' 'z' '99';const result = Diff3;
Options may passed as an object:
excludeFalseConflicts: truestringSeparator: /\s+/
excludeFalseConflicts- If both
bcontain an identical change from
o, this is considered a "false" conflict.
stringSeparator- If inputs buffers are strings, this controls how to split the strings into arrays. The separator value may be a string or a regular expression, as it is just passed to String.split().
# Diff3.merge(a, o, b, options)
Passes arguments to diff3Merge to generate a diff3-style merge result.
const r = Diff3;const result = rresult;// [// 'AA',// '\n<<<<<<<<<\n',// 'a',// 'b',// 'c',// '\n=========\n',// 'a',// 'd',// 'c',// '\n>>>>>>>>>\n',// 'ZZ',// '\n<<<<<<<<<\n',// 'new',// '00',// 'a',// 'a',// '\n=========\n',// '11',// '\n>>>>>>>>>\n',// 'M',// 'z',// 'z',// '99'// ]
# Diff3.mergeDiff3(a, o, b, options)
const r = Diff3;const result = rresult;// [// 'AA',// '<<<<<<< a',// 'a',// 'b',// 'c',// '||||||| o',// '=======',// 'a',// 'd',// 'c',// '>>>>>>> b',// 'ZZ',// '<<<<<<< a',// 'new',// '00',// 'a',// 'a',// '||||||| o',// '00',// '=======',// '11',// '>>>>>>> b',// 'M',// 'z',// 'z',// '99'// ]
// labels for conflict marker lineslabel:a: 'a'o: 'o'b: 'b'
# Diff3.mergeDigIn(a, o, b, options)
Passes arguments to diff3Merge to generate a digin-style merge result.
# Diff3.diff3MergeRegions(a, o, b)
Low-level function used by diff3Merge to determine the stable and unstable regions between
2-way diff and patching
# Diff3.diffPatch(buffer1, buffer2)
Performs a diff between arrays
patch result contains the information about the differing regions and can be applied to
buffer1 to yield
const buffer1 = 'AA' 'a' 'b' 'c' 'ZZ' 'new' '00' 'a' 'a' 'M' '99';const buffer2 = 'AA' 'a' 'd' 'c' 'ZZ' '11' 'M' 'z' 'z' '99';const patch = Diff3;// `patch` contains the information needed to turn `buffer1` into `buffer2`
# Diff3.patch(buffer1, patch)
Applies a patch to a buffer, returning a new buffer without modifying the original.
const result = Diff3;// `result` contains a new arrray which is a copy of `buffer2`
Strips some extra information from the patch, returning a new patch without modifying the original.
The "stripped" patch can still patch
buffer2, but can no londer be inverted.
const stripped = Diff3;// `stripped` contains a copy of a patch but with the extra information removed
Inverts the patch (for example to turn
buffer2 back into
buffer1), returning a new patch without modifying the original.
const inverted = Diff3;// `inverted` contains a copy of a patch to turn `buffer2` back into `buffer1`
# Diff3.diffComm(buffer1, buffer2)
Returns a comm-style result of the differences between
# Diff3.diffIndices(buffer1, buffer2)
Low-level function used by diff3MergeRegions to determine differing regions between
Longest Common Sequence (LCS)
# Diff3.LCS(buffer1, buffer2)
Low-level function used by other functions to find the LCS between
Returns a result linked list chain containing the common sequence path.