ramda
Version:
A practical functional library for JavaScript programmers.
44 lines • 1.7 kB
JavaScript
var _concat = /*#__PURE__*/require("./internal/_concat.js");
var _curry3 = /*#__PURE__*/require("./internal/_curry3.js");
/**
* Applies a function to the value at the given index of an array, returning a
* new copy of the array with the element at the given index replaced with the
* result of the function application.
*
* When `idx < -list.length || idx >= list.length`, the original list is returned.
*
* @func
* @memberOf R
* @since v0.14.0
* @category List
* @sig Number -> (a -> a) -> [a] -> [a]
* @param {Number} idx The index.
* @param {Function} fn The function to apply.
* @param {Array|Arguments} list An array-like object whose value
* at the supplied index will be replaced.
* @return {Array} A copy of the supplied array-like object with
* the element at index `idx` replaced with the value
* returned by applying `fn` to the existing element.
* @see R.update
* @example
*
* R.adjust(1, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'B', 'c', 'd']
* R.adjust(-1, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c', 'D']
*
* // out-of-range returns original list
* R.adjust(4, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c', 'd']
* R.adjust(-5, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c', 'd']
* @symb R.adjust(-1, f, [a, b]) = [a, f(b)]
* @symb R.adjust(0, f, [a, b]) = [f(a), b]
*/
var adjust = /*#__PURE__*/_curry3(function adjust(idx, fn, list) {
var len = list.length;
if (idx >= len || idx < -len) {
return list;
}
var _idx = (len + idx) % len;
var _list = _concat(list);
_list[_idx] = fn(list[_idx]);
return _list;
});
module.exports = adjust;