Last active
September 7, 2016 15:57
-
-
Save ardeshireshghi/8fa1b29ca43adb40382db0fda9184495 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.isEqual = function(a, b) { | |
if (a === b) { | |
return true; | |
} | |
// We do not expect string arguments | |
if (typeof a === 'string' || typeof b === 'string') { | |
return false; | |
} | |
// We need array like arguments (e.g. NodeList) | |
// That is why we don't check for explicitly Array type | |
if ((a && !a.length) || (b && !b.length) || a.length !== b.length) { | |
return false; | |
} | |
a = this.prototype.slice.call(a); | |
b = this.prototype.slice.call(b); | |
var i = -1; | |
while (++i < a.length) { | |
var currentItem = a[i]; | |
var haveEqualItemCount = function() { | |
var compareFilter = function() { | |
if (typeof item !== 'string' && item.length | |
&& typeof currentItem !== 'string' && currentItem.length) { | |
return this.isEqual(item, currentItem); | |
} | |
return item === currentItem; | |
}; | |
return (a.filter(compareFilter).length === b.filter(compareFilter).length); | |
}; | |
// Current item found (works for string, numbers and objects by reference) | |
if (b.indexOf(currentItem) > -1) { | |
continue; | |
} | |
// Otherwise if currentItem can not be found using indexOf | |
// We have a different object or we need to compare arrays | |
for (var j = 0; j < b.length; j++) { | |
// When both are array like (e.g Nodelist, arguments etc.) | |
if (typeof b[j] !== 'string' && b[j].length | |
&& typeof currentItem !== 'string' && currentItem.length) { | |
if (!this.isEqual(b[j], currentItem)) return false; | |
} | |
} | |
if (!haveEqualItemCount()) return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment