@monstermann/fn
Version:
A utility library for TypeScript.
24 lines (22 loc) • 713 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
//#region src/string/matchOr.ts
/**
* `matchOr(target, source, or)`
*
* Returns the result of matching `target` string against `source` string or regular expression, or the `or` value if no match is found.
*
* ```ts
* matchOr("hello world", "world", []); // ["world", index: 6, input: "hello world", groups: undefined]
* matchOr("hello world", /\d+/, []); // []
* ```
*
* ```ts
* pipe("hello world", matchOr("world", [])); // ["world", index: 6, input: "hello world", groups: undefined]
* pipe("hello world", matchOr(/\d+/, [])); // []
* ```
*/
const matchOr = dfdlT((target, source, or) => {
return target.match(source) ?? or;
}, 3);
//#endregion
export { matchOr };