Last active
April 28, 2018 17:40
-
-
Save jamesplease/9fc416b6ebd3b2c9fb499cee32d6577d to your computer and use it in GitHub Desktop.
Serializable is-equal
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
export default function isEqual(x, y) { | |
// Handles primitives and exact object matches | |
if (x === y) { | |
return true; | |
} | |
// We can only handle comparing "regular" objects and | |
// arrays; everything else is considered not equal. | |
else if ( | |
((x && x.constructor === Object) || Array.isArray(x)) && | |
((y && y.constructor === Object) || Array.isArray(y)) | |
) { | |
// If there aren't the same number of keys, then they are not equal. | |
if (Object.keys(x).length !== Object.keys(y).length) { | |
return false; | |
} | |
// If there are the same number of keys, then we just need to iterate | |
// a single time to compare each key. | |
for (var prop in x) { | |
if (y.hasOwnProperty(prop)) { | |
if (!isEqual(x[prop], y[prop])) { | |
return false; | |
} | |
} else { | |
return false; | |
} | |
} | |
return true; | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment