mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 02:17:48 +00:00
Add _.flatMap.
This commit is contained in:
48
lodash.js
48
lodash.js
@@ -5549,6 +5549,31 @@
|
|||||||
: -1;
|
: -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.
|
* Flattens `array` a single level.
|
||||||
*
|
*
|
||||||
@@ -6954,6 +6979,27 @@
|
|||||||
return new LodashWrapper(this.value(), this.__chain__);
|
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
|
* Gets the next value on a wrapped object following the
|
||||||
* [iterator protocol](https://mdn.io/iteration_protocols#iterator).
|
* [iterator protocol](https://mdn.io/iteration_protocols#iterator).
|
||||||
@@ -13629,6 +13675,7 @@
|
|||||||
lodash.dropWhile = dropWhile;
|
lodash.dropWhile = dropWhile;
|
||||||
lodash.fill = fill;
|
lodash.fill = fill;
|
||||||
lodash.filter = filter;
|
lodash.filter = filter;
|
||||||
|
lodash.flatMap = flatMap;
|
||||||
lodash.flatten = flatten;
|
lodash.flatten = flatten;
|
||||||
lodash.flattenDeep = flattenDeep;
|
lodash.flattenDeep = flattenDeep;
|
||||||
lodash.flip = flip;
|
lodash.flip = flip;
|
||||||
@@ -14085,6 +14132,7 @@
|
|||||||
lodash.prototype.at = wrapperAt;
|
lodash.prototype.at = wrapperAt;
|
||||||
lodash.prototype.chain = wrapperChain;
|
lodash.prototype.chain = wrapperChain;
|
||||||
lodash.prototype.commit = wrapperCommit;
|
lodash.prototype.commit = wrapperCommit;
|
||||||
|
lodash.prototype.flatMap = wrapperFlatMap;
|
||||||
lodash.prototype.next = wrapperNext;
|
lodash.prototype.next = wrapperNext;
|
||||||
lodash.prototype.plant = wrapperPlant;
|
lodash.prototype.plant = wrapperPlant;
|
||||||
lodash.prototype.reverse = wrapperReverse;
|
lodash.prototype.reverse = wrapperReverse;
|
||||||
|
|||||||
@@ -22562,7 +22562,7 @@
|
|||||||
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
|
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
|
||||||
|
|
||||||
QUnit.test('should accept falsey arguments', function(assert) {
|
QUnit.test('should accept falsey arguments', function(assert) {
|
||||||
assert.expect(279);
|
assert.expect(280);
|
||||||
|
|
||||||
var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));
|
var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user