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>`.
14 lines (13 loc) • 484 B
JavaScript
/**
* Maps a `Option<T>` to `U` by applying _transformer_ to a contained `Some(T)` value in _input_,
* or a _recoverer_ function to a contained `None` value in _input_.
* This function can be used to unpack a successful result while handling an error.
*/
export function mapOrElseForOption(input, recoverer, transformer) {
if (!input.ok) {
const fallback = recoverer();
return fallback;
}
const result = transformer(input.val);
return result;
}