Created
February 15, 2016 20:16
-
-
Save MehulATL/68ca24bb92dea5d8cac6 to your computer and use it in GitHub Desktop.
compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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