UNPKG

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