1 | var _arrayReduce =
|
---|
2 | /*#__PURE__*/
|
---|
3 | require("./internal/_arrayReduce.js");
|
---|
4 |
|
---|
5 | var _curry2 =
|
---|
6 | /*#__PURE__*/
|
---|
7 | require("./internal/_curry2.js");
|
---|
8 |
|
---|
9 | var _dispatchable =
|
---|
10 | /*#__PURE__*/
|
---|
11 | require("./internal/_dispatchable.js");
|
---|
12 |
|
---|
13 | var _map =
|
---|
14 | /*#__PURE__*/
|
---|
15 | require("./internal/_map.js");
|
---|
16 |
|
---|
17 | var _xmap =
|
---|
18 | /*#__PURE__*/
|
---|
19 | require("./internal/_xmap.js");
|
---|
20 |
|
---|
21 | var curryN =
|
---|
22 | /*#__PURE__*/
|
---|
23 | require("./curryN.js");
|
---|
24 |
|
---|
25 | var keys =
|
---|
26 | /*#__PURE__*/
|
---|
27 | require("./keys.js");
|
---|
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, R.pluck, R.project
|
---|
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 |
|
---|
65 | var 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 _arrayReduce(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 |
|
---|
87 | module.exports = map; |
---|