Skip to content

Commit 5696869

Browse files
committed
making the equals method work when the obj graph has circular references. This uses WeakMap.
1 parent ea5dea4 commit 5696869

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/Angular.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -825,24 +825,31 @@ function shallowCopy(src, dst) {
825825
* @param {*} o2 Object or value to compare.
826826
* @returns {boolean} True if arguments are equal.
827827
*/
828-
function equals(o1, o2, visitedObjArr) {
828+
function equals(o1, o2, comparisons) {
829829
if (o1 === o2) return true;
830830
if (o1 === null || o2 === null) return false;
831831
if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN
832832
var t1 = typeof o1, t2 = typeof o2, length, key, keySet;
833833
if (t1 == t2) {
834834
if (t1 == 'object') {
835-
if (typeof visitedObjArr == 'undefined') {
836-
visitedObjArr = {};
835+
comparisons || (comparisons = new WeakMap());
836+
if (comparisons.has(o1) && comparisons.get(o1).has(o2)) {
837+
return true;
837838
} else {
838-
if (o1 in visitedObjArr) return true;
839+
if (!comparisons.has(o1)) {
840+
comparisons.set(o1, new WeakMap());
841+
}
842+
comparisons.get(o1).set(o2, true);
843+
if (!comparisons.has(o2)) {
844+
comparisons.set(o2, new WeakMap());
845+
}
846+
comparisons.get(o2).set(o1, true);
839847
}
840-
visitedObjArr[o1] = true;
841848
if (isArray(o1)) {
842849
if (!isArray(o2)) return false;
843850
if ((length = o1.length) == o2.length) {
844851
for (key=0; key<length; key++) {
845-
if (!equals(o1[key], o2[key], visitedObjArr)) return false;
852+
if (!equals(o1[key], o2[key], comparisons)) return false;
846853
}
847854
return true;
848855
}
@@ -856,7 +863,7 @@ function equals(o1, o2, visitedObjArr) {
856863
keySet = {};
857864
for (key in o1) {
858865
if (key.charAt(0) === '$' || isFunction(o1[key])) continue;
859-
if (!equals(o1[key], o2[key], visitedObjArr)) return false;
866+
if (!equals(o1[key], o2[key], comparisons)) return false;
860867
keySet[key] = true;
861868
}
862869
for (key in o2) {

0 commit comments

Comments
 (0)