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>`.
39 lines (38 loc) • 1.12 kB
JavaScript
import { isNull } from '../nullable/nullable.js';
import { isUndefined } from '../undefinable/undefinable.js';
import { unsafeUnwrapValueInOkWithoutAnyCheck } from './internal/intrinsics_unsafe.js';
import { isErr, createOk } from './result.js';
/**
* Transposes a `Result` of an `Nullable<T>` into an `Nullable<T>` of a `Result`.
*
* - `Ok(T)` -> `Ok(T)`
* - `Ok(null)` -> `null`
* - `Err(E)` -> `Err(E)`
*/
export function transposeResultToNullable(input) {
if (isErr(input)) {
return input;
}
const inner = unsafeUnwrapValueInOkWithoutAnyCheck(input);
if (isNull(inner)) {
return null;
}
return createOk(inner);
}
/**
* Transposes a `Result` of an `Undefinable<T>` into an `Undefinable<T>` of a `Result`.
*
* - `Ok(T)` -> `Ok(T)`
* - `Ok(undefined)` -> `undefined`
* - `Err(E)` -> `Err(E)`
*/
export function transposeResultToUndefinable(input) {
if (isErr(input)) {
return input;
}
const inner = unsafeUnwrapValueInOkWithoutAnyCheck(input);
if (isUndefined(inner)) {
return undefined;
}
return createOk(inner);
}