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>`.
28 lines (27 loc) • 1.15 kB
JavaScript
import { expectNotNull } from '../core/nullable.js';
import { ERR_MSG_TRANSFORMER_MUST_NOT_RETURN_NO_VAL_FOR_NULLABLE, ERR_MSG_DEFAULT_VALUE_MUST_NOT_BE_NO_VAL_FOR_NULLABLE, } from '../internal/error_message.js';
/**
* Return the result of _transformer_ with using _input_ as an argument for it if _input_ is not `null`.
* Otherwise, return _defaultValue_.
*
* Basically, this operation is a combination `map()` and `unwrapOr()`.
*
* * `U` must not be `Nullable<*>`.
* * If the result of _transformer_ is `null`, this throw an `Error`.
* * If the result of _defaultValue_ is `null`, this throw an `Error`.
* * If you'd like to accept `Nullable<*>` as `U`, use a combination `andThen()` and `or()`.
*/
export function mapOrForNullable(input, defaultValue, transformer) {
let result;
let msg = '';
if (input !== null) {
result = transformer(input);
msg = ERR_MSG_TRANSFORMER_MUST_NOT_RETURN_NO_VAL_FOR_NULLABLE;
}
else {
result = defaultValue;
msg = ERR_MSG_DEFAULT_VALUE_MUST_NOT_BE_NO_VAL_FOR_NULLABLE;
}
const passed = expectNotNull(result, msg);
return passed;
}