Add _.flatMap.

This commit is contained in:
John-David Dalton
2015-11-20 18:04:43 -08:00
parent d4eeb8d186
commit 11d8867d7a
2 changed files with 49 additions and 1 deletions

View File

@@ -5549,6 +5549,31 @@
: -1;
}
/**
* Creates an array of flattened values by running each element in `array`
* through `iteratee` and concating its result to the other mapped values.
* The iteratee is invoked with three arguments: (value, index|key, array).
*
* @static
* @memberOf _
* @category Array
* @param {Array} array The array to iterate over.
* @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration.
* @returns {Array} Returns the new array.
* @example
*
* function duplicate(n) {
* return [n, n];
* }
*
* _.flatMap([1, 2], duplicate);
* // => [1, 1, 2, 2]
*/
function flatMap(array, iteratee) {
var length = array ? array.length : 0;
return length ? baseFlatten(arrayMap(array, getIteratee(iteratee, 3))) : [];
}
/**
* Flattens `array` a single level.
*
@@ -6954,6 +6979,27 @@
return new LodashWrapper(this.value(), this.__chain__);
}
/**
* This method is the wrapper version of `_.flatMap`.
*
* @static
* @memberOf _
* @category Chain
* @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration.
* @returns {Object} Returns the new `lodash` wrapper instance.
* @example
*
* function duplicate(n) {
* return [n, n];
* }
*
* _([1, 2]).flatMap(duplicate).value();
* // => [1, 1, 2, 2]
*/
function wrapperFlatMap(iteratee) {
return this.map(iteratee).flatten();
}
/**
* Gets the next value on a wrapped object following the
* [iterator protocol](https://mdn.io/iteration_protocols#iterator).
@@ -13629,6 +13675,7 @@
lodash.dropWhile = dropWhile;
lodash.fill = fill;
lodash.filter = filter;
lodash.flatMap = flatMap;
lodash.flatten = flatten;
lodash.flattenDeep = flattenDeep;
lodash.flip = flip;
@@ -14085,6 +14132,7 @@
lodash.prototype.at = wrapperAt;
lodash.prototype.chain = wrapperChain;
lodash.prototype.commit = wrapperCommit;
lodash.prototype.flatMap = wrapperFlatMap;
lodash.prototype.next = wrapperNext;
lodash.prototype.plant = wrapperPlant;
lodash.prototype.reverse = wrapperReverse;

View File

@@ -22562,7 +22562,7 @@
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
QUnit.test('should accept falsey arguments', function(assert) {
assert.expect(279);
assert.expect(280);
var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));