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>`.
19 lines (18 loc) • 809 B
JavaScript
import { isNullOrUndefined, expectNotNullOrUndefined, } from '../core/maybe.js';
import { ERR_MSG_TRANSFORMER_MUST_NOT_RETURN_NO_VAL_FOR_MAYBE } from '../internal/error_message.js';
/**
* Zips _self_ and another `Maybe` with function _transformer_.
* If _self_ is `T` and _other_ is `U`, this method returns the result of _transformer_.
* Otherwise, `undefined` is returned.
*
* @throws {TypeError}
* Throws if the _transformer_ returns `null` or `undefined`.
*/
export function zipWithForMaybe(self, other, transformer) {
if (isNullOrUndefined(self) || isNullOrUndefined(other)) {
return undefined;
}
const result = transformer(self, other);
const checked = expectNotNullOrUndefined(result, ERR_MSG_TRANSFORMER_MUST_NOT_RETURN_NO_VAL_FOR_MAYBE);
return checked;
}