UNPKG

option-t

Version:

A toolkit of Nullable/Option/Result type implementation in ECMAScript. Their APIs are inspired by Rust's `Option<T>` and `Result<T, E>`.

16 lines (15 loc) 563 B
import { createErr, createOk } from '../plain_result/result.js'; import { isNotUndefined } from './undefinable.js'; /** * Transforms the `Undefinable<T>` into a `Result<T, E>` by mapping `T` to `Ok(T)`. * If `input` is `undefined`, then returns `Err(E)` with the result of `recoverer()` */ export function okOrElseForUndefinable(input, recoverer) { if (isNotUndefined(input)) { const okWrapped = createOk(input); return okWrapped; } const fallback = recoverer(); const errWrapped = createErr(fallback); return errWrapped; }