Move Base.equals() to Base extension, where it belongs.

This commit is contained in:
Jürg Lehni 2012-11-06 07:19:04 -08:00
parent 1fad063a30
commit b354e01f49
2 changed files with 43 additions and 43 deletions

43
lib/bootstrap.js vendored
View file

@ -229,48 +229,6 @@ var Base = new function() { // Bootstrap scope
}, new obj.constructor());
}
/**
* Checks if two values or objects are equals to each other, by using their
* 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;
for (var i = 0, l = obj1.length; i < l; i++) {
if (!Base.equals(obj1, obj2))
return false;
}
return true;
}
// Compare objects
if (typeof obj1 === 'object' && typeof obj2 === 'object') {
function checkKeys(o1, o2) {
for (var i in o1)
if (typeof o2[i] === 'undefined')
return false;
return true;
}
if (!checkKeys(obj1, obj2) || !checkKeys(obj2, obj1))
return false;
for (var i in obj1) {
if (obj1.hasOwnProperty(i) && !Base.equals(obj1[i], obj2[i]))
return false;
}
return true;
}
return false;
}
// Inject into new ctor object that's passed to inject(), and then returned
return inject(function() {}, {
inject: function(src/*, ... */) {
@ -364,7 +322,6 @@ var Base = new function() { // Bootstrap scope
// Expose some local privates as Base generics.
each: each,
clone: clone,
equals: equals,
// Base.create does something different from Object.create, it only
// works on constructors and uses their prototype.
create: function(ctor) {

View file

@ -51,6 +51,49 @@ this.Base = Base.inject(/** @lends Base# */{
},
statics: /** @lends Base */{
/**
* Checks if two values or objects are equals to each other, by using their
* equals() methods if available, and also comparing elements of arrays
* and properties of objects.
*/
equals: function(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 (Array.isArray(obj1) && Array.isArray(obj2)) {
if (obj1.length !== obj2.length)
return false;
for (var i = 0, l = obj1.length; i < l; i++) {
if (!Base.equals(obj1, obj2))
return false;
}
return true;
}
// Compare objects
if (typeof obj1 === 'object' && typeof obj2 === 'object') {
function checkKeys(o1, o2) {
for (var i in o1)
if (o1.hasOwnProperty(i) && typeof o2[i] === 'undefined')
return false;
return true;
}
if (!checkKeys(obj1, obj2) || !checkKeys(obj2, obj1))
return false;
for (var i in obj1) {
if (obj1.hasOwnProperty(i) && !Base.equals(obj1[i], obj2[i]))
return false;
}
return true;
}
return false;
},
/**
* Reads arguments of the type of the class on which it is called on
* from the passed arguments list or array, at the given index, up to