Fix lodash.isEqual for circular references (#4320) (#4515)

This commit is contained in:
Chinedum Ukejianya
2019-10-16 17:43:11 -04:00
committed by John-David Dalton
parent 94c3a8133c
commit 0cec225778
2 changed files with 28 additions and 10 deletions

View File

@@ -5634,10 +5634,11 @@
if (arrLength != othLength && !(isPartial && othLength > arrLength)) { if (arrLength != othLength && !(isPartial && othLength > arrLength)) {
return false; return false;
} }
// Assume cyclic values are equal. // Check that cyclic values are equal.
var stacked = stack.get(array); var arrStacked = stack.get(array);
if (stacked && stack.get(other)) { var othStacked = stack.get(other);
return stacked == other; if (arrStacked && othStacked) {
return arrStacked == other && othStacked == array;
} }
var index = -1, var index = -1,
result = true, result = true,
@@ -5799,10 +5800,11 @@
return false; return false;
} }
} }
// Assume cyclic values are equal. // Check that cyclic values are equal.
var stacked = stack.get(object); var objStacked = stack.get(object);
if (stacked && stack.get(other)) { var othStacked = stack.get(other);
return stacked == other; if (objStacked && othStacked) {
return objStacked == other && othStacked == object;
} }
var result = true; var result = true;
stack.set(object, other); stack.set(object, other);

View File

@@ -9741,7 +9741,7 @@
}); });
QUnit.test('should compare arrays with circular references', function(assert) { QUnit.test('should compare arrays with circular references', function(assert) {
assert.expect(4); assert.expect(6);
var array1 = [], var array1 = [],
array2 = []; array2 = [];
@@ -9766,6 +9766,14 @@
array2 = ['a', ['a', 'b', 'c'], 'c']; array2 = ['a', ['a', 'b', 'c'], 'c'];
assert.strictEqual(_.isEqual(array1, array2), false); assert.strictEqual(_.isEqual(array1, array2), false);
array1 = [[[]]];
array1[0][0][0] = array1;
array2 = [];
array2[0] = array2;
assert.strictEqual(_.isEqual(array1, array2), false);
assert.strictEqual(_.isEqual(array2, array1), false);
}); });
QUnit.test('should have transitive equivalence for circular references of arrays', function(assert) { QUnit.test('should have transitive equivalence for circular references of arrays', function(assert) {
@@ -9783,7 +9791,7 @@
}); });
QUnit.test('should compare objects with circular references', function(assert) { QUnit.test('should compare objects with circular references', function(assert) {
assert.expect(4); assert.expect(6);
var object1 = {}, var object1 = {},
object2 = {}; object2 = {};
@@ -9808,6 +9816,14 @@
object2 = { 'a': 1, 'b': { 'a': 1, 'b': 2, 'c': 3 }, 'c': 3 }; object2 = { 'a': 1, 'b': { 'a': 1, 'b': 2, 'c': 3 }, 'c': 3 };
assert.strictEqual(_.isEqual(object1, object2), false); assert.strictEqual(_.isEqual(object1, object2), false);
object1 = {self: {self: {self: {}}}};
object1.self.self.self = object1;
object2 = {self: {}};
object2.self = object2;
assert.strictEqual(_.isEqual(object1, object2), false);
assert.strictEqual(_.isEqual(object2, object1), false);
}); });
QUnit.test('should have transitive equivalence for circular references of objects', function(assert) { QUnit.test('should have transitive equivalence for circular references of objects', function(assert) {