Broadly, jsdiff's diff functions all take an old text and a new text and perform three steps:
-
Diff.diffChars(oldStr, newStr[, options])
- diffs two blocks of text, treating each character as a token.
Returns a list of change objects.
Options
ignoreCase
: If true
, the uppercase and lowercase forms of a character are considered equal. Defaults to false
.
-
Diff.diffWords(oldStr, newStr[, options])
- diffs two blocks of text, treating each word and each word separator (punctuation, newline, or run of whitespace) as a token.
(Whitespace-only tokens are automatically treated as equal to each other, so changes like changing a space to a newline or a run of multiple spaces will be ignored.)
Returns a list of change objects.
Options
ignoreCase
: Same as in diffChars
. Defaults to false.
-
Diff.diffWordsWithSpace(oldStr, newStr[, options])
- same as diffWords
, except whitespace-only tokens are not automatically considered equal, so e.g. changing a space to a tab is considered a change.
-
Diff.diffLines(oldStr, newStr[, options])
- diffs two blocks of text, treating each line as a token.
Options
ignoreWhitespace
: true
to strip all leading and trailing whitespace characters from each line before performing the diff. Defaults to false
.stripTrailingCr
: true
to remove all trailing CR (\r
) characters before performing the diff. Defaults to false
.
This helps to get a useful diff when diffing UNIX text files against Windows text files.newlineIsToken
: true
to treat the newline character at the end of each line as its own token. 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
; the default behavior with this option turned off is better suited for patches and other computer friendly output. Defaults to false
.
Returns a list of change objects.
-
Diff.diffTrimmedLines(oldStr, newStr[, options])
- diffs two blocks of text, comparing line by line, after stripping leading and trailing whitespace. Equivalent to calling diffLines
with ignoreWhitespace: true
.
Options
stripTrailingCr
: Same as in diffLines
. Defaults to false
.newlineIsToken
: Same as in diffLines
. Defaults to false
.
Returns a list of change objects.
-
Diff.diffSentences(oldStr, newStr[, options])
- diffs two blocks of text, treating each sentence as a token.
Returns a list of change objects.
-
Diff.diffCss(oldStr, newStr[, options])
- diffs two blocks of text, comparing CSS tokens.
Returns a list of change objects.
-
Diff.diffJson(oldObj, newObj[, options])
- diffs two JSON-serializable objects by first serializing them to prettily-formatted JSON and then treating each line of the JSON as a token. Object properties are ordered alphabetically in the serialized JSON, so the order of properties in the objects being compared doesn't affect the result.
Returns a list of change objects.
Options
stringifyReplacer
: A custom replacer function. Operates similarly to the replacer
parameter to JSON.stringify()
, but must be a function.undefinedReplacement
: A value to replace undefined
with. Ignored if a stringifyReplacer
is provided.
-
Diff.diffArrays(oldArr, newArr[, options])
- diffs two arrays of tokens, comparing each item for strict equality (===).
Options
comparator
: function(left, right)
for custom equality checks
Returns a list of change objects.
-
Diff.createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr[, oldHeader[, newHeader[, options]]])
- creates a unified diff patch by first computing a diff with diffLines
and then serializing it to unified diff format.
Parameters:
oldFileName
: String to be output in the filename section of the patch for the removalsnewFileName
: String to be output in the filename section of the patch for the additionsoldStr
: Original string valuenewStr
: New string valueoldHeader
: Optional additional information to include in the old file header. Default: undefined
.newHeader
: Optional additional information to include in the new file header. Default: undefined
.options
: An object with options.
context
describes how many lines of context should be included. You can set this to Number.MAX_SAFE_INTEGER
or Infinity
to include the entire file content in one hunk.ignoreWhitespace
: Same as in diffLines
. Defaults to false
.stripTrailingCr
: Same as in diffLines
. Defaults to false
.newlineIsToken
: Same as in diffLines
. Defaults to false
.
-
Diff.createPatch(fileName, oldStr, newStr[, oldHeader[, newHeader[, options]]])
- creates a unified diff patch.
Just like Diff.createTwoFilesPatch, but with oldFileName being equal to newFileName.
-
Diff.formatPatch(patch)
- creates a unified diff patch.
patch
may be either a single structured patch object (as returned by structuredPatch
) or an array of them (as returned by parsePatch
).
-
Diff.structuredPatch(oldFileName, newFileName, oldStr, newStr[, oldHeader[, newHeader[, options]]])
- returns an object with an array of hunk objects.
This method is similar to createTwoFilesPatch, but returns a data structure
suitable for further processing. Parameters are the same as createTwoFilesPatch. The data structure returned may look like this:
{
oldFileName: 'oldfile', newFileName: 'newfile',
oldHeader: 'header1', newHeader: 'header2',
hunks: [{
oldStart: 1, oldLines: 3, newStart: 1, newLines: 3,
lines: [' line2', ' line3', '-line4', '+line5', '\\ No newline at end of file'],
}]
}
-
Diff.applyPatch(source, patch[, options])
- attempts to apply a unified diff patch.
If the patch was applied successfully, returns a string containing the patched text. If the patch could not be applied (because some hunks in the patch couldn't be fitted to the text in source
), returns false.
patch
may be a string diff or the output from the parsePatch
or structuredPatch
methods.
The optional options
object may have the following keys:
fuzzFactor
: Number of lines that are allowed to differ before rejecting a patch. Defaults to 0.compareLine(lineNumber, line, operation, patchContent)
: Callback used to compare to given lines to determine if they should be considered equal when patching. Defaults to strict equality but may be overridden to provide fuzzier comparison. Should return false if the lines should be rejected.
-
Diff.applyPatches(patch, options)
- applies one or more patches.
patch
may be either an array of structured patch objects, or a string representing a patch in unified diff format (which may patch one or more files).
This method will iterate over the contents of the patch and apply to data provided through callbacks. The general flow for each patch index is:
options.loadFile(index, callback)
is called. The caller should then load the contents of the file and then pass that to the callback(err, data)
callback. Passing an err
will terminate further patch execution.options.patched(index, content, callback)
is called once the patch has been applied. content
will be the return value from applyPatch
. When it's ready, the caller should call callback(err)
callback. Passing an err
will terminate further patch execution.
Once all patches have been applied or an error occurs, the options.complete(err)
callback is made.
-
Diff.parsePatch(diffStr)
- Parses a patch into structured data
Return a JSON object representation of the a patch, suitable for use with the applyPatch
method. This parses to the same structure returned by Diff.structuredPatch
.
-
Diff.reversePatch(patch)
- Returns a new structured patch which when applied will undo the original patch
.
patch
may be either a single structured patch object (as returned by structuredPatch
) or an array of them (as returned by parsePatch
).
-
Diff.convertChangesToXML(changes)
- converts a list of change objects to a serialized XML format
-
Diff.convertChangesToDMP(changes)
- converts a list of change objects to the format returned by Google's diff-match-patch library
If you need behavior a little different to what any of the text diffing functions above offer, you can roll your own by customizing both the tokenization behavior used and the notion of equality used to determine if two tokens are equal.
The simplest way to customize tokenization behavior is to simply tokenize the texts you want to diff yourself, with your own code, then pass the arrays of tokens to diffArrays
. For instance, if you wanted a semantically-aware diff of some code, you could try tokenizing it using a parser specific to the programming language the code is in, then passing the arrays of tokens to diffArrays
.
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.
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.
jsdiff deviates from the published algorithm in a couple of ways that don't affect results but do affect performance: