UNPKG

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) 753 B
import { ERR_MSG_TRANSFORMER_MUST_NOT_RETURN_NO_VAL_FOR_NULLABLE } from './internal/error_message.js'; import { isNull, expectNotNull } from './nullable.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 async function zipWithAsyncForNullable(self, other, transformer) { if (isNull(self) || isNull(other)) { return null; } const result = await transformer(self, other); const checked = expectNotNull(result, ERR_MSG_TRANSFORMER_MUST_NOT_RETURN_NO_VAL_FOR_NULLABLE); return checked; }