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 (34 loc) 1.2 kB
import { none } from '../../optional/impl/optional-none.mjs'; import { some } from '../../optional/impl/optional-some.mjs'; import { isOk } from './result-is-ok.mjs'; import { unwrapOk } from './result-unwrap-ok.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`. */ const toOptional = (result) => isOk(result) ? some(unwrapOk(result)) : none; export { toOptional }; //# sourceMappingURL=result-to-optional.mjs.map