source: node_modules/ramda/src/addIndexRight.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.6 KB
Line 
1var _concat =
2/*#__PURE__*/
3require("./internal/_concat.js");
4
5var _curry1 =
6/*#__PURE__*/
7require("./internal/_curry1.js");
8
9var curryN =
10/*#__PURE__*/
11require("./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
36var 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
55module.exports = addIndexRight;
Note: See TracBrowser for help on using the repository browser.