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) • 743 B
JavaScript
import { isNull, expectNotNull } from '../core/nullable.js';
import { ERR_MSG_TRANSFORMER_MUST_NOT_RETURN_NO_VAL_FOR_NULLABLE } from '../internal/error_message.js';
/**
* Zips _self_ and another `Nullable` with function _transformer_.
* If _self_ is `T` and _other_ is `U`, this method returns the result of _transformer_.
* Otherwise, `null` is returned.
*
* @throws {TypeError}
* Throws if the _transformer_ returns `null`.
*/
export function zipWithForNullable(self, other, transformer) {
if (isNull(self) || isNull(other)) {
return null;
}
const result = transformer(self, other);
const checked = expectNotNull(result, ERR_MSG_TRANSFORMER_MUST_NOT_RETURN_NO_VAL_FOR_NULLABLE);
return checked;
}