mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-19 14:10:14 -05:00
Further improve Base.equals().
This commit is contained in:
parent
0a537f0712
commit
43a7e6cfcc
1 changed files with 9 additions and 2 deletions
11
lib/bootstrap.js
vendored
11
lib/bootstrap.js
vendored
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue