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) • 799 B
JavaScript
import { expectNotUndefined, isUndefined, } from '../core/undefinable.js';
import { ERR_MSG_TRANSFORMER_MUST_NOT_RETURN_NO_VAL_FOR_UNDEFINABLE } from '../internal/error_message.js';
/**
* Zips _self_ and another `Undefinable` 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 `undefined`.
*/
export function zipWithForUndefinable(self, other, transformer) {
if (isUndefined(self) || isUndefined(other)) {
return undefined;
}
const result = transformer(self, other);
const checked = expectNotUndefined(result, ERR_MSG_TRANSFORMER_MUST_NOT_RETURN_NO_VAL_FOR_UNDEFINABLE);
return checked;
}