Skip to content

Instantly share code, notes, and snippets.

@jbrantly
Created May 10, 2010 14:07
Show Gist options
  • Save jbrantly/396073 to your computer and use it in GitHub Desktop.
Save jbrantly/396073 to your computer and use it in GitHub Desktop.
var hasOwnProperty = Object.prototype.hasOwnProperty;
var equivalent = function(actual, expected) {
if (actual === expected) {
return true;
}
else if (expected instanceof Date) {
if (actual instanceof Date && actual.getTime() == expected.getTime()) {
return true;
}
else {
return false;
}
}
else if (typeof actual != "object" && typeof expected != "object") {
return actual == expected;
}
else {
if (actual.prototype !== expected.prototype) {
return false;
}
var actualLength = 0, expectedLength = 0;
for (var prop in actual) {
if (hasOwnProperty.call(actual, prop)) {
actualLength++;
if (!hasOwnProperty.call(expected, prop) || !equivalent(actual[prop], expected[prop])) {
return false;
}
}
}
for (var prop in expected) {
if (hasOwnProperty.call(expected, prop)) {
expectedLength++;
}
}
return actualLength == expectedLength;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment