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) • 787 B
JavaScript
import { isNotUndefined, expectNotUndefined, } from '../core/undefinable.js';
import { ERR_MSG_RECOVERER_MUST_NOT_RETURN_NO_VAL_FOR_UNDEFINABLE } from '../internal/error_message.js';
/**
* Return _input_ as `T` if the passed _input_ is not `undefined`.
* Otherwise, return the result of _recoverer_.
*
* * The result of _recoverer_ must not be `Undefinable<*>`.
* * If you try to recover the value, use `orElse()`
* * If the result of _recoverer_ is `undefined`, throw `TypeError`.
*/
export function unwrapOrElseForUndefinable(input, recoverer) {
if (isNotUndefined(input)) {
return input;
}
const fallback = recoverer();
const passed = expectNotUndefined(fallback, ERR_MSG_RECOVERER_MUST_NOT_RETURN_NO_VAL_FOR_UNDEFINABLE);
return passed;
}