UNPKG

ts-data-forge

Version:

[![npm version](https://img.shields.io/npm/v/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![npm downloads](https://img.shields.io/npm/dm/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![License](https://img.shields.

37 lines (35 loc) 1.19 kB
import { Optional } from '../../optional/index.mjs'; import { isOk } from './result-is-ok.mjs'; import { unwrapOk } from './result-unwrap-ok.mjs'; import { type UnwrapOk } from './types.mjs'; /** * Converts a `Result` to an `Optional`. * * This conversion is useful when you want to discard error information and * only care about whether an operation succeeded. The error information is * lost in this conversion, so use it when error details are not needed. * * If the `Result` is `Ok`, returns `Some` with the value. If the `Result` is * `Err`, returns `None`. * * @example * * ```ts * const okValue = Result.ok(7); * * const errValue = Result.err('fail'); * * assert.deepStrictEqual(Result.toOptional(okValue), Optional.some(7)); * * assert.deepStrictEqual(Result.toOptional(errValue), Optional.none); * ``` * * @template R The input `UnknownResult` type. * @param result The `Result` to convert. * @returns An `Optional<UnwrapOk<R>>` containing the success value or * representing `None`. */ export const toOptional = <R extends UnknownResult>( result: R, ): Optional<UnwrapOk<R>> => isOk(result) ? Optional.some(unwrapOk(result)) : Optional.none;