UNPKG

1.42 kBJavaScriptView Raw
1var _concat =
2/*#__PURE__*/
3require("./internal/_concat");
4
5var _curry3 =
6/*#__PURE__*/
7require("./internal/_curry3");
8/**
9 * Applies a function to the value at the given index of an array, returning a
10 * new copy of the array with the element at the given index replaced with the
11 * result of the function application.
12 *
13 * @func
14 * @memberOf R
15 * @since v0.14.0
16 * @category List
17 * @sig Number -> (a -> a) -> [a] -> [a]
18 * @param {Number} idx The index.
19 * @param {Function} fn The function to apply.
20 * @param {Array|Arguments} list An array-like object whose value
21 * at the supplied index will be replaced.
22 * @return {Array} A copy of the supplied array-like object with
23 * the element at index `idx` replaced with the value
24 * returned by applying `fn` to the existing element.
25 * @see R.update
26 * @example
27 *
28 * R.adjust(1, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'B', 'c', 'd']
29 * R.adjust(-1, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c', 'D']
30 * @symb R.adjust(-1, f, [a, b]) = [a, f(b)]
31 * @symb R.adjust(0, f, [a, b]) = [f(a), b]
32 */
33
34
35var adjust =
36/*#__PURE__*/
37_curry3(function adjust(idx, fn, list) {
38 if (idx >= list.length || idx < -list.length) {
39 return list;
40 }
41
42 var start = idx < 0 ? list.length : 0;
43
44 var _idx = start + idx;
45
46 var _list = _concat(list);
47
48 _list[_idx] = fn(list[_idx]);
49 return _list;
50});
51
52module.exports = adjust;
\No newline at end of file