@monstermann/fn
Version:
A utility library for TypeScript.
37 lines (35 loc) • 1.11 kB
TypeScript
import { ArrayMap, OrElse } from "./internals/types.js";
//#region src/array/mapAtOrElse.d.ts
/**
* `mapAtOrElse(array, index, mapper, callback)`
*
* Applies the `mapper` function to the element at the specified `index` in `array`, returning a new array with the mapped element, or the result of calling `callback` with the array if the index is out of bounds.
*
* ```ts
* mapAtOrElse(
* [1, 2, 3],
* 10,
* (x) => x * 10,
* (arr) => arr.length,
* ); // 3
* ```
*
* ```ts
* pipe(
* [1, 2, 3],
* mapAtOrElse(
* 10,
* (x) => x * 10,
* (arr) => arr.length,
* ),
* ); // 3
* ```
*/
declare const mapAtOrElse: {
<T, U>(idx: number, map: ArrayMap<T>, orElse: OrElse<T, U>): (target: T[]) => T[] | U;
<T, U>(idx: number, map: ArrayMap<T>, orElse: OrElse<T, U>): (target: readonly T[]) => readonly T[] | U;
<T, U>(target: T[], idx: number, map: ArrayMap<T>, orElse: OrElse<T, U>): T[] | U;
<T, U>(target: readonly T[], idx: number, map: ArrayMap<T>, orElse: OrElse<T, U>): readonly T[] | U;
};
//#endregion
export { mapAtOrElse };