1 | var _concat =
|
---|
2 | /*#__PURE__*/
|
---|
3 | require("./internal/_concat.js");
|
---|
4 |
|
---|
5 | var _curry1 =
|
---|
6 | /*#__PURE__*/
|
---|
7 | require("./internal/_curry1.js");
|
---|
8 |
|
---|
9 | var curryN =
|
---|
10 | /*#__PURE__*/
|
---|
11 | require("./curryN.js");
|
---|
12 | /**
|
---|
13 | * As with `addIndex`, `addIndexRight` creates a new list iteration function
|
---|
14 | * from an existing one by adding two new parameters to its callback function:
|
---|
15 | * the current index, and the entire list.
|
---|
16 | *
|
---|
17 | * Unlike `addIndex`, `addIndexRight` iterates from the right to the left.
|
---|
18 | *
|
---|
19 | * @func
|
---|
20 | * @memberOf R
|
---|
21 | * @since v0.29.0
|
---|
22 | * @category Function
|
---|
23 | * @category List
|
---|
24 | * @sig ((a ... -> b) ... -> [a] -> *) -> (a ..., Int, [a] -> b) ... -> [a] -> *)
|
---|
25 | * @param {Function} fn A list iteration function that does not pass index or list to its callback
|
---|
26 | * @return {Function} An altered list iteration function that passes (item, index, list) to its callback
|
---|
27 | * @example
|
---|
28 | *
|
---|
29 | * const revmap = (fn, ary) => R.map(fn, R.reverse(ary));
|
---|
30 | * const revmapIndexed = R.addIndexRight(revmap);
|
---|
31 | * revmapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']);
|
---|
32 | * //=> [ '5-r', '4-a', '3-b', '2-o', '1-o', '0-f' ]
|
---|
33 | */
|
---|
34 |
|
---|
35 |
|
---|
36 | var addIndexRight =
|
---|
37 | /*#__PURE__*/
|
---|
38 | _curry1(function addIndex(fn) {
|
---|
39 | return curryN(fn.length, function () {
|
---|
40 | var origFn = arguments[0];
|
---|
41 | var list = arguments[arguments.length - 1];
|
---|
42 | var idx = list.length - 1;
|
---|
43 | var args = Array.prototype.slice.call(arguments, 0);
|
---|
44 |
|
---|
45 | args[0] = function () {
|
---|
46 | var result = origFn.apply(this, _concat(arguments, [idx, list]));
|
---|
47 | idx -= 1;
|
---|
48 | return result;
|
---|
49 | };
|
---|
50 |
|
---|
51 | return fn.apply(this, args);
|
---|
52 | });
|
---|
53 | });
|
---|
54 |
|
---|
55 | module.exports = addIndexRight; |
---|