Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0cec225

Browse filesBrowse files
cukejianyajdalton
authored andcommitted
Fix lodash.isEqual for circular references (#4320) (#4515)
1 parent 94c3a81 commit 0cec225
Copy full SHA for 0cec225

File tree

2 files changed

+28
-10
lines changed
Filter options

2 files changed

+28
-10
lines changed

‎lodash.js

Copy file name to clipboardExpand all lines: lodash.js
+10-8Lines changed: 10 additions & 8 deletions
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

Copy file name to clipboardExpand all lines: test/test.js
+18-2Lines changed: 18 additions & 2 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.