@monstermann/fn
Version:
A utility library for TypeScript.
25 lines (23 loc) • 612 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
//#region src/string/indexOfOr.ts
/**
* `indexOfOr(target, source, or)`
*
* Returns the index of the first occurrence of `source` string in `target` string, or the `or` value if not found.
*
* ```ts
* indexOfOr("hello world", "world", -1); // 6
* indexOfOr("hello world", "foo", -1); // -1
* ```
*
* ```ts
* pipe("hello world", indexOfOr("world", -1)); // 6
* pipe("hello world", indexOfOr("foo", -1)); // -1
* ```
*/
const indexOfOr = dfdlT((a, b, or) => {
const idx = a.indexOf(b);
return Number.isFinite(idx) ? idx : or;
}, 3);
//#endregion
export { indexOfOr };