[d24f17c] | 1 | import _arrayReduce from "./internal/_arrayReduce.js";
|
---|
| 2 | import _curry2 from "./internal/_curry2.js";
|
---|
| 3 | import _dispatchable from "./internal/_dispatchable.js";
|
---|
| 4 | import _map from "./internal/_map.js";
|
---|
| 5 | import _xmap from "./internal/_xmap.js";
|
---|
| 6 | import curryN from "./curryN.js";
|
---|
| 7 | import keys from "./keys.js";
|
---|
| 8 | /**
|
---|
| 9 | * Takes a function and
|
---|
| 10 | * a [functor](https://github.com/fantasyland/fantasy-land#functor),
|
---|
| 11 | * applies the function to each of the functor's values, and returns
|
---|
| 12 | * a functor of the same shape.
|
---|
| 13 | *
|
---|
| 14 | * Ramda provides suitable `map` implementations for `Array` and `Object`,
|
---|
| 15 | * so this function may be applied to `[1, 2, 3]` or `{x: 1, y: 2, z: 3}`.
|
---|
| 16 | *
|
---|
| 17 | * Dispatches to the `map` method of the second argument, if present.
|
---|
| 18 | *
|
---|
| 19 | * Acts as a transducer if a transformer is given in list position.
|
---|
| 20 | *
|
---|
| 21 | * Also treats functions as functors and will compose them together.
|
---|
| 22 | *
|
---|
| 23 | * @func
|
---|
| 24 | * @memberOf R
|
---|
| 25 | * @since v0.1.0
|
---|
| 26 | * @category List
|
---|
| 27 | * @sig Functor f => (a -> b) -> f a -> f b
|
---|
| 28 | * @param {Function} fn The function to be called on every element of the input `list`.
|
---|
| 29 | * @param {Array} list The list to be iterated over.
|
---|
| 30 | * @return {Array} The new list.
|
---|
| 31 | * @see R.transduce, R.addIndex, R.pluck, R.project
|
---|
| 32 | * @example
|
---|
| 33 | *
|
---|
| 34 | * const double = x => x * 2;
|
---|
| 35 | *
|
---|
| 36 | * R.map(double, [1, 2, 3]); //=> [2, 4, 6]
|
---|
| 37 | *
|
---|
| 38 | * R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}
|
---|
| 39 | * @symb R.map(f, [a, b]) = [f(a), f(b)]
|
---|
| 40 | * @symb R.map(f, { x: a, y: b }) = { x: f(a), y: f(b) }
|
---|
| 41 | * @symb R.map(f, functor_o) = functor_o.map(f)
|
---|
| 42 | */
|
---|
| 43 |
|
---|
| 44 | var map =
|
---|
| 45 | /*#__PURE__*/
|
---|
| 46 | _curry2(
|
---|
| 47 | /*#__PURE__*/
|
---|
| 48 | _dispatchable(['fantasy-land/map', 'map'], _xmap, function map(fn, functor) {
|
---|
| 49 | switch (Object.prototype.toString.call(functor)) {
|
---|
| 50 | case '[object Function]':
|
---|
| 51 | return curryN(functor.length, function () {
|
---|
| 52 | return fn.call(this, functor.apply(this, arguments));
|
---|
| 53 | });
|
---|
| 54 |
|
---|
| 55 | case '[object Object]':
|
---|
| 56 | return _arrayReduce(function (acc, key) {
|
---|
| 57 | acc[key] = fn(functor[key]);
|
---|
| 58 | return acc;
|
---|
| 59 | }, {}, keys(functor));
|
---|
| 60 |
|
---|
| 61 | default:
|
---|
| 62 | return _map(fn, functor);
|
---|
| 63 | }
|
---|
| 64 | }));
|
---|
| 65 |
|
---|
| 66 | export default map; |
---|