Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0cec225

Browse files
cukejianyajdalton
authored andcommittedOct 16, 2019
Fix lodash.isEqual for circular references (#4320) (#4515)
1 parent 94c3a81 commit 0cec225

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed
 

‎lodash.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -5634,10 +5634,11 @@
56345634
if (arrLength != othLength && !(isPartial && othLength > arrLength)) {
56355635
return false;
56365636
}
5637-
// Assume cyclic values are equal.
5638-
var stacked = stack.get(array);
5639-
if (stacked && stack.get(other)) {
5640-
return stacked == other;
5637+
// Check that cyclic values are equal.
5638+
var arrStacked = stack.get(array);
5639+
var othStacked = stack.get(other);
5640+
if (arrStacked && othStacked) {
5641+
return arrStacked == other && othStacked == array;
56415642
}
56425643
var index = -1,
56435644
result = true,
@@ -5799,10 +5800,11 @@
57995800
return false;
58005801
}
58015802
}
5802-
// Assume cyclic values are equal.
5803-
var stacked = stack.get(object);
5804-
if (stacked && stack.get(other)) {
5805-
return stacked == other;
5803+
// Check that cyclic values are equal.
5804+
var objStacked = stack.get(object);
5805+
var othStacked = stack.get(other);
5806+
if (objStacked && othStacked) {
5807+
return objStacked == other && othStacked == object;
58065808
}
58075809
var result = true;
58085810
stack.set(object, other);

‎test/test.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -9741,7 +9741,7 @@
97419741
});
97429742

97439743
QUnit.test('should compare arrays with circular references', function(assert) {
9744-
assert.expect(4);
9744+
assert.expect(6);
97459745

97469746
var array1 = [],
97479747
array2 = [];
@@ -9766,6 +9766,14 @@
97669766
array2 = ['a', ['a', 'b', 'c'], 'c'];
97679767

97689768
assert.strictEqual(_.isEqual(array1, array2), false);
9769+
9770+
array1 = [[[]]];
9771+
array1[0][0][0] = array1;
9772+
array2 = [];
9773+
array2[0] = array2;
9774+
9775+
assert.strictEqual(_.isEqual(array1, array2), false);
9776+
assert.strictEqual(_.isEqual(array2, array1), false);
97699777
});
97709778

97719779
QUnit.test('should have transitive equivalence for circular references of arrays', function(assert) {
@@ -9783,7 +9791,7 @@
97839791
});
97849792

97859793
QUnit.test('should compare objects with circular references', function(assert) {
9786-
assert.expect(4);
9794+
assert.expect(6);
97879795

97889796
var object1 = {},
97899797
object2 = {};
@@ -9808,6 +9816,14 @@
98089816
object2 = { 'a': 1, 'b': { 'a': 1, 'b': 2, 'c': 3 }, 'c': 3 };
98099817

98109818
assert.strictEqual(_.isEqual(object1, object2), false);
9819+
9820+
object1 = {self: {self: {self: {}}}};
9821+
object1.self.self.self = object1;
9822+
object2 = {self: {}};
9823+
object2.self = object2;
9824+
9825+
assert.strictEqual(_.isEqual(object1, object2), false);
9826+
assert.strictEqual(_.isEqual(object2, object1), false);
98119827
});
98129828

98139829
QUnit.test('should have transitive equivalence for circular references of objects', function(assert) {

0 commit comments

Comments
 (0)
Failed to load comments.