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>`.

17 lines (16 loc) 677 B
import { ERR_MSG_DEFAULT_VALUE_MUST_NOT_BE_NO_VAL_FOR_MAYBE } from './internal/error_message.js'; import { isNotNullOrUndefined, expectNotNullOrUndefined, } from './maybe.js'; /** * Return _input_ as `T` if the passed _input_ is not `null` and `undefined`. * Otherwise, return _defaultValue_. * * * _defaultValue_ must not be `Maybe<*>`. * * If the _defaultValue_ is `null` or `undefined`, throw `TypeError`. */ export function unwrapOrForMaybe(input, defaultValue) { if (isNotNullOrUndefined(input)) { return input; } const passed = expectNotNullOrUndefined(defaultValue, ERR_MSG_DEFAULT_VALUE_MUST_NOT_BE_NO_VAL_FOR_MAYBE); return passed; }