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>`.
14 lines (13 loc) • 462 B
JavaScript
import { isOk } from '../core/result.js';
import { unsafeUnwrapValueInOkWithoutAnyCheck } from '../internal/intrinsics_unsafe.js';
/**
* Unwraps a result _input_, returns the content of an `Ok(T)`.
* If the value is an `Err(E)` then return _defaultValue_.
*/
export function unwrapOrForResult(input, defaultValue) {
if (isOk(input)) {
const val = unsafeUnwrapValueInOkWithoutAnyCheck(input);
return val;
}
return defaultValue;
}