1.0.0 • Published 11 years ago

diff-replay v1.0.0

Weekly downloads
3
License
-
Repository
github
Last release
11 years ago

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.

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

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