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>`.
23 lines (22 loc) • 1.22 kB
JavaScript
import { isUndefined, expectNotUndefined, } from '../core/undefinable.js';
import { ERR_MSG_TRANSFORMER_MUST_NOT_RETURN_NO_VAL_FOR_UNDEFINABLE, ERR_MSG_DEFAULT_VALUE_MUST_NOT_BE_NO_VAL_FOR_UNDEFINABLE, } from '../internal/error_message.js';
/**
* Return the result of _transformer_ with using _input_ as an argument for it if _input_ is not `undefined`.
* Otherwise, return _defaultValue_.
*
* Basically, this operation is a combination `map()` and `unwrapOr()`.
*
* * `U` must not be `Undefinable<*>`.
* * If the result of _transformer_ is `undefined`, this throw an `Error`.
* * If the result of _defaultValue_ is `undefined`, this throw an `Error`.
* * If you'd like to accept `Undefinable<*>` as `U`, use a combination `andThen()` and `or()`.
*/
export async function mapOrAsyncForUndefinable(input, defaultValue, transformer) {
if (isUndefined(input)) {
const nonNullDefault = expectNotUndefined(defaultValue, ERR_MSG_DEFAULT_VALUE_MUST_NOT_BE_NO_VAL_FOR_UNDEFINABLE);
return nonNullDefault;
}
const result = await transformer(input);
const checked = expectNotUndefined(result, ERR_MSG_TRANSFORMER_MUST_NOT_RETURN_NO_VAL_FOR_UNDEFINABLE);
return checked;
}