@monstermann/fn
Version:
A utility library for TypeScript.
25 lines (23 loc) • 577 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
//#region src/array/indexOfOr.ts
/**
* `indexOfOr(target, value, or)`
*
* Returns the index of the first occurrence of `value` in `target`. If `value` is not found, returns `or`.
*
* ```ts
* indexOfOr([1, 2, 3, 2], 2, -1); // 1
* indexOfOr([1, 2, 3], 4, -1); // -1
* ```
*
* ```ts
* pipe([1, 2, 3, 2], indexOfOr(2, -1)); // 1
* pipe([1, 2, 3], indexOfOr(4, -1)); // -1
* ```
*/
const indexOfOr = dfdlT((target, value, or) => {
const idx = target.indexOf(value);
return idx < 0 ? or : idx;
}, 3);
//#endregion
export { indexOfOr };