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

27 lines (26 loc) 981 B
import { unsafeUnwrapValueInOkWithoutAnyCheck } from './internal/intrinsics_unsafe.js'; import { isErr } from './result.js'; /** * Returns `true` if the _result_ is `Ok<T>` and the value inside of it matches a _predicate_. */ // XXX: // We cannot use `result is Ok<T>` as the returned type here because of that // it **does not mean** "result is not Ok<T>" even if _predicate_ returns `false`. export function isOkAndForResult(result, predicate) { if (isErr(result)) { return false; } const val = unsafeUnwrapValueInOkWithoutAnyCheck(result); const ok = predicate(val); return ok; } /** * Returns `true` if the _result_ is `Ok<T>` and the value inside of it matches a _predicate_. * Then _result_ would be `Ok<U>`. * * Please use {@link isOkAndForResult} generally if you don't have to narrow the type. */ export function isOkAndWithEnsureTypeForResult(result, predicate) { const ok = isOkAndForResult(result, predicate); return ok; }