Skip to content

indexOfOrElse

ts
function String.indexOfOrElse<T>(
    target: string,
    source: string,
    orElse: (target: string) => T,
): number | T

Returns the index of the first occurrence of source string in target string, or the result of calling orElse function with target if not found.

Example

ts
import { String } from "@monstermann/string";

String.indexOfOrElse("hello world", "world", () => -1); // 6
String.indexOfOrElse("hello world", "foo", (str) => str.length); // 11
ts
import { String } from "@monstermann/string";

pipe(
    "hello world",
    String.indexOfOrElse("world", () => -1),
); // 6

pipe(
    "hello world",
    String.indexOfOrElse("foo", (str) => str.length),
); // 11