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>`.

53 lines (52 loc) 2.36 kB
import { unsafeUnwrapValueInErrWithoutAnyCheck, unsafeUnwrapValueInOkWithoutAnyCheck, } from './internal/intrinsics_unsafe.js'; import { isErr, isOk } from './result.js'; /** * * Return _input_ directly. * * This value is passed as the input. But it maybe mutated by calling _effector_. * * Call _effector_ with the inner value of _input_ if _input_ is `Ok(T)`. * * This main purpose is to inspect an inner value in a chained function calling. * If you don't have to do it, you should not mutate the inner value. * if-else statement might be sufficient to mutate the inner value instead of calling this function. */ export function inspectOkForResult(input, effector) { if (isOk(input)) { const val = unsafeUnwrapValueInOkWithoutAnyCheck(input); effector(val); } return input; } /** * * Return _input_ directly. * * This value is passed as the input. But it maybe mutated by calling _effector_. * * Call _effector_ with the inner value of _input_ if _input_ is `Err(E)`. * * This main purpose is to inspect an inner value in a chained function calling. * If you don't have to do it, you should not mutate the inner value. * if-else statement might be sufficient to mutate the inner value instead of calling this function. */ export function inspectErrForResult(input, effector) { if (isErr(input)) { const err = unsafeUnwrapValueInErrWithoutAnyCheck(input); effector(err); } return input; } /** * * Return _input_ directly. * * This value is passed as the input. But it maybe mutated by calling _effector_. * * Call _okEffector_ with the inner value of _input_ if _input_ is `Ok(T)`. * Otherwise, call _errEffector_ with the inner value if _input_ is `Err(E)` * * This main purpose is to inspect an inner value in a chained function calling. * If you don't have to do it, you should not mutate the inner value. * if-else statement might be sufficient to mutate the inner value instead of calling this function. */ export function inspectBothForResult(input, okEffector, errEffector) { if (isOk(input)) { const val = unsafeUnwrapValueInOkWithoutAnyCheck(input); okEffector(val); } else { const err = unsafeUnwrapValueInErrWithoutAnyCheck(input); errEffector(err); } return input; }