Remove isDeep params from _.clone and _.flatten.

This commit is contained in:
jdalton
2015-07-07 08:34:06 -07:00
committed by John-David Dalton
parent 0b7bffe3b5
commit 9aa34e2487
2 changed files with 32 additions and 101 deletions

View File

@@ -4647,31 +4647,21 @@
}
/**
* Flattens a nested array. If `isDeep` is `true` the array is recursively
* flattened, otherwise it's only flattened a single level.
* Flattens a nested array a single level.
*
* @static
* @memberOf _
* @category Array
* @param {Array} array The array to flatten.
* @param {boolean} [isDeep] Specify a deep flatten.
* @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
* @returns {Array} Returns the new flattened array.
* @example
*
* _.flatten([1, [2, 3, [4]]]);
* // => [1, 2, 3, [4]]
*
* // using `isDeep`
* _.flatten([1, [2, 3, [4]]], true);
* // => [1, 2, 3, 4]
*/
function flatten(array, isDeep, guard) {
function flatten(array) {
var length = array ? array.length : 0;
if (guard && isIterateeCall(array, isDeep, guard)) {
isDeep = false;
}
return length ? baseFlatten(array, isDeep) : [];
return length ? baseFlatten(array) : [];
}
/**
@@ -6281,11 +6271,10 @@
* `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`.
*
* The guarded methods are:
* `ary`, `callback`, `chunk`, `clone`, `create`, `curry`, `curryRight`,
* `drop`, `dropRight`, `every`, `fill`, `flatten`, `invert`, `max`, `min`,
* `parseInt`, `slice`, `sortBy`, `take`, `takeRight`, `template`, `trim`,
* `trimLeft`, `trimRight`, `trunc`, `random`, `range`, `sample`, `some`,
* `sum`, `uniq`, and `words`
* `ary`, `callback`, `chunk`, `create`, `curry`, `curryRight`, `drop`,
* `dropRight`, `every`, `fill`, `invert`, `parseInt`, `random`, `range`,
* `sample`, `slice`, `some`, `sortBy`, `sumBy`, `take`, `takeRight`,
* `template`, `trim`, `trimLeft`, `trimRight`, `trunc`, `uniqBy`, and `words`
*
* @static
* @memberOf _
@@ -7755,11 +7744,10 @@
/*------------------------------------------------------------------------*/
/**
* Creates a clone of `value`. If `isDeep` is `true` nested objects are cloned,
* otherwise they are assigned by reference. If `customizer` is provided it's
* invoked to produce the cloned values. If `customizer` returns `undefined`
* cloning is handled by the method instead. The `customizer` is invoked with
* up to three argument; (value [, index|key, object]).
* Creates a clone of `value`. If `customizer` is provided it's invoked to
* produce the cloned values. If `customizer` returns `undefined` cloning is
* handled by the method instead. The `customizer` is invoked with up to
* three arguments; (value [, index|key, object]).
*
* **Note:** This method is loosely based on the
* [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm).
@@ -7772,7 +7760,6 @@
* @memberOf _
* @category Lang
* @param {*} value The value to clone.
* @param {boolean} [isDeep] Specify a deep clone.
* @param {Function} [customizer] The function to customize cloning values.
* @returns {*} Returns the cloned value.
* @example
@@ -7786,10 +7773,6 @@
* shallow[0] === users[0];
* // => true
*
* var deep = _.clone(users, true);
* deep[0] === users[0];
* // => false
*
* // using a customizer callback
* var el = _.clone(document.body, function(value) {
* if (_.isElement(value)) {
@@ -7804,24 +7787,17 @@
* el.childNodes.length;
* // => 0
*/
function clone(value, isDeep, customizer) {
if (isDeep && typeof isDeep != 'boolean' && isIterateeCall(value, isDeep, customizer)) {
isDeep = false;
}
else if (typeof isDeep == 'function') {
customizer = isDeep;
isDeep = false;
}
function clone(value, customizer) {
return typeof customizer == 'function'
? baseClone(value, isDeep, customizer)
: baseClone(value, isDeep);
? baseClone(value, false, customizer)
: baseClone(value);
}
/**
* Creates a deep clone of `value`. If `customizer` is provided it's invoked
* to produce the cloned values. If `customizer` returns `undefined` cloning
* is handled by the method instead. The `customizer` is invoked with up to
* three argument; (value [, index|key, object]).
* three arguments; (value [, index|key, object]).
*
* **Note:** This method is loosely based on the
* [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm).

View File

@@ -1949,17 +1949,6 @@
ok(actual !== array && actual[0] === array[0]);
});
test('`_.clone` should work with `isDeep`', 8, function() {
var array = [{ 'a': 0 }, { 'b': 1 }],
values = [true, 1, {}, '1'];
_.each(values, function(isDeep) {
var actual = _.clone(array, isDeep);
deepEqual(actual, array);
ok(actual !== array && actual[0] !== array[0]);
});
});
test('`_.cloneDeep` should deep clone objects with circular references', 1, function() {
var object = {
'foo': { 'b': { 'c': { 'd': {} } } },
@@ -4527,30 +4516,11 @@
deepEqual(_.flatten(array), [['a'], ['b']]);
});
test('should work with `isDeep`', 2, function() {
var array = [[['a']], [['b']]],
expected = ['a', 'b'];
test('should flatten `arguments` objects', 2, function() {
var array = [args, [args]];
deepEqual(_.flatten(array, true), expected);
deepEqual(_.flattenDeep(array), expected);
});
test('should flatten `arguments` objects', 3, function() {
var array = [args, [args]],
expected = [1, 2, 3, args];
deepEqual(_.flatten(array), expected);
expected = [1, 2, 3, 1, 2, 3];
deepEqual(_.flatten(array, true), expected);
deepEqual(_.flattenDeep(array), expected);
});
test('should work as an iteratee for methods like `_.map`', 2, function() {
var array = [[[['a']]], [[['b']]]];
deepEqual(_.map(array, _.flatten), [[['a']], [['b']]]);
deepEqual(_.map(array, _.flattenDeep), [['a'], ['b']]);
deepEqual(_.flatten(array), [1, 2, 3, args]);
deepEqual(_.flattenDeep(array), [1, 2, 3, 1, 2, 3]);
});
test('should treat sparse arrays as dense', 6, function() {
@@ -4588,26 +4558,18 @@
});
});
test('should work with empty arrays', 3, function() {
var array = [[], [[]], [[], [[[]]]]],
expected = [[], [], [[[]]]];
test('should work with empty arrays', 2, function() {
var array = [[], [[]], [[], [[[]]]]];
deepEqual(_.flatten(array), expected);
expected = [];
deepEqual(_.flatten(array, true), expected);
deepEqual(_.flattenDeep(array), expected);
deepEqual(_.flatten(array), [[], [], [[[]]]]);
deepEqual(_.flattenDeep(array), []);
});
test('should support flattening of nested arrays', 3, function() {
var array = [1, [2, 3], 4, [[5]]],
expected = [1, 2, 3, 4, [5]];
test('should support flattening of nested arrays', 2, function() {
var array = [1, [2, 3], 4, [[5]]];
deepEqual(_.flatten(array), expected);
expected = [1, 2, 3, 4, 5];
deepEqual(_.flatten(array, true), expected);
deepEqual(_.flattenDeep(array), expected);
deepEqual(_.flatten(array), [1, 2, 3, 4, [5]]);
deepEqual(_.flattenDeep(array), [1, 2, 3, 4, 5]);
});
test('should return an empty array for non array-like objects', 3, function() {
@@ -4618,28 +4580,21 @@
deepEqual(_.flattenDeep({ 'a': 1 }), expected);
});
test('should return a wrapped value when chaining', 6, function() {
test('should return a wrapped value when chaining', 4, function() {
if (!isNpm) {
var wrapped = _([1, [2], [3, [4]]]),
actual = wrapped.flatten(),
expected = [1, 2, 3, [4]];
actual = wrapped.flatten();
ok(actual instanceof _);
deepEqual(actual.value(), expected);
expected = [1, 2, 3, 4];
actual = wrapped.flatten(true);
ok(actual instanceof _);
deepEqual(actual.value(), expected);
deepEqual(actual.value(), [1, 2, 3, [4]]);
actual = wrapped.flattenDeep();
ok(actual instanceof _);
deepEqual(actual.value(), expected);
deepEqual(actual.value(), [1, 2, 3, 4]);
}
else {
skipTest(6);
skipTest(4);
}
});
}(1, 2, 3));