A javascript text differencing implementation.
-
JsDiff.diffChars(oldStr, newStr[, options]) - diffs two blocks of text, comparing character by character.
Returns a list of change objects (See below).
Options
ignoreCase: true to ignore casing difference. Defaults to false.
-
JsDiff.diffWords(oldStr, newStr[, options]) - diffs two blocks of text, comparing word by word, ignoring whitespace.
Returns a list of change objects (See below).
Options
ignoreCase: Same as in diffChars.
-
JsDiff.diffWordsWithSpace(oldStr, newStr[, options]) - diffs two blocks of text, comparing word by word, treating whitespace as significant.
Returns a list of change objects (See below).
-
JsDiff.diffLines(oldStr, newStr[, options]) - diffs two blocks of text, comparing line by line.
Options
ignoreWhitespace: true to ignore leading and trailing whitespace. This is the same as diffTrimmedLines
newlineIsToken: true to treat newline characters as separate tokens. This allows for changes to the newline structure to occur independently of the line content and to be treated as such. In general this is the more human friendly form of diffLines and diffLines is better suited for patches and other computer friendly output.
Returns a list of change objects (See below).
-
JsDiff.diffTrimmedLines(oldStr, newStr[, options]) - diffs two blocks of text, comparing line by line, ignoring leading and trailing whitespace.
Many of the methods above return change objects. These objects consist of the following fields:
Note that some cases may omit a particular flag field. Comparison on the flag fields should always be done in a truthy or falsy manner.
require('colors');
var jsdiff = require('diff');
var one = 'beep boop';
var other = 'beep boob blah';
var diff = jsdiff.diffChars(one, other);
diff.forEach(function(part){
// green for additions, red for deletions
// grey for common parts
var color = part.added ? 'green' :
part.removed ? 'red' : 'grey';
process.stderr.write(part.value[color]);
});
console.log
<pre id="display"></pre>
<script src="diff.js"></script>
<script>
var one = 'beep boop',
other = 'beep boob blah',
color = '',
span = null;
var diff = JsDiff.diffChars(one, other),
display = document.getElementById('display'),
fragment = document.createDocumentFragment();
diff.forEach(function(part){
jsdiff supports all ES3 environments with some known issues on IE8 and below. Under these browsers some diff algorithms such as word diff and others may fail due to lack of support for capturing groups in the split operation.