Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lodash.isEqual for circular references #4320 #4515

Merged
merged 1 commit into from Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions lodash.js
Expand Up @@ -5618,10 +5618,11 @@
if (arrLength != othLength && !(isPartial && othLength > arrLength)) {
return false;
}
// Assume cyclic values are equal.
var stacked = stack.get(array);
if (stacked && stack.get(other)) {
return stacked == other;
// Check that cyclic values are equal.
var arrStacked = stack.get(array);
var othStacked = stack.get(other);
if (arrStacked && othStacked) {
return arrStacked == other && othStacked == array;
}
var index = -1,
result = true,
Expand Down Expand Up @@ -5783,10 +5784,11 @@
return false;
}
}
// Assume cyclic values are equal.
var stacked = stack.get(object);
if (stacked && stack.get(other)) {
return stacked == other;
// Check that cyclic values are equal.
var objStacked = stack.get(object);
var othStacked = stack.get(other);
if (objStacked && othStacked) {
return objStacked == other && othStacked == object;
}
var result = true;
stack.set(object, other);
Expand Down
20 changes: 18 additions & 2 deletions test/test.js
Expand Up @@ -9741,7 +9741,7 @@
});

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

var array1 = [],
array2 = [];
Expand All @@ -9766,6 +9766,14 @@
array2 = ['a', ['a', 'b', 'c'], 'c'];

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) {
Expand All @@ -9783,7 +9791,7 @@
});

QUnit.test('should compare objects with circular references', function(assert) {
assert.expect(4);
assert.expect(6);

var object1 = {},
object2 = {};
Expand All @@ -9808,6 +9816,14 @@
object2 = { 'a': 1, 'b': { 'a': 1, 'b': 2, 'c': 3 }, 'c': 3 };

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) {
Expand Down