Further improve Base.equals().

This commit is contained in:
Jürg Lehni 2012-11-05 11:15:41 -08:00
parent 0a537f0712
commit 43a7e6cfcc

11
lib/bootstrap.js vendored
View file

@ -231,13 +231,18 @@ var Base = new function() { // Bootstrap scope
/**
* Checks if two values or objects are equals to each other, by using their
* equals() methods if available, and also comparing elements of arrays.
* equals() methods if available, and also comparing elements of arrays
* and properties of objects.
*/
function equals(obj1, obj2) {
if (obj1 == obj2)
return true;
// Call #equals() on both obj1 and obj2
if (obj1 != null && obj1.equals)
return obj1.equals(obj2);
if (obj2 != null && obj2.equals)
return obj2.equals(obj1);
// Compare arrays
if (isArray(obj1) && isArray(obj2)) {
if (obj1.length !== obj2.length)
return false;
@ -247,6 +252,7 @@ var Base = new function() { // Bootstrap scope
}
return true;
}
// Compare objects
if (typeof obj1 === 'object' && typeof obj2 === 'object') {
function checkKeys(o1, o2) {
for (var i in o1)
@ -256,9 +262,10 @@ var Base = new function() { // Bootstrap scope
}
if (!checkKeys(obj1, obj2) || !checkKeys(obj2, obj1))
return false;
for (var i in obj1)
for (var i in obj1) {
if (obj1.hasOwnProperty(i) && !Base.equals(obj1[i], obj2[i]))
return false;
}
return true;
}
return false;