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) • 616 B
JavaScript
import { ERR_MSG_DEFAULT_VALUE_MUST_NOT_BE_NO_VAL_FOR_NULLABLE } from './internal/error_message.js';
import { isNotNull, expectNotNull } from './nullable.js';
/**
* Return _input_ as `T` if the passed _input_ is not `null`.
* Otherwise, return _defaultValue_.
*
* * _defaultValue_ must not be `Nullable<*>`.
* * If the _defaultValue_ is `null`, throw `TypeError`.
*/
export function unwrapOrForNullable(input, defaultValue) {
if (isNotNull(input)) {
return input;
}
const passed = expectNotNull(defaultValue, ERR_MSG_DEFAULT_VALUE_MUST_NOT_BE_NO_VAL_FOR_NULLABLE);
return passed;
}