Option overview
Extension to the Effect Option module providing interoperability with nullable values, JavaScript iterators, and Effect.
Mental model
Option.Option<A>is a sum ofSome<A>andNone.- This module focuses on bridging
Optionwith non-Effect-aware shapes (nullable values, raw iterators) and lifting it intoEffect.
Common tasks
- Bridge with nullables: {@link OptionOrNullable}, {@link fromOptionOrNullable}
- Bridge with iterators: {@link fromNextIteratorValue}
Quickstart
Example (Bridging from nullable input)
import * as MOption from "@parischap/effect-lib/MOption"
console.log(MOption.fromOptionOrNullable(null)) // None
console.log(MOption.fromOptionOrNullable(undefined)) // None
console.log(MOption.fromOptionOrNullable(42)) // Some(42)
Table of contents
Models
OptionOrNullable (type alias)
Tagged union representing “an optional A” using whichever shape callers find natural: an Option<A>, a bare A, or null/undefined.
- Use at API boundaries that accept input from code unaware of
Option.
Signature
export type OptionOrNullable<A> = Option.Option<A> | null | undefined | A
Type (type alias)
Type on which this module’s functions operate.
Signature
export type Type<A> = Option.Option<A>
Utils
fromNextIteratorValue
Advances iterator by one step and lifts the result into an Option.
- Returns
Option.some(value)when the iterator produced a value,Option.nonewhen it is exhausted.
Example (Pull next value from an iterator)
import * as MOption from "@parischap/effect-lib/MOption"
const it = [1, 2][Symbol.iterator]()
console.log(MOption.fromNextIteratorValue(it)) // Some(1)
console.log(MOption.fromNextIteratorValue(it)) // Some(2)
console.log(MOption.fromNextIteratorValue(it)) // None
Signature
export declare const fromNextIteratorValue: <A>(iterator: Iterator<A>) => Option.Option<A>
fromOptionOrNullable
Normalizes an {@link OptionOrNullable} to an Option.
- When the input is already an
Option, it is returned as-is. nullandundefinedbecomeOption.none; any other value becomesOption.some.
Example (Normalize varied inputs)
import { Option } from "effect"
import * as MOption from "@parischap/effect-lib/MOption"
console.log(MOption.fromOptionOrNullable(null)) // None
console.log(MOption.fromOptionOrNullable(undefined)) // None
console.log(MOption.fromOptionOrNullable(42)) // Some(42)
console.log(MOption.fromOptionOrNullable(Option.some(7))) // Some(7)
Signature
export declare const fromOptionOrNullable: <A>(a: OptionOrNullable<A>) => Option.Option<A>