Hyperlinkv0.8.0-beta.28

Option

Option.liftNullishOrconsteffect/Option.ts:941
<A extends ReadonlyArray<unknown>, B>(f: (...a: A) => B): (
  ...a: A
) => Option<NonNullable<B>>

Lifts a function that may return null or undefined into one that returns an Option.

When to use

Use to wrap existing nullable-returning functions for use in Option pipelines

Details

  • Calls the original function with the given arguments
  • Wraps the result via fromNullishOr

Example (Lifting a parser)

import { Option } from "effect"

const parse = (s: string): number | undefined => {
  const n = parseFloat(s)
  return isNaN(n) ? undefined : n
}

const parseOption = Option.liftNullishOr(parse)

console.log(parseOption("1"))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }

console.log(parseOption("not a number"))
// Output: { _id: 'Option', _tag: 'None' }
Source effect/Option.ts:9414 lines
export const liftNullishOr = <A extends ReadonlyArray<unknown>, B>(
  f: (...a: A) => B
): (...a: A) => Option<NonNullable<B>> =>
(...a) => fromNullishOr(f(...a))