testling - automated browser tests
browserling - interactive browser testing
commit 120b5e51c5e83760e83865eb7aea7a53ca937605
Author: James Halliday
Date: Wed Feb 22 14:30:32 2012 +0000

Introducing difflet, a handy node.js module for computing pretty object diffs!

Just plug in some initial options and the objects you want to compare!

var difflet = require('difflet');
var s = difflet({ indent : 2, comment : true }).compare(
    { z : [6,7], a : 'abcdefgh', b : [ 31, 'xxt' ] },
    { x : 5, a : 'abdcefg', b : [ 51, 'xxs' ] }
);
console.log(s);

and you'll get a colored and annotated object expressing the differences between the 2 objects you passed in:

Deleted elements between the first and second object are shown in red and commented if comments are turned on. New object show up in green and updated objects show in blue. The comments show what the previous value was, if any.

You can set a bunch of options to adjust the formatting including comma-first output. You can even generate HTML output:

var difflet = require('difflet');
var ent = require('ent');

var tags = {
    inserted : '<span class="g">',
    updated : '<span class="b">',
    deleted : '<span class="r">',
};
var diff = difflet({
    start : function (t, s) {
        s.write(tags[t]);
    },
    stop : function (t, s) {
        s.write('</span>');
    },
    write : function (buf, s) {
        s.write(ent.encode(buf))
    },
});

var prev = {
    yy : 6,
    zz : 5,
    a : [1,2,3],
    fn : function qq () {}
};
var next = {
    a : [ 1, 2, 3, [4], "z", /beep/, new Buffer([0,1,2]) ],
    fn : 'I <3 robots',
    b : [5,6,7]
};
diff(prev, next).pipe(process.stdout);

which generates some HTML output that you can stuff into a browser:

$ node example/html.js 
{"a":[1,2,3,<span class="g">[4]</span>,<span class="g">"z"</span>,<span class="g">/beep/</span>,<span class="g"><Buffer 00 01 02></span>],"fn":<span class="b">"I <3 robots"</span>,<span class="g">"b":[5,6,7]</span>,<span class="r">"yy":6,"zz":5</span>}

Plus, pkrumins and I just rolled out difflet output for testling to make debugging t.deepEqual() statements on big objects easier:

node-tap is now using difflet too in 0.2.1!

Check out the code on github or with npm do:

npm install difflet
more
git clone http://substack.net/blog.git