Skip to content

Instantly share code, notes, and snippets.

@trodrigues
Created December 5, 2014 13:45
Show Gist options
  • Save trodrigues/4aead044ab1a81f1cf02 to your computer and use it in GitHub Desktop.
Save trodrigues/4aead044ab1a81f1cf02 to your computer and use it in GitHub Desktop.

Revisions

  1. trodrigues created this gist Dec 5, 2014.
    36 changes: 36 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    // use with https://www.npmjs.org/package/deep-diff

    toEqualObj: function () {
    var KINDS = {
    'N': 'newly added property/element',
    'D': 'property/element was deleted',
    'E': 'property/element was edited',
    'A': 'change occurred within an array'
    };

    function formatDiff(objdiff) {
    return objdiff.map(function (diff) {
    return '\t'+diff.kind +' at '+ diff.path.join('.') +'\n'+
    '\t'+diff.actual +' should be '+ diff.expected;
    }).join('\n\n');
    }

    return {
    compare: function (actual, expected) {
    var objdiff = (deepDiff(expected, actual) || []).map(function (diff) {
    return {
    kind: KINDS[diff.kind],
    path: diff.path,
    expected: diff.lhs,
    actual: diff.rhs
    };
    });

    return {
    pass : objdiff.length === 0,
    message : 'Expected object not to have differences\n\n' + formatDiff(objdiff)
    };

    }
    };
    }