Skip to content

Instantly share code, notes, and snippets.

@jamesplease
Last active April 28, 2018 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesplease/9fc416b6ebd3b2c9fb499cee32d6577d to your computer and use it in GitHub Desktop.
Save jamesplease/9fc416b6ebd3b2c9fb499cee32d6577d to your computer and use it in GitHub Desktop.
Serializable is-equal
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