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>`.
18 lines (17 loc) • 744 B
JavaScript
import { isNotNullOrUndefined, expectNotNullOrUndefined, } from '../core/maybe.js';
import { ERR_MSG_RECOVERER_MUST_NOT_RETURN_NO_VAL_FOR_MAYBE } from '../internal/error_message.js';
/**
* Return _input_ as `T` if the passed _input_ is not `null` and `undefined`.
* Otherwise, return the result of _recoverer_.
*
* * The result of _recoverer_ must not be `Maybe<*>`.
* * If the result of _recoverer_ is `null` or `undefined`, throw `TypeError`.
*/
export function unwrapOrElseForMaybe(input, recoverer) {
if (isNotNullOrUndefined(input)) {
return input;
}
const fallback = recoverer();
const passed = expectNotNullOrUndefined(fallback, ERR_MSG_RECOVERER_MUST_NOT_RETURN_NO_VAL_FOR_MAYBE);
return passed;
}