Skip to content

Instantly share code, notes, and snippets.

@MehulATL
Created February 15, 2016 20:16
Show Gist options
  • Save MehulATL/68ca24bb92dea5d8cac6 to your computer and use it in GitHub Desktop.
Save MehulATL/68ca24bb92dea5d8cac6 to your computer and use it in GitHub Desktop.
compare
function compare(expected, actual) {
if (typeof(actual) !== typeof(expected)) {
return false;
}
if (typeof(expected) !== 'object' || expected === null) {
return expected === actual;
}
if (!!expected && !actual) {
return false;
}
if (Array.isArray(expected)) {
if (typeof(actual.length) !== 'number') {
return false;
}
var aa = Array.prototype.slice.call(actual);
return expected.every(function (exp) {
return aa.some(function (act) {
return compare(exp, act);
});
});
}
if(expected instanceof Date && actual instanceof Date) {
return expected.getTime() === actual.getTime();
}
return Object.keys(expected).every(function (key) {
var eo = expected[key];
var ao = actual[key];
if (typeof(eo) === 'object' && eo !== null && ao !== null) {
return compare(eo, ao);
}
return ao === eo;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment