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