@monstermann/fn
Version:
A utility library for TypeScript.
34 lines (32 loc) • 984 B
JavaScript
import { is } from "../function/is.js";
import { resolveOffset } from "./internals/offset.js";
import { dfdlT } from "@monstermann/dfdl";
import { cloneArray } from "@monstermann/remmi";
//#region src/array/setAt.ts
/**
* `setAt(target, idx, value)`
*
* Sets the value at the specified `idx` in `target` to `value`. Returns the original array if the index is out of bounds or the value is already the same.
*
* ```ts
* setAt([1, 2, 3], 1, 9); // [1, 9, 3]
* setAt([1, 2, 3], -1, 9); // [1, 2, 9]
* setAt([1, 2, 3], 5, 9); // [1, 2, 3]
* ```
*
* ```ts
* pipe([1, 2, 3], setAt(1, 9)); // [1, 9, 3]
* pipe([1, 2, 3], setAt(-1, 9)); // [1, 2, 9]
* pipe([1, 2, 3], setAt(5, 9)); // [1, 2, 3]
* ```
*/
const setAt = dfdlT((target, idx, value) => {
const offset = resolveOffset(target, idx);
if (offset < 0) return target;
if (is(target[offset], value)) return target;
target = cloneArray(target);
target.splice(offset, 1, value);
return target;
}, 3);
//#endregion
export { setAt };