diff-replay

Diff implementation with diff application and reversion functions. Save bandwidth by calculating the diff of a document on the client and sending the diff to the server instead of the entire document. The server can construct the new document from the old document by applying the diff.

npm install diff-replay
2 downloads in the last week
10 downloads in the last month

diff-replay

Javascript diff implementation with diff application and reversion functions. Save bandwidth by calculating the diff of a document on the client and sending the diff to the server instead of the entire document. The server can construct the new document from the old document by applying the diff.

Applying or reversing a diff will check for success by comparing input and output against an MD5 hash calculated when creating the diff. Ensures application or reversion of a diff is executed on exactly the same document, preventing information loss.

Installation and usage

Install the package with the following command. Optionally specify the --save parameter to include diff-replay as a dependency in package.json.

npm install diff-replay

Usage for server and client-side javascript are the same. Just make sure you Browserify your client-side code.

var diffReplay = require("diff-replay");

var diffresult = diffReplay.diffChars("old text", "new text");
var newText = diffReplay.applyDiff("old text", diffresult);

In the code sample applyDiff() will throw an exception if the MD5 hash of "old text" does not match diffresult.oldStringMD5Hash.

Available functions

Use the functions 'diffChars(oldString, newString)', 'diffWords(oldString, newString)' and 'diffLines(oldString, newString)' to get the diff of the desired level of detail.

Use the function 'applyDiff(oldString, diff)' to get the newString. Use the function 'reverseDiff(newString, diff)' to get the oldString.

You can round-trip applyDiff and reverseDiff, like so:

var diffresult = diffReplay.diffChars("oldString", "newString");
var newString = diffReplay.applyDiff("oldString", diffresult);
var oldString = diffReplay.reverseDiff(newString, diffresult);
assert.equal(oldString, "oldString");
npm loves you