Files
lodash/map.js
2017-01-09 17:38:33 -08:00

43 lines
1.3 KiB
JavaScript

import arrayMap from './_arrayMap.js';
import baseMap from './_baseMap.js';
import isArray from './isArray.js';
/**
* Creates an array of values by running each element in `collection` thru
* `iteratee`. The iteratee is invoked with three arguments:
* (value, index|key, collection).
*
* Many lodash methods are guarded to work as iteratees for methods like
* `every`, `filter`, `map`, `mapValues`, `reject`, and `some`.
*
* The guarded methods are:
* `ary`, `chunk`, `curry`, `curryRight`, `drop`, `dropRight`, `every`,
* `fill`, `invert`, `parseInt`, `random`, `range`, `rangeRight`, `repeat`,
* `sampleSize`, `slice`, `some`, `sortBy`, `split`, `take`, `takeRight`,
* `template`, `trim`, `trimEnd`, `trimStart`, and `words`
*
* @static
* @since 0.1.0
* @category Collection
* @param {Array|Object} collection The collection to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @returns {Array} Returns the new mapped array.
* @example
*
* function square(n) {
* return n * n;
* }
*
* map([4, 8], square);
* // => [16, 64]
*
* map({ 'a': 4, 'b': 8 }, square);
* // => [16, 64] (iteration order is not guaranteed)
*/
function map(collection, iteratee) {
const func = isArray(collection) ? arrayMap : baseMap;
return func(collection, iteratee);
}
export default map;