replaceLastOrElse
ts
function Array.replaceLastOrElse<T, U>(
target: readonly T[],
value: NoInfer<T>,
replacement: NoInfer<T>,
orElse: (target: readonly NoInfer<T>[]) => U,
): readonly T[] | UReplaces the last occurrence of value in target with replacement. If value is not found, calls orElse with the original array.
Example
ts
import { Array } from "@monstermann/array";
Array.replaceLastOrElse([1, 2, 3, 2], 2, 9, () => []); // [1, 2, 3, 9]
Array.replaceLastOrElse([1, 2, 3], 4, 9, (arr) => arr); // [1, 2, 3]ts
import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 2],
Array.replaceLastOrElse(2, 9, () => []),
); // [1, 2, 3, 9]
pipe(
[1, 2, 3],
Array.replaceLastOrElse(4, 9, (arr) => arr),
); // [1, 2, 3]