UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

27 lines (25 loc) 825 B
import { FnError } from "../function/FnError.js"; import { dfdlT } from "@monstermann/dfdl"; //#region src/string/lastIndexOfOrThrow.ts /** * `lastIndexOfOrThrow(target, source)` * * Returns the index of the last occurrence of `source` string in `target` string, or throws an error if not found. * * ```ts * lastIndexOfOrThrow("hello world hello", "hello"); // 12 * lastIndexOfOrThrow("hello world", "foo"); // throws FnError * ``` * * ```ts * pipe("hello world hello", lastIndexOfOrThrow("hello")); // 12 * pipe("hello world", lastIndexOfOrThrow("foo")); // throws FnError * ``` */ const lastIndexOfOrThrow = dfdlT((a, b) => { const idx = a.lastIndexOf(b); if (Number.isFinite(idx)) return idx; throw new FnError("String.lastIndexOfOrThrow: Value not found.", [a, b]); }, 2); //#endregion export { lastIndexOfOrThrow };