option.ts 670 B

12345678910111213141516171819
  1. import * as O from "fp-ts/Option"
  2. import * as A from "fp-ts/Array"
  3. import { pipe } from "fp-ts/function"
  4. /**
  5. * Tries to match one of the given predicates.
  6. * If a predicate is matched, the associated value is returned in a Some.
  7. * Else if none of the predicates is matched, None is returned.
  8. * @param choice An array of tuples having a predicate function and the selected value
  9. * @returns A function which takes the input and returns an Option
  10. */
  11. export const optionChoose =
  12. <T, V>(choice: Array<[(x: T) => boolean, V]>) =>
  13. (input: T): O.Option<V> =>
  14. pipe(
  15. choice,
  16. A.findFirst(([pred]) => pred(input)),
  17. O.map(([_, value]) => value)
  18. )