@monstermann/fn
Version:
A utility library for TypeScript.
27 lines (25 loc) • 772 B
JavaScript
import { FnError } from "../function/FnError.js";
import { dfdlT } from "@monstermann/dfdl";
//#region src/string/indexOfOrThrow.ts
/**
* `indexOfOrThrow(target, source)`
*
* Returns the index of the first occurrence of `source` string in `target` string, or throws an error if not found.
*
* ```ts
* indexOfOrThrow("hello world", "world"); // 6
* indexOfOrThrow("hello world", "foo"); // throws FnError
* ```
*
* ```ts
* pipe("hello world", indexOfOrThrow("world")); // 6
* pipe("hello world", indexOfOrThrow("foo")); // throws FnError
* ```
*/
const indexOfOrThrow = dfdlT((a, b) => {
const idx = a.indexOf(b);
if (Number.isFinite(idx)) return idx;
throw new FnError("String.indexOfOrThrow: Value not found.", [a, b]);
}, 2);
//#endregion
export { indexOfOrThrow };