Last active
December 20, 2015 17:49
-
-
Save tarqd/6171014 to your computer and use it in GitHub Desktop.
Compare objects
This file contains 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
var toString = Object.protototype.toString | |
function objectEquals(x, y) { | |
// anything that's strictly equal is good | |
if(x === y) return true; | |
// at this point any falsy values will not be equal | |
if(!x || !y) return false; | |
// valueOf should work for dates | |
if (x.valueOf() === y.valueOf()) { return true; } | |
// if one of them is date, they must had equal valueOf | |
if (x instanceof Date) { return false; } | |
if (y instanceof Date) { return false; } | |
// if they are not function or strictly equal, they both need to be Objects | |
if (!(x instanceof Object)) { return false; } | |
if (!(y instanceof Object)) { return false; } | |
var p = Object.keys(x); | |
return Object.keys(y).every(function (i) { return p.indexOf(i) !== -1; }) ? | |
p.every(function (i) { return objectEquals(x[i], y[i]); }) : false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment