[go: up one dir, main page]

Skip to content

Commit

Permalink
Fix lodash.isEqual for circular references (#4320) (#4515)
Browse files Browse the repository at this point in the history
  • Loading branch information
cukejianya authored and jdalton committed Oct 16, 2019
1 parent 94c3a81 commit 0cec225
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
18 changes: 10 additions & 8 deletions lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -5634,10 +5634,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 @@ -5799,10 +5800,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
Original file line number Diff line number Diff line change
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

0 comments on commit 0cec225

Please sign in to comment.