js-diff
v0.0.1
Published
A recursive JavaScript object comparison script
Downloads
205
Readme
js-diff
A basic JavaScript object comparison script that will iterate, recursivly, through two JSON objects to compare differences.
This was built for a project in which I had a JSON object of user data that I cached on page load and compared against an edited version on save. I used this information to construct email summaries of changed behaviour as well as provide a detailed audit trail of actions.
Code
Code can be found under src
at src/diff.js. That's all you need to worry about and the code has no dependencies on any library. NodeJS and all the other files are purely for running tests.
Params
obj1
and obj2
are two objects to be compared.
Return
Returns false
if no differences are found.
Returns an object
if differences are found. The object will maintain path structure where the value fot he changed key/value pair is highlighted by from and to fields.
Example
var x = { name: 'omgaz', location: 'London' };
var y = { name: 'omgaz', location: 'Melbourne' };
getDiff(x, y);
// Returns object
{
location: {
from: 'London',
to: 'Melbourne'
}
}
Tests
If you'd like to run tests, check out the whole project. You'll need NodeJS installed. Tests use Karma and PhantomJS.
npm install
npm test