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.

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.

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 diffs = diffReplay.diffChars("old text", "new text");
var newText = diffReplay.applyDiff("old text", diffs);

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 diff = diffReplay.diffChars("oldString", "newString");
var newString = diffReplay.applyDiff("oldString", diff);
var oldString = diffReplay.reverseDiff(newString, diff);
assert.equal(oldString, "oldString");