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>`.
18 lines (17 loc) • 605 B
JavaScript
import { isNullOrUndefined } from '../core/maybe.js';
/**
* Returns `null` or `undefined` if the _input_ is `null` or `undefined`,
* otherwise calls _transformer_ with the value and returns the result.
*
* XXX:
* Some languages call this operation flatmap.
* But we don't provide `flatMap()` as alias of this function.
* because it's too hard to undarstand that "flatMap" operation for `T | null | undefined`.
*/
export function andThenForMaybe(input, transformer) {
if (isNullOrUndefined(input)) {
return input;
}
const result = transformer(input);
return result;
}