mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 18:07:49 +00:00
Ensure _.flattenDeep works on arrays with circular references. [closes #1664]
This commit is contained in:
16
lodash.js
16
lodash.js
@@ -2489,9 +2489,10 @@
|
|||||||
* @param {boolean} [isDeep] Specify a deep flatten.
|
* @param {boolean} [isDeep] Specify a deep flatten.
|
||||||
* @param {boolean} [isStrict] Restrict flattening to arrays-like objects.
|
* @param {boolean} [isStrict] Restrict flattening to arrays-like objects.
|
||||||
* @param {Array} [result=[]] The initial result value.
|
* @param {Array} [result=[]] The initial result value.
|
||||||
|
* @param {Object} [stack] Tracks traversed arrays.
|
||||||
* @returns {Array} Returns the new flattened array.
|
* @returns {Array} Returns the new flattened array.
|
||||||
*/
|
*/
|
||||||
function baseFlatten(array, isDeep, isStrict, result) {
|
function baseFlatten(array, isDeep, isStrict, result, stack) {
|
||||||
result || (result = []);
|
result || (result = []);
|
||||||
|
|
||||||
var index = -1,
|
var index = -1,
|
||||||
@@ -2502,9 +2503,18 @@
|
|||||||
if (isArrayLikeObject(value) &&
|
if (isArrayLikeObject(value) &&
|
||||||
(isStrict || isArray(value) || isArguments(value))) {
|
(isStrict || isArray(value) || isArguments(value))) {
|
||||||
if (isDeep) {
|
if (isDeep) {
|
||||||
|
stack || (stack = new Stack);
|
||||||
|
if (stack.get(array)) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
stack.set(array, true);
|
||||||
|
|
||||||
// Recursively flatten arrays (susceptible to call stack limits).
|
// Recursively flatten arrays (susceptible to call stack limits).
|
||||||
baseFlatten(value, isDeep, isStrict, result);
|
baseFlatten(value, isDeep, isStrict, result, stack);
|
||||||
} else {
|
|
||||||
|
stack['delete'](array);
|
||||||
|
}
|
||||||
|
else {
|
||||||
arrayPush(result, value);
|
arrayPush(result, value);
|
||||||
}
|
}
|
||||||
} else if (!isStrict) {
|
} else if (!isStrict) {
|
||||||
|
|||||||
26
test/test.js
26
test/test.js
@@ -5081,6 +5081,32 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.flattenDeep');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
QUnit.test('should flatten arrays with circular references', function(assert) {
|
||||||
|
assert.expect(2);
|
||||||
|
|
||||||
|
var array = [1, 2];
|
||||||
|
array.push(array);
|
||||||
|
|
||||||
|
var expected = [1, 2, 1, 2];
|
||||||
|
|
||||||
|
try {
|
||||||
|
var actual = _.flattenDeep(array);
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
|
||||||
|
array = [1, 2];
|
||||||
|
array = [array, [array]];
|
||||||
|
|
||||||
|
assert.deepEqual(_.flattenDeep(array), expected);
|
||||||
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('flatten methods');
|
QUnit.module('flatten methods');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user