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>`.
19 lines (18 loc) • 689 B
JavaScript
import { createErr, createOk } from '../../plain_result/core/result.js';
import { isNotNullOrUndefined } from '../core/maybe.js';
/**
* Transforms the `Maybe<T>` into a `Result<T, E>` by mapping `T` to `Ok(T)`.
* If `input` is `undefined` or `null`, then return `Err(E)` with passed `err`.
*
* Arguments passed to this are eagerly evaluated;
* if you are passing the result of a function call, it is recommended to use okOrElse,
* which is lazily evaluated.
*/
export function okOrForMaybe(input, err) {
if (isNotNullOrUndefined(input)) {
const okWrapped = createOk(input);
return okWrapped;
}
const errWrapped = createErr(err);
return errWrapped;
}