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) • 990 B
JavaScript
import { unsafeUnwrapValueInErrWithoutAnyCheck } from './internal/intrinsics_unsafe.js';
import { isOk } from './result.js';
/**
* Returns `true` if the _result_ is `Err<E>` and the value inside of it matches a _predicate_.
*/
// XXX:
// We cannot use `result is Err<E>` as the returned type here because of that
// it **does not mean** "result is not Err<E>" even if _predicate_ returns `false`.
export function isErrAndForResult(result, predicate) {
if (isOk(result)) {
return false;
}
const err = unsafeUnwrapValueInErrWithoutAnyCheck(result);
const ok = predicate(err);
return ok;
}
/**
* Returns `true` if the _result_ is `Ok<T>` and the value inside of it matches a _predicate_.
* Then _result_ would be `Err<F>`.
*
* Please use {@link isErrAndForResult} generally if you don't have to narrow the type.
*/
export function isErrAndWithEnsureTypeForResult(result, predicate) {
const ok = isErrAndForResult(result, predicate);
return ok;
}