@arrows/array
Version:
Functional tools for JS arrays
27 lines (26 loc) • 833 B
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.update_ = void 0;
const curry_1 = require("@arrows/composition/curry");
const _update_ = (updaterFn, index, arr) => {
if (index > arr.length - 1) {
throw new Error("The provided index is out of bound of the array.");
}
const newArr = [...arr];
newArr[index] = updaterFn(newArr[index]);
return newArr;
};
/**
* Creates a new array with a new value at the provided index,
* calculated by updater function that maps an old value into a new one.
*
* If the index is out of bound of the array throws an error.
*
* @param value New value
* @param index Specific index
* @param arr Initial array
* @returns New array
*/
const update_ = curry_1.default(_update_);
exports.update_ = update_;
exports.default = update_;
;