\href{http://travis-ci.org/kpdecker/jsdiff}{\texttt{ }} \href{https://saucelabs.com/u/jsdiff}{\texttt{ }}

A javascript text differencing implementation.

Based on the algorithm proposed in \href{http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927}{\texttt{ \char`\"{}\+An O(\+ND) Difference Algorithm and its Variations\char`\"{} (Myers, 1986)}}.\hypertarget{md_sfcode_node_modules_diff_README_autotoc_md8148}{}\doxysection{Installation}\label{md_sfcode_node_modules_diff_README_autotoc_md8148}

\begin{DoxyCode}{0}
\DoxyCodeLine{npm install diff -\/-\/save }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_diff_README_autotoc_md8149}{}\doxysection{API}\label{md_sfcode_node_modules_diff_README_autotoc_md8149}

\begin{DoxyItemize}
\item {\ttfamily Js\+Diff.\+diff\+Chars(old\+Str, new\+Str\mbox{[}, options\mbox{]})} -\/ diffs two blocks of text, comparing character by character.

Returns a list of change objects (See below).

Options
\begin{DoxyItemize}
\item {\ttfamily ignore\+Case}\+: {\ttfamily true} to ignore casing difference. Defaults to {\ttfamily false}.
\end{DoxyItemize}
\item {\ttfamily Js\+Diff.\+diff\+Words(old\+Str, new\+Str\mbox{[}, options\mbox{]})} -\/ diffs two blocks of text, comparing word by word, ignoring whitespace.

Returns a list of change objects (See below).

Options
\begin{DoxyItemize}
\item {\ttfamily ignore\+Case}\+: Same as in {\ttfamily diff\+Chars}.
\end{DoxyItemize}
\item {\ttfamily Js\+Diff.\+diff\+Words\+With\+Space(old\+Str, new\+Str\mbox{[}, options\mbox{]})} -\/ diffs two blocks of text, comparing word by word, treating whitespace as significant.

Returns a list of change objects (See below).
\item {\ttfamily Js\+Diff.\+diff\+Lines(old\+Str, new\+Str\mbox{[}, options\mbox{]})} -\/ diffs two blocks of text, comparing line by line.

Options
\begin{DoxyItemize}
\item {\ttfamily ignore\+Whitespace}\+: {\ttfamily true} to ignore leading and trailing whitespace. This is the same as {\ttfamily diff\+Trimmed\+Lines}
\item {\ttfamily newline\+Is\+Token}\+: {\ttfamily 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 {\ttfamily diff\+Lines} and {\ttfamily diff\+Lines} is better suited for patches and other computer friendly output.
\end{DoxyItemize}

Returns a list of change objects (See below).
\item {\ttfamily Js\+Diff.\+diff\+Trimmed\+Lines(old\+Str, new\+Str\mbox{[}, options\mbox{]})} -\/ diffs two blocks of text, comparing line by line, ignoring leading and trailing whitespace.

Returns a list of change objects (See below).
\item {\ttfamily Js\+Diff.\+diff\+Sentences(old\+Str, new\+Str\mbox{[}, options\mbox{]})} -\/ diffs two blocks of text, comparing sentence by sentence.

Returns a list of change objects (See below).
\item {\ttfamily Js\+Diff.\+diff\+Css(old\+Str, new\+Str\mbox{[}, options\mbox{]})} -\/ diffs two blocks of text, comparing CSS tokens.

Returns a list of change objects (See below).
\item {\ttfamily Js\+Diff.\+diff\+Json(old\+Obj, new\+Obj\mbox{[}, options\mbox{]})} -\/ diffs two JSON objects, comparing the fields defined on each. The order of fields, etc does not matter in this comparison.

Returns a list of change objects (See below).
\item {\ttfamily Js\+Diff.\+diff\+Arrays(old\+Arr, new\+Arr\mbox{[}, options\mbox{]})} -\/ diffs two arrays, comparing each item for strict equality (===).

Options
\begin{DoxyItemize}
\item {\ttfamily comparator}\+: {\ttfamily function(left, right)} for custom equality checks
\end{DoxyItemize}

Returns a list of change objects (See below).
\item {\ttfamily Js\+Diff.\+create\+Two\+Files\+Patch(old\+File\+Name, new\+File\+Name, old\+Str, new\+Str, old\+Header, new\+Header)} -\/ creates a unified diff patch.

Parameters\+:
\begin{DoxyItemize}
\item {\ttfamily old\+File\+Name} \+: String to be output in the filename section of the patch for the removals
\item {\ttfamily new\+File\+Name} \+: String to be output in the filename section of the patch for the additions
\item {\ttfamily old\+Str} \+: Original string value
\item {\ttfamily new\+Str} \+: New string value
\item {\ttfamily old\+Header} \+: Additional information to include in the old file header
\item {\ttfamily new\+Header} \+: Additional information to include in the new file header
\item {\ttfamily options} \+: An object with options. Currently, only {\ttfamily context} is supported and describes how many lines of context should be included.
\end{DoxyItemize}
\item {\ttfamily Js\+Diff.\+create\+Patch(file\+Name, old\+Str, new\+Str, old\+Header, new\+Header)} -\/ creates a unified diff patch.

Just like Js\+Diff.\+create\+Two\+Files\+Patch, but with old\+File\+Name being equal to new\+File\+Name.
\item {\ttfamily Js\+Diff.\+structured\+Patch(old\+File\+Name, new\+File\+Name, old\+Str, new\+Str, old\+Header, new\+Header, options)} -\/ returns an object with an array of hunk objects.

This method is similar to create\+Two\+Files\+Patch, but returns a data structure suitable for further processing. Parameters are the same as create\+Two\+Files\+Patch. The data structure returned may look like this\+:

\`{}\`{}\`{}js \{ old\+File\+Name\+: \textquotesingle{}oldfile\textquotesingle{}, new\+File\+Name\+: \textquotesingle{}newfile\textquotesingle{}, old\+Header\+: \textquotesingle{}header1\textquotesingle{}, new\+Header\+: \textquotesingle{}header2\textquotesingle{}, hunks\+: \mbox{[}\{ old\+Start\+: 1, old\+Lines\+: 3, new\+Start\+: 1, new\+Lines\+: 3, lines\+: \mbox{[}\textquotesingle{} line2\textquotesingle{}, \textquotesingle{} line3\textquotesingle{}, \textquotesingle{}-\/line4\textquotesingle{}, \textquotesingle{}+line5\textquotesingle{}, \textquotesingle{}\textbackslash{} No newline at end of file\textquotesingle{}\mbox{]}, \}\mbox{]} \} \`{}\`{}\`{}
\item {\ttfamily Js\+Diff.\+apply\+Patch(source, patch\mbox{[}, options\mbox{]})} -\/ applies a unified diff patch.

Return a string containing new version of provided data. {\ttfamily patch} may be a string diff or the output from the {\ttfamily parse\+Patch} or {\ttfamily structured\+Patch} methods.

The optional {\ttfamily options} object may have the following keys\+:
\begin{DoxyItemize}
\item {\ttfamily fuzz\+Factor}\+: Number of lines that are allowed to differ before rejecting a patch. Defaults to 0.
\item {\ttfamily compare\+Line(line\+Number, line, operation, patch\+Content)}\+: 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.
\end{DoxyItemize}
\item {\ttfamily Js\+Diff.\+apply\+Patches(patch, options)} -\/ applies one or more patches.

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\+:
\begin{DoxyItemize}
\item {\ttfamily options.\+load\+File(index, callback)} is called. The caller should then load the contents of the file and then pass that to the {\ttfamily callback(err, data)} callback. Passing an {\ttfamily err} will terminate further patch execution.
\item {\ttfamily options.\+patched(index, content, callback)} is called once the patch has been applied. {\ttfamily content} will be the return value from {\ttfamily apply\+Patch}. When it\textquotesingle{}s ready, the caller should call {\ttfamily callback(err)} callback. Passing an {\ttfamily err} will terminate further patch execution.
\end{DoxyItemize}

Once all patches have been applied or an error occurs, the {\ttfamily options.\+complete(err)} callback is made.
\item {\ttfamily Js\+Diff.\+parse\+Patch(diff\+Str)} -\/ Parses a patch into structured data

Return a JSON object representation of the a patch, suitable for use with the {\ttfamily apply\+Patch} method. This parses to the same structure returned by {\ttfamily Js\+Diff.\+structured\+Patch}.
\item {\ttfamily convert\+Changes\+To\+XML(changes)} -\/ converts a list of changes to a serialized XML format
\end{DoxyItemize}

All methods above which accept the optional {\ttfamily callback} method will run in sync mode when that parameter is omitted and in async mode when supplied. This allows for larger diffs without blocking the event loop. This may be passed either directly as the final parameter or as the {\ttfamily callback} field in the {\ttfamily options} object.\hypertarget{md_sfcode_node_modules_diff_README_autotoc_md8150}{}\doxysubsection{Change Objects}\label{md_sfcode_node_modules_diff_README_autotoc_md8150}
Many of the methods above return change objects. These objects consist of the following fields\+:


\begin{DoxyItemize}
\item {\ttfamily value}\+: Text content
\item {\ttfamily added}\+: True if the value was inserted into the new string
\item {\ttfamily removed}\+: True if the value was removed from the old string
\end{DoxyItemize}

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.\hypertarget{md_sfcode_node_modules_diff_README_autotoc_md8151}{}\doxysection{Examples}\label{md_sfcode_node_modules_diff_README_autotoc_md8151}
Basic example in Node


\begin{DoxyCode}{0}
\DoxyCodeLine{require('colors');}
\DoxyCodeLine{var jsdiff = require('diff');}
\DoxyCodeLine{}
\DoxyCodeLine{var one = 'beep boop';}
\DoxyCodeLine{var other = 'beep boob blah';}
\DoxyCodeLine{}
\DoxyCodeLine{var diff = jsdiff.diffChars(one, other);}
\DoxyCodeLine{}
\DoxyCodeLine{diff.forEach(function(part)\{}
\DoxyCodeLine{  // green for additions, red for deletions}
\DoxyCodeLine{  // grey for common parts}
\DoxyCodeLine{  var color = part.added ? 'green' :}
\DoxyCodeLine{    part.removed ? 'red' : 'grey';}
\DoxyCodeLine{  process.stderr.write(part.value[color]);}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{console.log(); }

\end{DoxyCode}
 Running the above program should yield



Basic example in a web page


\begin{DoxyCode}{0}
\DoxyCodeLine{<pre id="{}display"{}></pre>}
\DoxyCodeLine{<script src="{}diff.js"{}></script>}
\DoxyCodeLine{<script>}
\DoxyCodeLine{var one = 'beep boop',}
\DoxyCodeLine{    other = 'beep boob blah',}
\DoxyCodeLine{    color = '',}
\DoxyCodeLine{    span = null;}
\DoxyCodeLine{}
\DoxyCodeLine{var diff = JsDiff.diffChars(one, other),}
\DoxyCodeLine{    display = document.getElementById('display'),}
\DoxyCodeLine{    fragment = document.createDocumentFragment();}
\DoxyCodeLine{}
\DoxyCodeLine{diff.forEach(function(part)\{}
\DoxyCodeLine{  // green for additions, red for deletions}
\DoxyCodeLine{  // grey for common parts}
\DoxyCodeLine{  color = part.added ? 'green' :}
\DoxyCodeLine{    part.removed ? 'red' : 'grey';}
\DoxyCodeLine{  span = document.createElement('span');}
\DoxyCodeLine{  span.style.color = color;}
\DoxyCodeLine{  span.appendChild(document}
\DoxyCodeLine{    .createTextNode(part.value));}
\DoxyCodeLine{  fragment.appendChild(span);}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{display.appendChild(fragment);}
\DoxyCodeLine{</script> }

\end{DoxyCode}


Open the above .html file in a browser and you should see



{\bfseries{\href{http://kpdecker.github.com/jsdiff}{\texttt{ Full online demo}}}}\hypertarget{md_sfcode_node_modules_diff_README_autotoc_md8152}{}\doxysection{Compatibility}\label{md_sfcode_node_modules_diff_README_autotoc_md8152}
\href{https://saucelabs.com/u/jsdiff}{\texttt{ }}

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 {\ttfamily split} operation.\hypertarget{md_sfcode_node_modules_diff_README_autotoc_md8153}{}\doxysection{License}\label{md_sfcode_node_modules_diff_README_autotoc_md8153}
See \href{https://github.com/kpdecker/jsdiff/blob/master/LICENSE}{\texttt{ LICENSE}}. 