| 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 | * Creates a new list iteration function from an existing one by adding two new
|
|---|
| 14 | * parameters to its callback function: the current index, and the entire list.
|
|---|
| 15 | *
|
|---|
| 16 | * This would turn, for instance, [`R.map`](#map) function into one that
|
|---|
| 17 | * more closely resembles `Array.prototype.map`. Note that this will only work
|
|---|
| 18 | * for functions in which the iteration callback function is the first
|
|---|
| 19 | * parameter, and where the list is the last parameter. (This latter might be
|
|---|
| 20 | * unimportant if the list parameter is not used.)
|
|---|
| 21 | *
|
|---|
| 22 | * @func
|
|---|
| 23 | * @memberOf R
|
|---|
| 24 | * @since v0.15.0
|
|---|
| 25 | * @category Function
|
|---|
| 26 | * @category List
|
|---|
| 27 | * @sig (((a ...) -> b) ... -> [a] -> *) -> (((a ..., Int, [a]) -> b) ... -> [a] -> *)
|
|---|
| 28 | * @param {Function} fn A list iteration function that does not pass index or list to its callback
|
|---|
| 29 | * @return {Function} An altered list iteration function that passes (item, index, list) to its callback
|
|---|
| 30 | * @example
|
|---|
| 31 | *
|
|---|
| 32 | * const mapIndexed = R.addIndex(R.map);
|
|---|
| 33 | * mapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']);
|
|---|
| 34 | * //=> ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | var addIndex =
|
|---|
| 39 | /*#__PURE__*/
|
|---|
| 40 | _curry1(function addIndex(fn) {
|
|---|
| 41 | return curryN(fn.length, function () {
|
|---|
| 42 | var idx = 0;
|
|---|
| 43 | var origFn = arguments[0];
|
|---|
| 44 | var list = arguments[arguments.length - 1];
|
|---|
| 45 | var args = Array.prototype.slice.call(arguments, 0);
|
|---|
| 46 |
|
|---|
| 47 | args[0] = function () {
|
|---|
| 48 | var result = origFn.apply(this, _concat(arguments, [idx, list]));
|
|---|
| 49 | idx += 1;
|
|---|
| 50 | return result;
|
|---|
| 51 | };
|
|---|
| 52 |
|
|---|
| 53 | return fn.apply(this, args);
|
|---|
| 54 | });
|
|---|
| 55 | });
|
|---|
| 56 |
|
|---|
| 57 | module.exports = addIndex; |
|---|