import type { Predicate } from './Predicate' import type { Refinement } from './Refinement' export interface None { readonly _tag: 'None' } export interface Some { readonly _tag: 'Some' readonly value: A } export type Option = None | Some /** * @internal */ export function Some(a: A): Option { return { _tag: 'Some', value: a } } /** * @internal */ export function None(): Option { return { _tag: 'None' } } /** * Constructs a new `Option` from a value and the given predicate * * @category Constructors * @since 1.0.0 * @internal */ export function fromPredicate_(a: A, refinement: Refinement): Option export function fromPredicate_(a: A, predicate: Predicate): Option export function fromPredicate_(a: A, predicate: Predicate): Option { return predicate(a) ? None() : Some(a) } /** * Returns a smart constructor based on the given predicate * * @category Constructors * @since 1.0.0 * @internal */ export function fromPredicate(refinement: Refinement): (a: A) => Option export function fromPredicate(predicate: Predicate): (a: A) => Option export function fromPredicate(predicate: Predicate): (a: A) => Option { return (a) => fromPredicate_(a, predicate) } export function fromNullable(a: A | null | undefined): Option> { return a == null ? None() : Some(a as NonNullable) } /** * @internal */ export function getOrElse_(fa: Option, onNone: () => B): A | B { return fa._tag === 'Some' ? fa.value : onNone() } /** * @internal */ export function getOrElse(onNone: () => B): (fa: Option) => A | B { return (fa) => getOrElse_(fa, onNone) }