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) • 679 B
JavaScript
import { isNotUndefined, expectNotUndefined, } from '../core/undefinable.js';
import { ERR_MSG_DEFAULT_VALUE_MUST_NOT_BE_NO_VAL_FOR_UNDEFINABLE } from '../internal/error_message.js';
/**
* Return _input_ as `T` if the passed _input_ is not `undefined`.
* Otherwise, return _defaultValue_.
*
* * _defaultValue_ must not be `Undefinable<*>`.
* * If the result of _defaultValue_ is `undefined`, throw `TypeError`.
*/
export function unwrapOrForUndefinable(input, defaultValue) {
if (isNotUndefined(input)) {
return input;
}
const passed = expectNotUndefined(defaultValue, ERR_MSG_DEFAULT_VALUE_MUST_NOT_BE_NO_VAL_FOR_UNDEFINABLE);
return passed;
}