Created
July 18, 2015 09:52
-
-
Save l0co/e029063b91c2a78c3889 to your computer and use it in GitHub Desktop.
Angular hard limit recursion in equals()
This file contains hidden or 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
var currentEqualsLevel = 0; | |
var maxEqualsLevel = 10; | |
function equals(o1, o2) { | |
if (currentEqualsLevel>=maxEqualsLevel) | |
return true; // we consider objects equal to this level | |
if (o1 === o2) return true; | |
if (o1 === null || o2 === null) return false; | |
if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN | |
var t1 = typeof o1, t2 = typeof o2, length, key, keySet; | |
if (t1 == t2) { | |
if (t1 == 'object') { | |
if (isArray(o1)) { | |
if (!isArray(o2)) return false; | |
if ((length = o1.length) == o2.length) { | |
for (key = 0; key < length; key++) { | |
try { | |
currentEqualsLevel++; | |
if (!equals(o1[key], o2[key])) return false; | |
} finally { | |
currentEqualsLevel--; | |
} | |
} | |
return true; | |
} | |
} else if (isDate(o1)) { | |
if (!isDate(o2)) return false; | |
try { | |
currentEqualsLevel++; | |
return equals(o1.getTime(), o2.getTime()); | |
} finally { | |
currentEqualsLevel--; | |
} | |
} else if (isRegExp(o1)) { | |
return isRegExp(o2) ? o1.toString() == o2.toString() : false; | |
} else { | |
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2) || | |
isArray(o2) || isDate(o2) || isRegExp(o2)) return false; | |
keySet = {}; | |
for (key in o1) { | |
if (key.charAt(0) === '$' || isFunction(o1[key])) continue; | |
try { | |
currentEqualsLevel++; | |
if (!equals(o1[key], o2[key])) return false; | |
} finally { | |
currentEqualsLevel--; | |
} | |
keySet[key] = true; | |
} | |
for (key in o2) { | |
if (!keySet.hasOwnProperty(key) && | |
key.charAt(0) !== '$' && | |
o2[key] !== undefined && | |
!isFunction(o2[key])) return false; | |
} | |
return true; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment