From 630e1d693bc6719da9be9095dfd45dbf2b7657ce Mon Sep 17 00:00:00 2001 From: Giulio Canti Date: Mon, 16 Feb 2026 14:47:57 +0100 Subject: [PATCH] Result: tests + type-level tests + jsdocs (#1248) --- packages/effect/dtslint/Result.tst.ts | 353 ++++++++ packages/effect/src/Result.ts | 1204 +++++++++++++++---------- packages/effect/src/index.ts | 66 ++ packages/effect/test/Result.test.ts | 117 ++- 4 files changed, 1260 insertions(+), 480 deletions(-) create mode 100644 packages/effect/dtslint/Result.tst.ts diff --git a/packages/effect/dtslint/Result.tst.ts b/packages/effect/dtslint/Result.tst.ts new file mode 100644 index 0000000000..bd9046f59e --- /dev/null +++ b/packages/effect/dtslint/Result.tst.ts @@ -0,0 +1,353 @@ +import { Array, hole, Option, pipe, Predicate, Result } from "effect" +import { describe, expect, it } from "tstyche" + +declare const string$string: Result.Result +declare const number$string: Result.Result +declare const boolean$string: Result.Result +declare const boolean$Error: Result.Result +declare const literal$Error: Result.Result<"a", Error> + +describe("Result", () => { + it("flip", () => { + expect(Result.flip(number$string)).type.toBe>() + expect(pipe(number$string, Result.flip)).type.toBe>() + }) + + it("try", () => { + expect(Result.try(() => 1)).type.toBe>() + expect(Result.try({ try: () => 1, catch: () => new Error() })).type.toBe>() + }) + + describe("all", () => { + it("tuple", () => { + expect(Result.all([])).type.toBe>() + expect(Result.all([number$string])).type.toBe>() + expect(Result.all([number$string, boolean$string])).type.toBe>() + expect(Result.all([number$string, boolean$Error])).type.toBe>() + expect(pipe([number$string, boolean$string] as const, Result.all)).type.toBe< + Result.Result<[number, boolean], string> + >() + expect(pipe([number$string, boolean$Error] as const, Result.all)).type.toBe< + Result.Result<[number, boolean], string | Error> + >() + }) + + it("struct", () => { + expect(Result.all({})).type.toBe>() + expect(Result.all({ a: number$string })).type.toBe>() + expect(Result.all({ a: number$string, b: boolean$string })).type.toBe< + Result.Result<{ a: number; b: boolean }, string> + >() + expect(Result.all({ a: number$string, b: boolean$Error })).type.toBe< + Result.Result<{ a: number; b: boolean }, string | Error> + >() + expect(pipe({ a: number$string, b: boolean$string }, Result.all)).type.toBe< + Result.Result<{ a: number; b: boolean }, string> + >() + expect(pipe({ a: number$string, b: boolean$Error }, Result.all)).type.toBe< + Result.Result<{ a: number; b: boolean }, string | Error> + >() + }) + + it("array", () => { + const resultArray = hole>>() + expect(Result.all(resultArray)).type.toBe, string>>() + expect(pipe(resultArray, Result.all)).type.toBe, string>>() + }) + + it("record", () => { + const resultRecord = hole>>() + expect(Result.all(resultRecord)).type.toBe>() + expect(pipe(resultRecord, Result.all)).type.toBe>() + }) + }) + + it("andThen", () => { + expect(Result.andThen(string$string, number$string)).type.toBe>() + expect(string$string.pipe(Result.andThen(number$string))).type.toBe>() + + expect(Result.andThen(string$string, () => number$string)).type.toBe>() + expect(string$string.pipe(Result.andThen(() => number$string))).type.toBe>() + }) + + it("liftPredicate", () => { + const primitiveNumber = hole() + const stringOrNumber = hole() + const predicateNumberOrString = hole>() + const refinementNumberOrStringToNumber = hole>() + + expect( + Result.liftPredicate(predicateNumberOrString, (sn) => { + expect(sn).type.toBe() + return "b" as const + }) + ).type.toBe<(a: string | number) => Result.Result>() + expect( + Result.liftPredicate(refinementNumberOrStringToNumber, (sn) => { + expect(sn).type.toBe() + return "b" as const + }) + ).type.toBe<(a: string | number) => Result.Result>() + + expect( + Result.liftPredicate( + stringOrNumber, + (sn): sn is number => { + expect(sn).type.toBe() + return typeof sn === "number" + }, + (sn) => { + expect(sn).type.toBe() + return "b" as const + } + ) + ).type.toBe>() + expect( + pipe( + stringOrNumber, + Result.liftPredicate( + (sn): sn is number => { + expect(sn).type.toBe() + return typeof sn === "number" + }, + (sn) => { + expect(sn).type.toBe() + return "b" as const + } + ) + ) + ).type.toBe>() + + expect( + Result.liftPredicate(stringOrNumber, predicateNumberOrString, (sn) => { + expect(sn).type.toBe() + return "b" as const + }) + ).type.toBe>() + expect( + pipe( + stringOrNumber, + Result.liftPredicate(predicateNumberOrString, (sn) => { + expect(sn).type.toBe() + return "b" as const + }) + ) + ).type.toBe>() + + expect( + Result.liftPredicate(primitiveNumber, predicateNumberOrString, (sn) => { + expect(sn).type.toBe() + return "b" as const + }) + ).type.toBe>() + expect( + pipe( + primitiveNumber, + Result.liftPredicate(predicateNumberOrString, (sn) => { + expect(sn).type.toBe() + return "b" as const + }) + ) + ).type.toBe>() + }) + + it("fromNullishOr", () => { + const nullableString = hole() + const nullableObject = hole<{ a: string } | undefined>() + + expect( + Result.fromNullishOr( + nullableString, + () => new Error() + ) + ).type.toBe>() + + expect( + pipe( + nullableString, + Result.fromNullishOr(() => new Error()) + ) + ).type.toBe>() + + expect( + Result.fromNullishOr(nullableObject, () => new Error()) + ).type.toBe>() + + expect( + pipe( + nullableObject, + Result.fromNullishOr(() => new Error()) + ) + ).type.toBe< + Result.Result<{ a: string }, Error> + >() + }) + + it("filterOrFail", () => { + const predicateUnknown = hole>() + + const arrayOfStrings$Error = hole, Error>>() + expect( + Result.filterOrFail(arrayOfStrings$Error, Array.isArrayNonEmpty, (ss) => { + expect(ss).type.toBe>() + return "b" as const + }) + ).type.toBe], "b" | Error>>() + expect( + pipe( + arrayOfStrings$Error, + Result.filterOrFail(Array.isArrayNonEmpty, (ss) => { + expect(ss).type.toBe>() + return "b" as const + }) + ) + ).type.toBe], "b" | Error>>() + + const readonlyArrayOfStrings$Error = hole, Error>>() + expect( + Result.filterOrFail(readonlyArrayOfStrings$Error, Array.isReadonlyArrayNonEmpty, (ss) => { + expect(ss).type.toBe>() + return "b" as const + }) + ).type.toBe], "b" | Error>>() + expect( + pipe( + readonlyArrayOfStrings$Error, + Result.filterOrFail(Array.isReadonlyArrayNonEmpty, (ss) => { + expect(ss).type.toBe>() + return "b" as const + }) + ) + ).type.toBe], "b" | Error>>() + + // @tstyche fixme -- This doesn't work but it should + expect( + Result.filterOrFail(literal$Error, Predicate.isString, (a) => { + // @tstyche fixme -- This doesn't work but it should + expect(a).type.toBe<"a">() + return "b" as const + }) + ).type.toBe>() + expect( + pipe( + literal$Error, + Result.filterOrFail(Predicate.isString, (a) => { + expect(a).type.toBe<"a">() + return "b" as const + }) + ) + ).type.toBe>() + + // @tstyche fixme -- This doesn't work but it should + expect( + Result.filterOrFail(literal$Error, Predicate.isString, (_s: string) => "b" as const) + ).type.toBe>() + expect( + pipe( + literal$Error, + Result.filterOrFail(Predicate.isString, (_s: string) => "b" as const) + ) + ).type.toBe>() + + expect( + Result.filterOrFail(literal$Error, predicateUnknown, (a) => { + expect(a).type.toBe<"a">() + return "b" as const + }) + ).type.toBe>() + expect( + pipe( + literal$Error, + Result.filterOrFail(predicateUnknown, (a) => { + expect(a).type.toBe<"a">() + return "b" as const + }) + ) + ).type.toBe>() + + expect( + Result.filterOrFail(literal$Error, predicateUnknown, (_s: string) => "b" as const) + ).type.toBe>() + expect( + pipe( + literal$Error, + Result.filterOrFail(predicateUnknown, (_s: string) => "b" as const) + ) + ).type.toBe>() + }) + + it("type level helpers", () => { + type S = Result.Result.Success + type F = Result.Result.Failure + expect().type.toBe() + expect().type.toBe() + }) + + it("do notation", () => { + expect( + pipe( + Result.Do, + Result.bind("a", (scope) => { + expect(scope).type.toBe<{}>() + return Result.succeed(1) + }), + Result.bind("b", (scope) => { + expect(scope).type.toBe<{ a: number }>() + return Result.succeed("b") + }), + Result.let("c", (scope) => { + expect(scope).type.toBe<{ a: number; b: string }>() + return true + }) + ) + ).type.toBe>() + + expect( + pipe( + Result.succeed(1), + Result.bindTo("a"), + Result.bind("b", (scope) => { + expect(scope).type.toBe<{ a: number }>() + return Result.succeed("b") + }), + Result.let("c", (scope) => { + expect(scope).type.toBe<{ a: number; b: string }>() + return true + }) + ) + ).type.toBe>() + }) +}) + +it("transposeMapOption", () => { + expect(Result.transposeMapOption(Option.none(), (value) => { + expect(value).type.toBe() + return string$string + })).type.toBe< + Result.Result, string> + >() + expect(pipe( + Option.none(), + Result.transposeMapOption((value) => { + expect(value).type.toBe() + return string$string + }) + )).type.toBe< + Result.Result, string> + >() + expect(Result.transposeMapOption(Option.some(42), (value) => { + expect(value).type.toBe() + return string$string + })).type.toBe< + Result.Result, string> + >() + expect(pipe( + Option.some(42), + Result.transposeMapOption((value) => { + expect(value).type.toBe() + return string$string + }) + )).type.toBe< + Result.Result, string> + >() +}) diff --git a/packages/effect/src/Result.ts b/packages/effect/src/Result.ts index 7a7b33a1a1..ddc5e23439 100644 --- a/packages/effect/src/Result.ts +++ b/packages/effect/src/Result.ts @@ -1,4 +1,70 @@ /** + * A synchronous, pure type for representing computations that can succeed + * (`Success`) or fail (`Failure`). Unlike `Effect`, `Result` is + * evaluated eagerly and carries no side effects. + * + * **Mental model** + * + * - `Result` is a discriminated union: `Success | Failure` + * - `Success` wraps a value of type `A`, accessed via `.success` + * - `Failure` wraps an error of type `E`, accessed via `.failure` + * - `Result` is a monad: chain operations with {@link flatMap}, compose pipelines with `pipe` + * - All operations are pure and return new `Result` values; the input is never mutated + * - `Result` is yieldable in `Effect.gen`, producing the inner value or short-circuiting on failure + * + * **Common tasks** + * + * - Create from a value: {@link succeed}, {@link fail} + * - Create from nullable: {@link fromNullishOr} + * - Create from Option: {@link fromOption} + * - Create from throwing code: {@link try_ try} + * - Create from predicate: {@link liftPredicate} + * - Transform: {@link map}, {@link mapError}, {@link mapBoth} + * - Unwrap: {@link getOrElse}, {@link getOrNull}, {@link getOrUndefined}, {@link getOrThrow} + * - Pattern match: {@link match} + * - Sequence: {@link flatMap}, {@link andThen}, {@link all} + * - Recover: {@link orElse} + * - Filter: {@link filterOrFail} + * - Convert to Option: {@link getSuccess}, {@link getFailure} + * - Generator syntax: {@link gen} + * - Do notation: {@link Do}, {@link bind}, {@link let_ let} + * - Check variant: {@link isResult}, {@link isSuccess}, {@link isFailure} + * + * **Gotchas** + * + * - `E` defaults to `never`, so `Result` means a result that cannot fail + * - {@link andThen} accepts a `Result`, a function returning a `Result`, a plain value, or a function returning a plain value; {@link flatMap} only accepts a function returning a `Result` + * - {@link all} short-circuits on the first `Failure` and returns it; later elements are not inspected + * - {@link getOrThrow} throws the raw failure value `E`; use {@link getOrThrowWith} for custom error objects + * - {@link tap} runs a side-effect but does not change the result; its return value is ignored + * + * **Quickstart** + * + * **Example** (Parsing and validating with Result) + * + * ```ts + * import { Result } from "effect" + * + * const parse = (input: string): Result.Result => + * isNaN(Number(input)) + * ? Result.fail("not a number") + * : Result.succeed(Number(input)) + * + * const ensurePositive = (n: number): Result.Result => + * n > 0 ? Result.succeed(n) : Result.fail("not positive") + * + * const result = Result.flatMap(parse("42"), ensurePositive) + * + * console.log(Result.getOrElse(result, (err) => `Error: ${err}`)) + * // Output: 42 + * ``` + * + * **See also** + * + * - {@link succeed} / {@link fail} to create values + * - {@link match} to fold both branches + * - {@link gen} for generator-based composition + * * @since 4.0.0 */ @@ -22,44 +88,63 @@ import * as Gen from "./Utils.ts" const TypeId = "~effect/data/Result" /** - * Represents a computation that can either succeed with a value of type `A` or fail with an error of type `E`. + * A value that is either `Success` or `Failure`. + * + * - Use {@link succeed} / {@link fail} to construct + * - Use {@link match} to fold both branches + * - Use {@link isSuccess} / {@link isFailure} to narrow the type + * + * `E` defaults to `never`, so `Result` means a result that cannot fail. + * + * **Example** (Creating and matching a Result) * - * @example * ```ts * import { Result } from "effect" * - * // Create a successful result * const success = Result.succeed(42) - * - * // Create a failed result * const failure = Result.fail("something went wrong") * - * // Pattern match on the result * const message = Result.match(success, { * onSuccess: (value) => `Success: ${value}`, * onFailure: (error) => `Error: ${error}` * }) + * console.log(message) + * // Output: "Success: 42" * ``` * + * @see {@link succeed} / {@link fail} to create values + * @see {@link match} to fold both branches + * @see {@link isSuccess} / {@link isFailure} for type guards + * * @category Models * @since 4.0.0 */ export type Result = Success | Failure /** - * Represents a failed computation with an error of type `E`. + * The failure variant of {@link Result}. Wraps an error of type `E`. + * + * - Access the error via the `.failure` property + * - Use {@link isFailure} to narrow a `Result` to `Failure` + * - Create with {@link fail} + * + * **Example** (Accessing the failure value) * - * @example * ```ts * import { Result } from "effect" * * const failure = Result.fail("Network error") * * if (Result.isFailure(failure)) { - * console.log(failure.failure) // "Network error" + * console.log(failure.failure) + * // Output: "Network error" * } * ``` * + * @see {@link fail} to create a Failure + * @see {@link isFailure} to narrow the type + * @see {@link Success} for the other variant + * * @category Models * @since 4.0.0 */ @@ -77,19 +162,29 @@ export interface Failure extends Pipeable, Inspectable, Yieldable< } /** - * Represents a successful computation with a value of type `A`. + * The success variant of {@link Result}. Wraps a value of type `A`. + * + * - Access the value via the `.success` property + * - Use {@link isSuccess} to narrow a `Result` to `Success` + * - Create with {@link succeed} + * + * **Example** (Accessing the success value) * - * @example * ```ts * import { Result } from "effect" * * const success = Result.succeed(42) * * if (Result.isSuccess(success)) { - * console.log(success.success) // 42 + * console.log(success.success) + * // Output: 42 * } * ``` * + * @see {@link succeed} to create a Success + * @see {@link isSuccess} to narrow the type + * @see {@link Failure} for the other variant + * * @category Models * @since 4.0.0 */ @@ -107,17 +202,10 @@ export interface Success extends Pipeable, Inspectable, Yieldable< } /** - * Type-level utility for unifying Result types in generic contexts. + * Type-level utility for unifying `Result` types in generic contexts. * - * @example - * ```ts - * import { Result } from "effect" - * - * // This interface helps TypeScript unify different Result types - * const stringResult = Result.succeed("hello") - * const numberResult = Result.succeed(42) - * // These can be unified in generic contexts - * ``` + * This is an internal interface used by the Effect type system. You typically + * do not need to reference it directly. * * @category Models * @since 4.0.0 @@ -127,16 +215,10 @@ export interface ResultUnify { } /** - * Marker interface for ignoring unification in Result types. + * Marker interface for ignoring unification in `Result` types. * - * @example - * ```ts - * import { Result } from "effect" - * - * // This interface is used internally by the type system - * // to control when Result types should not be unified - * const result = Result.succeed("hello") - * ``` + * This is an internal interface used by the Effect type system. You typically + * do not need to reference it directly. * * @category Models * @since 4.0.0 @@ -144,16 +226,11 @@ export interface ResultUnify { export interface ResultUnifyIgnore {} /** - * Higher-kinded type representation for Result. + * Higher-kinded type representation for `Result`. * - * @example - * ```ts - * import type { Result } from "effect" - * - * // This interface allows Result to work with higher-kinded type utilities - * declare const resultTypeLambda: Result.ResultTypeLambda - * // Used for higher-kinded type operations - * ``` + * Used internally to integrate `Result` with generic type-class utilities + * (e.g., `map`, `flatMap` abstractions). You typically do not need to + * reference this directly. * * @category Type Lambdas * @since 4.0.0 @@ -163,16 +240,21 @@ export interface ResultTypeLambda extends TypeLambda { } /** - * Namespace containing type-level utilities for working with Result types. + * Namespace containing type-level utilities for extracting the inner types + * of a `Result`. + * + * **Example** (Extracting inner types) * - * @example * ```ts - * import { Result } from "effect" + * import type { Result } from "effect" * - * // This namespace contains type-level utilities - * const stringResult = Result.succeed("hello") - * const numberResult = Result.fail(404) - * // Used for extracting types at compile time + * type R = Result.Result + * + * // number + * type A = Result.Result.Success + * + * // string + * type E = Result.Result.Failure * ``` * * @category Type Level @@ -180,32 +262,14 @@ export interface ResultTypeLambda extends TypeLambda { */ export declare namespace Result { /** - * Extracts the failure type from a Result type. - * - * @example - * ```ts - * import { Result } from "effect" - * - * // This type utility extracts the failure type - * const errorResult = Result.fail("error") - * // Used for type-level operations - * ``` + * Extracts the failure type `E` from `Result`. * * @since 4.0.0 * @category Type Level */ export type Failure> = [T] extends [Result] ? _E : never /** - * Extracts the success type from a Result type. - * - * @example - * ```ts - * import { Result } from "effect" - * - * // This type utility extracts the success type - * const successResult = Result.succeed(42) - * // Used for type-level operations - * ``` + * Extracts the success type `A` from `Result`. * * @since 4.0.0 * @category Type Level @@ -214,40 +278,52 @@ export declare namespace Result { } /** - * Constructs a new `Result` holding a `Success` value. + * Creates a `Result` holding a `Success` value. + * + * - Use when you have a value and want to lift it into the `Result` type + * - The error type `E` defaults to `never` + * - Does not mutate input; allocates a new `Success` wrapper + * + * **Example** (Wrapping a value) * - * @example * ```ts * import { Result } from "effect" * * const result = Result.succeed(42) * - * console.log(Result.isSuccess(result)) // true - * if (Result.isSuccess(result)) { - * console.log(result.success) // 42 - * } + * console.log(Result.isSuccess(result)) + * // Output: true * ``` * + * @see {@link fail} to create a Failure + * @see {@link void} for a pre-built `Success` + * * @category Constructors * @since 4.0.0 */ export const succeed: (right: A) => Result = result.succeed /** - * Constructs a new `Result` holding a `Failure` value. + * Creates a `Result` holding a `Failure` value. + * + * - Use when you want to represent a failed computation + * - The success type `A` defaults to `never` + * - Does not mutate input; allocates a new `Failure` wrapper + * + * **Example** (Creating a failure) * - * @example * ```ts * import { Result } from "effect" * * const result = Result.fail("Something went wrong") * - * console.log(Result.isFailure(result)) // true - * if (Result.isFailure(result)) { - * console.log(result.failure) // "Something went wrong" - * } + * console.log(Result.isFailure(result)) + * // Output: true * ``` * + * @see {@link succeed} to create a Success + * @see {@link mapError} to transform the error + * * @category Constructors * @since 4.0.0 */ @@ -256,17 +332,24 @@ export const fail: (left: E) => Result = result.fail const void_: Result = succeed(void 0) export { /** - * Constructs a new `Result` holding a `Success` value with `void`. + * A pre-built `Result` holding `undefined` as its success value. + * + * - Use when you need a `Result` that represents "completed with no meaningful value" + * - Equivalent to `Result.succeed(undefined)` but avoids an extra allocation + * + * **Example** (Using void result) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * const result = Result.void - * assert.deepStrictEqual(result, Result.succeed(undefined)) + * const result: Result.Result = Result.void + * + * console.log(Result.isSuccess(result)) + * // Output: true * ``` * + * @see {@link succeed} + * * @category Constructors * @since 4.0.0 */ @@ -274,25 +357,28 @@ export { } /** - * Takes a lazy default and a nullish value, if the value is not nully (`null` - * or `undefined`), turn it into a `Success`, if the value is nully use the - * provided default as a `Failure`. + * Converts a possibly `null` or `undefined` value into a `Result`. + * + * - Non-nullish values become `Success>` + * - `null` or `undefined` becomes `Failure` using the provided function + * - Supports both data-first and data-last (piped) usage + * - The `onNullish` callback receives the original value + * + * **Example** (Handling nullable values) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * assert.deepStrictEqual( - * Result.fromNullishOr(1, () => "fallback"), - * Result.succeed(1) - * ) - * assert.deepStrictEqual( - * Result.fromNullishOr(null, () => "fallback"), - * Result.fail("fallback") - * ) + * console.log(Result.fromNullishOr(1, () => "fallback")) + * // Output: { _tag: "Success", success: 1, ... } + * + * console.log(Result.fromNullishOr(null, () => "fallback")) + * // Output: { _tag: "Failure", failure: "fallback", ... } * ``` * + * @see {@link fromOption} to convert from an Option + * @see {@link succeed} / {@link fail} for direct construction + * * @category Constructors * @since 4.0.0 */ @@ -306,21 +392,29 @@ export const fromNullishOr: { ) /** - * @example + * Converts an `Option` into a `Result`. + * + * - `Some` becomes `Success` + * - `None` becomes `Failure` using the provided function + * - Supports both data-first and data-last (piped) usage + * + * **Example** (Converting an Option to a Result) + * * ```ts * import { Option, Result } from "effect" - * import * as assert from "node:assert" * - * assert.deepStrictEqual( - * Result.fromOption(Option.some(1), () => "error"), - * Result.succeed(1) - * ) - * assert.deepStrictEqual( - * Result.fromOption(Option.none(), () => "error"), - * Result.fail("error") - * ) + * const some = Result.fromOption(Option.some(1), () => "missing") + * console.log(some) + * // Output: { _tag: "Success", success: 1, ... } + * + * const none = Result.fromOption(Option.none(), () => "missing") + * console.log(none) + * // Output: { _tag: "Failure", failure: "missing", ... } * ``` * + * @see {@link getSuccess} / {@link getFailure} to convert back to Option + * @see {@link fromNullishOr} to convert from nullable values + * * @category Constructors * @since 4.0.0 */ @@ -360,21 +454,33 @@ const try_: { export { /** - * Imports a synchronous side-effect into a pure `Result` value, translating any - * thrown exceptions into typed failed Results creating with `Failure`. + * Wraps a synchronous computation that may throw into a `Result`. + * + * - If the function returns normally, the result is `Success` + * - If the function throws, the exception is caught and becomes `Failure` + * - With a single function argument, the error type is `unknown` + * - With `{ try, catch }` options, the `catch` function maps the thrown value to `E` + * + * **Example** (Catching JSON parse errors) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * const success = Result.try(() => JSON.parse("{\"name\": \"John\"}")) - * assert.deepStrictEqual(success, Result.succeed({ name: "John" })) + * const ok = Result.try(() => JSON.parse('{"name": "Alice"}')) + * console.log(ok) + * // Output: { _tag: "Success", success: { name: "Alice" }, ... } * - * const failure = Result.try(() => JSON.parse("invalid json")) - * assert.deepStrictEqual(Result.isFailure(failure), true) + * const err = Result.try({ + * try: () => JSON.parse("not json"), + * catch: (e) => `Parse failed: ${e}` + * }) + * console.log(Result.isFailure(err)) + * // Output: true * ``` * + * @see {@link succeed} / {@link fail} for direct construction + * @see {@link fromNullishOr} for nullable values + * * @category Constructors * @since 4.0.0 */ @@ -382,119 +488,161 @@ export { } /** - * Tests if a value is a `Result`. + * Tests whether a value is a `Result` (either `Success` or `Failure`). + * + * - Use to validate unknown input before operating on it as a `Result` + * - Returns `true` for both `Success` and `Failure` variants + * - Acts as a TypeScript type guard, narrowing to `Result` + * + * **Example** (Checking if a value is a Result) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * assert.deepStrictEqual(Result.isResult(Result.succeed(1)), true) - * assert.deepStrictEqual(Result.isResult(Result.fail("a")), true) - * assert.deepStrictEqual(Result.isResult({ value: 1 }), false) + * console.log(Result.isResult(Result.succeed(1))) + * // Output: true + * + * console.log(Result.isResult({ value: 1 })) + * // Output: false * ``` * + * @see {@link isSuccess} / {@link isFailure} to narrow to a specific variant + * * @category Type Guards * @since 4.0.0 */ export const isResult: (input: unknown) => input is Result = result.isResult /** - * Determine if a `Result` is a `Failure`. + * Checks whether a `Result` is a `Failure`. + * + * - Acts as a TypeScript type guard, narrowing to `Failure` + * - After narrowing, you can access `.failure` to read the error value + * + * **Example** (Narrowing to Failure) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * assert.deepStrictEqual(Result.isFailure(Result.succeed(1)), false) - * assert.deepStrictEqual(Result.isFailure(Result.fail("a")), true) + * const result = Result.fail("oops") + * + * if (Result.isFailure(result)) { + * console.log(result.failure) + * // Output: "oops" + * } * ``` * + * @see {@link isSuccess} for the opposite check + * @see {@link isResult} to check if a value is any Result + * * @category Type Guards * @since 4.0.0 */ export const isFailure: (self: Result) => self is Failure = result.isFailure /** - * Determine if a `Result` is a `Success`. + * Checks whether a `Result` is a `Success`. + * + * - Acts as a TypeScript type guard, narrowing to `Success` + * - After narrowing, you can access `.success` to read the value + * + * **Example** (Narrowing to Success) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * assert.deepStrictEqual(Result.isSuccess(Result.succeed(1)), true) - * assert.deepStrictEqual(Result.isSuccess(Result.fail("a")), false) + * const result = Result.succeed(42) + * + * if (Result.isSuccess(result)) { + * console.log(result.success) + * // Output: 42 + * } * ``` * + * @see {@link isFailure} for the opposite check + * @see {@link isResult} to check if a value is any Result + * * @category Type Guards * @since 4.0.0 */ export const isSuccess: (self: Result) => self is Success = result.isSuccess /** - * Converts a `Result` to an `Option` discarding the `Failure`. + * Extracts the success value as an `Option`, discarding the failure. + * + * - `Success` becomes `Some` + * - `Failure` becomes `None` + * - Use when you only care about the success case and want to discard error info + * + * **Example** (Extracting the success as an Option) * - * @example * ```ts * import { Option, Result } from "effect" - * import * as assert from "node:assert" * - * assert.deepStrictEqual( - * Result.getSuccess(Result.succeed("ok")), - * Option.some("ok") - * ) - * assert.deepStrictEqual(Result.getSuccess(Result.fail("err")), Option.none()) + * console.log(Result.getSuccess(Result.succeed("ok"))) + * // Output: { _tag: "Some", value: "ok" } + * + * console.log(Result.getSuccess(Result.fail("err"))) + * // Output: { _tag: "None" } * ``` * + * @see {@link getFailure} to extract the error instead + * @see {@link fromOption} for the reverse conversion + * * @category Getters * @since 4.0.0 */ export const getSuccess: (self: Result) => Option = result.getSuccess /** - * Converts a `Result` to an `Option` discarding the `Success`. + * Extracts the failure value as an `Option`, discarding the success. + * + * - `Failure` becomes `Some` + * - `Success` becomes `None` + * - Use when you only care about the error case + * + * **Example** (Extracting the failure as an Option) * - * @example * ```ts * import { Option, Result } from "effect" - * import * as assert from "node:assert" * - * assert.deepStrictEqual(Result.getFailure(Result.succeed("ok")), Option.none()) - * assert.deepStrictEqual( - * Result.getFailure(Result.fail("err")), - * Option.some("err") - * ) + * console.log(Result.getFailure(Result.succeed("ok"))) + * // Output: { _tag: "None" } + * + * console.log(Result.getFailure(Result.fail("err"))) + * // Output: { _tag: "Some", value: "err" } * ``` * + * @see {@link getSuccess} to extract the success instead + * @see {@link fromOption} for the reverse conversion + * * @category Getters * @since 4.0.0 */ export const getFailure: (self: Result) => Option = result.getFailure /** - * Returns an `Equivalence` for comparing two `Result` values. + * Creates an `Equivalence` for comparing two `Result` values. * - * **Example** + * - Two `Success` values are equal when the `success` equivalence says so + * - Two `Failure` values are equal when the `failure` equivalence says so + * - A `Success` and a `Failure` are never equal + * + * **Example** (Comparing Results for equality) * * ```ts * import { Equivalence, Result } from "effect" * - * const stringEquivalence = Equivalence.strictEqual() - * const numberEquivalence = Equivalence.strictEqual() - * - * const resultEquivalence = Result.makeEquivalence( - * numberEquivalence, - * stringEquivalence + * const eq = Result.makeEquivalence( + * Equivalence.strictEqual(), + * Equivalence.strictEqual() * ) * - * console.log(resultEquivalence(Result.succeed(1), Result.succeed(1))) - * // true - * console.log(resultEquivalence(Result.succeed(1), Result.succeed(2))) - * // false - * console.log(resultEquivalence(Result.succeed(1), Result.fail("foo"))) - * // false + * console.log(eq(Result.succeed(1), Result.succeed(1))) + * // Output: true + * + * console.log(eq(Result.succeed(1), Result.fail("x"))) + * // Output: false * ``` * * @category Equivalence @@ -511,32 +659,33 @@ export const makeEquivalence = ( ) /** - * Maps both the success and failure values of a `Result` using provided functions. + * Transforms both the success and failure channels of a `Result`. + * + * - Applies `onSuccess` if the result is a `Success` + * - Applies `onFailure` if the result is a `Failure` + * - Returns a new `Result`; does not mutate the input + * - Use when you need to transform both channels in a single operation + * + * **Example** (Mapping both channels) * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * - * const success = pipe( + * const result = pipe( * Result.succeed(1), * Result.mapBoth({ * onSuccess: (n) => n + 1, * onFailure: (e) => `Error: ${e}` * }) * ) - * assert.deepStrictEqual(success, Result.succeed(2)) - * - * const failure = pipe( - * Result.fail("not a number"), - * Result.mapBoth({ - * onSuccess: (n) => n + 1, - * onFailure: (e) => `Error: ${e}` - * }) - * ) - * assert.deepStrictEqual(failure, Result.fail("Error: not a number")) + * console.log(result) + * // Output: { _tag: "Success", success: 2, ... } * ``` * + * @see {@link map} to transform only the success value + * @see {@link mapError} to transform only the error value + * @see {@link match} to fold into a single value + * * @category Mapping * @since 4.0.0 */ @@ -558,26 +707,28 @@ export const mapBoth: { ) /** - * Maps the `Failure` side of an `Result` value to a new `Result` value. + * Transforms the failure channel of a `Result`, leaving the success channel unchanged. + * + * - If the result is a `Failure`, applies `f` to the error and returns a new `Failure` + * - If the result is a `Success`, returns it as-is + * - Does not mutate the input + * + * **Example** (Adding context to an error) * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * - * const success = pipe( - * Result.succeed(1), + * const result = pipe( + * Result.fail("not found"), * Result.mapError((e) => `Error: ${e}`) * ) - * assert.deepStrictEqual(success, Result.succeed(1)) - * - * const failure = pipe( - * Result.fail("not a number"), - * Result.mapError((e) => `Error: ${e}`) - * ) - * assert.deepStrictEqual(failure, Result.fail("Error: not a number")) + * console.log(result) + * // Output: { _tag: "Failure", failure: "Error: not found", ... } * ``` * + * @see {@link map} to transform only the success value + * @see {@link mapBoth} to transform both channels + * * @category Mapping * @since 4.0.0 */ @@ -591,26 +742,30 @@ export const mapError: { ) /** - * Maps the `Success` side of an `Result` value to a new `Result` value. + * Transforms the success channel of a `Result`, leaving the failure channel unchanged. + * + * - If the result is a `Success`, applies `f` to the value and returns a new `Success` + * - If the result is a `Failure`, returns it as-is + * - Does not mutate the input + * - Use {@link flatMap} if `f` returns a `Result` (to avoid nested Results) + * + * **Example** (Doubling the success value) * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * - * const success = pipe( - * Result.succeed(1), - * Result.map((n) => n + 1) + * const result = pipe( + * Result.succeed(3), + * Result.map((n) => n * 2) * ) - * assert.deepStrictEqual(success, Result.succeed(2)) - * - * const failure = pipe( - * Result.fail("not a number"), - * Result.map((n) => n + 1) - * ) - * assert.deepStrictEqual(failure, Result.fail("not a number")) + * console.log(result) + * // Output: { _tag: "Success", success: 6, ... } * ``` * + * @see {@link mapError} to transform only the error value + * @see {@link mapBoth} to transform both channels + * @see {@link flatMap} when `f` returns a `Result` + * * @category Mapping * @since 4.0.0 */ @@ -624,33 +779,33 @@ export const map: { ) /** - * Takes two functions and an `Result` value, if the value is a `Failure` the inner - * value is applied to the `onFailure function, if the value is a `Success` the inner - * value is applied to the `onSuccess` function. + * Folds a `Result` into a single value by applying one of two functions. + * + * - Applies `onSuccess` if the result is a `Success` + * - Applies `onFailure` if the result is a `Failure` + * - Both branches must return the same type (or a common supertype) + * - Use when you need to "exit" the `Result` type and produce a plain value + * + * **Example** (Folding to a string) * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * - * const onFailure = (strings: ReadonlyArray): string => - * `strings: ${strings.join(", ")}` + * const format = Result.match({ + * onSuccess: (n: number) => `Got ${n}`, + * onFailure: (e: string) => `Err: ${e}` + * }) * - * const onSuccess = (value: number): string => `Ok: ${value}` + * console.log(format(Result.succeed(42))) + * // Output: "Got 42" * - * assert.deepStrictEqual( - * pipe(Result.succeed(1), Result.match({ onFailure, onSuccess })), - * "Ok: 1" - * ) - * assert.deepStrictEqual( - * pipe( - * Result.fail(["string 1", "string 2"]), - * Result.match({ onFailure, onSuccess }) - * ), - * "strings: string 1, string 2" - * ) + * console.log(format(Result.fail("timeout"))) + * // Output: "Err: timeout" * ``` * + * @see {@link merge} to extract `A | E` without mapping + * @see {@link getOrElse} to unwrap only the success with a fallback + * * @category Pattern Matching * @since 4.0.0 */ @@ -672,32 +827,32 @@ export const match: { ) /** - * Transforms a `Predicate` function into a `Success` of the input value if the predicate returns `true` - * or a `Failure` of the result of the provided function if the predicate returns false + * Lifts a value into a `Result` based on a predicate or refinement. + * + * - If the predicate returns `true`, the value becomes `Success` + * - If the predicate returns `false`, `orFailWith` produces the error for `Failure` + * - Also accepts a `Refinement` to narrow the success type + * - Supports both data-first and data-last (piped) usage + * + * **Example** (Validating a number) * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * - * const isPositive = (n: number): boolean => n > 0 - * - * assert.deepStrictEqual( - * pipe( - * 1, - * Result.liftPredicate(isPositive, (n) => `${n} is not positive`) - * ), - * Result.succeed(1) - * ) - * assert.deepStrictEqual( - * pipe( - * 0, - * Result.liftPredicate(isPositive, (n) => `${n} is not positive`) - * ), - * Result.fail("0 is not positive") + * const ensurePositive = pipe( + * 5, + * Result.liftPredicate( + * (n: number) => n > 0, + * (n) => `${n} is not positive` + * ) * ) + * console.log(ensurePositive) + * // Output: { _tag: "Success", success: 5, ... } * ``` * + * @see {@link filterOrFail} to validate a value that is already in a `Result` + * @see {@link fromNullishOr} for nullable-based construction + * * @category Constructors * @since 4.0.0 */ @@ -724,32 +879,34 @@ export const liftPredicate: { ) /** - * Filter the right value with the provided function. - * If the predicate fails, set the left value with the result of the provided function. + * Validates the success value of a `Result` using a predicate, failing with a + * custom error if the predicate returns `false`. + * + * - If the result is already a `Failure`, it is returned as-is + * - If the predicate passes, the `Success` is returned unchanged + * - If the predicate fails, `orFailWith` produces the error for a new `Failure` + * - Also accepts a `Refinement` to narrow the success type + * - The error type of the output is the union of both error types + * + * **Example** (Filtering a success value) * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * - * const isPositive = (n: number): boolean => n > 0 - * - * assert.deepStrictEqual( - * pipe( - * Result.succeed(1), - * Result.filterOrFail(isPositive, (n) => `${n} is not positive`) - * ), - * Result.succeed(1) - * ) - * assert.deepStrictEqual( - * pipe( - * Result.succeed(0), - * Result.filterOrFail(isPositive, (n) => `${n} is not positive`) - * ), - * Result.fail("0 is not positive") + * const result = pipe( + * Result.succeed(0), + * Result.filterOrFail( + * (n) => n > 0, + * (n) => `${n} is not positive` + * ) * ) + * console.log(result) + * // Output: { _tag: "Failure", failure: "0 is not positive", ... } * ``` * + * @see {@link liftPredicate} to create a `Result` from a raw value with a predicate + * @see {@link flatMap} for general conditional chaining + * * @since 4.0.0 * @category Filtering */ @@ -775,43 +932,56 @@ export const filterOrFail: { ): Result => flatMap(self, (a) => predicate(a) ? succeed(a) : fail(orFailWith(a)))) /** - * Returns the value from a `Result`, merging the success and failure cases. + * Unwraps a `Result` into `A | E` by returning the inner value regardless + * of whether it is a success or failure. + * + * - `Success` returns `A` + * - `Failure` returns `E` + * - Useful when both channels share a compatible type + * + * **Example** (Extracting the inner value) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * const success = Result.succeed(42) - * assert.deepStrictEqual(Result.merge(success), 42) + * console.log(Result.merge(Result.succeed(42))) + * // Output: 42 * - * const failure = Result.fail("error") - * assert.deepStrictEqual(Result.merge(failure), "error") + * console.log(Result.merge(Result.fail("error"))) + * // Output: "error" * ``` * + * @see {@link match} to map each branch to a common type + * @see {@link getOrElse} to provide a fallback for failures + * * @category Getters * @since 4.0.0 */ export const merge: (self: Result) => E | A = match({ onFailure: identity, onSuccess: identity }) /** - * Returns the wrapped value if it's a `Success` or a default value if is a `Failure`. + * Extracts the success value, or computes a fallback from the error. + * + * - `Success` returns the inner value + * - `Failure` applies `onFailure` to the error and returns the result + * - The return type is `A | A2` (union of both branches) + * + * **Example** (Providing a fallback) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * assert.deepStrictEqual( - * Result.getOrElse(Result.succeed(1), (error) => error + "!"), - * 1 - * ) - * assert.deepStrictEqual( - * Result.getOrElse(Result.fail("not a number"), (error) => error + "!"), - * "not a number!" - * ) + * console.log(Result.getOrElse(Result.succeed(1), () => 0)) + * // Output: 1 + * + * console.log(Result.getOrElse(Result.fail("err"), () => 0)) + * // Output: 0 * ``` * + * @see {@link getOrNull} / {@link getOrUndefined} for simpler fallbacks + * @see {@link getOrThrow} to throw on failure + * @see {@link match} to map both branches + * * @category Getters * @since 4.0.0 */ @@ -825,59 +995,86 @@ export const getOrElse: { ) /** - * Returns the wrapped value if it's a `Success` or `null` if it's a `Failure`. + * Extracts the success value, or returns `null` on failure. + * + * - `Success` returns `A` + * - `Failure` returns `null` + * - Convenient for interop with APIs that use `null` to represent absence + * + * **Example** (Unwrapping to nullable) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * assert.deepStrictEqual(Result.getOrNull(Result.succeed(1)), 1) - * assert.deepStrictEqual(Result.getOrNull(Result.fail("a")), null) + * console.log(Result.getOrNull(Result.succeed(1))) + * // Output: 1 + * + * console.log(Result.getOrNull(Result.fail("err"))) + * // Output: null * ``` * + * @see {@link getOrUndefined} to return `undefined` instead + * @see {@link getOrElse} for a custom fallback + * * @category Getters * @since 4.0.0 */ export const getOrNull: (self: Result) => A | null = getOrElse(constNull) /** - * Returns the wrapped value if it's a `Success` or `undefined` if it's a `Failure`. + * Extracts the success value, or returns `undefined` on failure. + * + * - `Success` returns `A` + * - `Failure` returns `undefined` + * - Convenient for interop with APIs that use `undefined` to represent absence + * + * **Example** (Unwrapping to optional) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * assert.deepStrictEqual(Result.getOrUndefined(Result.succeed(1)), 1) - * assert.deepStrictEqual(Result.getOrUndefined(Result.fail("a")), undefined) + * console.log(Result.getOrUndefined(Result.succeed(1))) + * // Output: 1 + * + * console.log(Result.getOrUndefined(Result.fail("err"))) + * // Output: undefined * ``` * + * @see {@link getOrNull} to return `null` instead + * @see {@link getOrElse} for a custom fallback + * * @category Getters * @since 4.0.0 */ export const getOrUndefined: (self: Result) => A | undefined = getOrElse(constUndefined) /** - * Extracts the success value of a `Result` or throws the value returned by the - * provided function if the `Result` is a `Failure`. + * Extracts the success value or throws a custom error derived from the failure. * - * **Example** + * - `Success` returns `A` + * - `Failure` throws the value returned by `onFailure(e)` + * - Use when you want to convert a `Result` into a thrown exception with a + * custom error message or error type + * + * **Example** (Throwing a custom error) * * ```ts * import { Result } from "effect" * - * Result.getOrThrowWith(Result.succeed(1), () => new Error("Unexpected Err")) - * // => 1 - * - * Result.getOrThrowWith( - * Result.fail("error"), - * (err) => new Error(`Unexpected Err: ${err}`) + * console.log( + * Result.getOrThrowWith(Result.succeed(1), () => new Error("fail")) * ) - * // => throws new Error('Unexpected Err: error') + * // Output: 1 + * + * // This would throw: new Error("Unexpected: oops") + * // Result.getOrThrowWith( + * // Result.fail("oops"), + * // (err) => new Error(`Unexpected: ${err}`) + * // ) * ``` * - * @see {@link getOrThrow} for a version that throws the failure value. + * @see {@link getOrThrow} to throw the raw failure value + * @see {@link getOrElse} for a non-throwing alternative * * @category Getters * @since 4.0.0 @@ -893,19 +1090,26 @@ export const getOrThrowWith: { }) /** - * Extracts the success value of an `Result` or throws if the `Result` is a - * `Failure`. + * Extracts the success value or throws the raw failure value `E`. + * + * - `Success` returns `A` + * - `Failure` throws `E` directly + * - Use {@link getOrThrowWith} for a custom error object + * + * **Example** (Unwrapping or throwing) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * assert.deepStrictEqual(Result.getOrThrow(Result.succeed(1)), 1) - * assert.throws(() => Result.getOrThrow(Result.fail("error"))) + * console.log(Result.getOrThrow(Result.succeed(1))) + * // Output: 1 + * + * // This would throw the string "error": + * // Result.getOrThrow(Result.fail("error")) * ``` * - * @throws `E` + * @see {@link getOrThrowWith} for custom error mapping + * @see {@link getOrElse} for a non-throwing alternative * * @category Getters * @since 4.0.0 @@ -913,26 +1117,29 @@ export const getOrThrowWith: { export const getOrThrow: (self: Result) => A = getOrThrowWith(identity) /** - * Returns `self` if it is a `Success` or `that` otherwise. + * Returns the original `Result` if it is a `Success`, otherwise applies + * `that` to the error and returns the resulting `Result`. + * + * - `Success` is returned unchanged + * - `Failure` calls `that(e)` to produce a new `Result` + * - Use to provide a recovery path or fallback computation on failure + * + * **Example** (Recovering from a failure) * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * - * const success = pipe( - * Result.succeed(1), - * Result.orElse(() => Result.succeed(2)) + * const result = pipe( + * Result.fail("primary failed"), + * Result.orElse(() => Result.succeed(99)) * ) - * assert.deepStrictEqual(success, Result.succeed(1)) - * - * const failure = pipe( - * Result.fail("error"), - * Result.orElse(() => Result.succeed(2)) - * ) - * assert.deepStrictEqual(failure, Result.succeed(2)) + * console.log(result) + * // Output: { _tag: "Success", success: 99, ... } * ``` * + * @see {@link getOrElse} to unwrap with a fallback value (not a Result) + * @see {@link mapError} to transform the error without recovering + * * @category Error Handling * @since 4.0.0 */ @@ -946,25 +1153,31 @@ export const orElse: { ) /** - * Sequentially chain two `Result` values, where the second depends on the success value of the first. + * Chains a function that returns a `Result` onto a successful value. + * + * - If `self` is a `Success`, applies `f` to the value and returns the resulting `Result` + * - If `self` is a `Failure`, short-circuits and returns it unchanged + * - The error types are merged into a union (`E | E2`) + * - This is the monadic `bind` / `>>=` for `Result` + * + * **Example** (Sequential validation) * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * - * const parseAndDouble = (s: string) => - * pipe( - * Result.try(() => parseInt(s)), - * Result.flatMap((n) => - * n > 0 ? Result.succeed(n * 2) : Result.fail("not positive") - * ) + * const result = pipe( + * Result.succeed(5), + * Result.flatMap((n) => + * n > 0 ? Result.succeed(n * 2) : Result.fail("not positive") * ) - * - * assert.deepStrictEqual(parseAndDouble("5"), Result.succeed(10)) - * assert.deepStrictEqual(parseAndDouble("-1"), Result.fail("not positive")) + * ) + * console.log(result) + * // Output: { _tag: "Success", success: 10, ... } * ``` * + * @see {@link andThen} for a more flexible variant that also accepts plain values + * @see {@link map} when `f` does not return a `Result` + * * @category Sequencing * @since 4.0.0 */ @@ -978,26 +1191,42 @@ export const flatMap: { ) /** - * Executes a sequence of two `Result`s. The second `Result` can be dependent on the result of the first `Result`. + * A flexible variant of {@link flatMap} that accepts multiple input shapes. + * + * The second argument can be: + * - A function `(a: A) => Result` (same as `flatMap`) + * - A function `(a: A) => A2` (auto-wrapped in `succeed`) + * - A `Result` value (ignores the success of `self`) + * - A plain value `A2` (auto-wrapped in `succeed`, ignores `self`) + * + * If `self` is a `Failure`, the second argument is never evaluated. + * + * **Example** (Using andThen with different argument types) * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * - * const success = pipe( + * // With a function returning a Result + * const a = pipe( * Result.succeed(1), * Result.andThen((n) => Result.succeed(n + 1)) * ) - * assert.deepStrictEqual(success, Result.succeed(2)) * - * const failure = pipe( - * Result.fail("error"), - * Result.andThen((n) => Result.succeed(n + 1)) + * // With a plain mapping function + * const b = pipe( + * Result.succeed(1), + * Result.andThen((n) => n + 1) * ) - * assert.deepStrictEqual(failure, Result.fail("error")) + * + * // With a constant value + * const c = pipe(Result.succeed(1), Result.andThen("done")) + * + * console.log(a, b, c) * ``` * + * @see {@link flatMap} for the stricter variant (function returning Result only) + * @see {@link map} when you always return a plain value + * * @category Sequencing * @since 4.0.0 */ @@ -1023,31 +1252,34 @@ export const andThen: { ) /** - * Takes a structure of `Result`s and returns an `Result` of values with the same structure. + * Collects a structure of `Result`s into a single `Result` of collected values. * - * - If a tuple is supplied, then the returned `Result` will contain a tuple with the same length. - * - If a struct is supplied, then the returned `Result` will contain a struct with the same keys. - * - If an iterable is supplied, then the returned `Result` will contain an array. + * Accepts: + * - A tuple/array: returns `Result` with a tuple/array of success values + * - A struct (record): returns `Result` with a struct of success values + * - An iterable: returns `Result` with an array of success values + * + * Short-circuits on the first `Failure` encountered; later elements are not inspected. + * + * **Example** (Collecting a tuple and a struct) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * assert.deepStrictEqual( - * Result.all([Result.succeed(1), Result.succeed(2)]), - * Result.succeed([1, 2]) - * ) - * assert.deepStrictEqual( - * Result.all({ right: Result.succeed(1), b: Result.succeed("hello") }), - * Result.succeed({ right: 1, b: "hello" }) - * ) - * assert.deepStrictEqual( - * Result.all({ right: Result.succeed(1), b: Result.fail("error") }), - * Result.fail("error") - * ) + * // Tuple + * const tuple = Result.all([Result.succeed(1), Result.succeed("two")]) + * console.log(tuple) + * // Output: { _tag: "Success", success: [1, "two"], ... } + * + * // Struct + * const struct = Result.all({ x: Result.succeed(1), y: Result.fail("err") }) + * console.log(struct) + * // Output: { _tag: "Failure", failure: "err", ... } * ``` * + * @see {@link flatMap} for chaining two Results sequentially + * @see {@link gen} for generator-based composition of multiple Results + * * @category Sequencing * @since 4.0.0 */ @@ -1088,21 +1320,27 @@ export const all: > | Record` becomes `Failure` (i.e., `Result`) + * - `Failure` becomes `Success` (i.e., `Result`) + * - Useful when you want to apply success-oriented operations (like `map`) + * to the error channel, then flip back + * + * **Example** (Swapping channels) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * const success = Result.succeed(42) - * assert.deepStrictEqual(Result.flip(success), Result.fail(42)) + * console.log(Result.flip(Result.succeed(42))) + * // Output: { _tag: "Failure", failure: 42, ... } * - * const failure = Result.fail("error") - * assert.deepStrictEqual(Result.flip(failure), Result.succeed("error")) + * console.log(Result.flip(Result.fail("error"))) + * // Output: { _tag: "Success", success: "error", ... } * ``` * + * @see {@link mapError} to transform the error without swapping + * * @category Utilities * @since 4.0.0 */ @@ -1110,22 +1348,31 @@ export const flip = (self: Result): Result => isFailure(self) ? succeed(self.failure) : fail(self.success) /** - * Provides a generator-based DSL for working with `Result` values in a sequential manner. + * Generator-based syntax for composing `Result` values sequentially. + * + * - Use `yield*` to unwrap a `Result` inside the generator; if any yielded + * `Result` is a `Failure`, the generator short-circuits and returns that failure + * - The return value of the generator is wrapped in `Success` + * - Evaluated eagerly and synchronously (unlike `Effect.gen`) + * + * **Example** (Composing multiple Results) * - * @example * ```ts * import { Result } from "effect" - * import * as assert from "node:assert" * - * const program = Result.gen(function*() { + * const result = Result.gen(function*() { * const a = yield* Result.succeed(1) * const b = yield* Result.succeed(2) * return a + b * }) * - * assert.deepStrictEqual(program, Result.succeed(3)) + * console.log(result) + * // Output: { _tag: "Success", success: 3, ... } * ``` * + * @see {@link flatMap} for point-free sequential composition + * @see {@link all} to collect multiple independent Results + * * @category Generators * @since 4.0.0 */ @@ -1150,23 +1397,16 @@ export const gen: Gen.Gen = (...args) => { // ------------------------------------------------------------------------------------- /** - * The "do simulation" in Effect allows you to write code in a more declarative style, similar to the "do notation" in other programming languages. It provides a way to define variables and perform operations on them using functions like `bind` and `let`. + * Starting point for the "do notation" simulation with `Result`. * - * Here's how the do simulation works: + * Creates a `Result<{}>` (success with an empty object). Use with + * {@link bind} to add `Result`-producing fields and {@link let_ let} + * to add pure computed fields. * - * 1. Start the do simulation using the `Do` value - * 2. Within the do simulation scope, you can use the `bind` function to define variables and bind them to `Result` values - * 3. You can accumulate multiple `bind` statements to define multiple variables within the scope - * 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values + * **Example** (Building an object step by step) * - * @see {@link bind} - * @see {@link bindTo} - * @see {@link let_ let} - * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * * const result = pipe( * Result.Do, @@ -1174,42 +1414,45 @@ export const gen: Gen.Gen = (...args) => { * Result.bind("y", () => Result.succeed(3)), * Result.let("sum", ({ x, y }) => x + y) * ) - * assert.deepStrictEqual(result, Result.succeed({ x: 2, y: 3, sum: 5 })) + * console.log(result) + * // Output: { _tag: "Success", success: { x: 2, y: 3, sum: 5 }, ... } * ``` * + * @see {@link bind} to add Result-producing fields + * @see {@link let_ let} to add pure computed fields + * @see {@link gen} for an alternative generator-based syntax + * * @category Do Notation * @since 4.0.0 */ export const Do: Result<{}> = succeed({}) /** - * The "do simulation" in Effect allows you to write code in a more declarative style, similar to the "do notation" in other programming languages. It provides a way to define variables and perform operations on them using functions like `bind` and `let`. + * Adds a named field to the do-notation accumulator by running a `Result`-producing + * function that receives the current accumulated object. * - * Here's how the do simulation works: + * - Short-circuits on the first `Failure` + * - The field name must not collide with existing keys + * - Use {@link let_ let} for pure (non-Result) computed fields * - * 1. Start the do simulation using the `Do` value - * 2. Within the do simulation scope, you can use the `bind` function to define variables and bind them to `Result` values - * 3. You can accumulate multiple `bind` statements to define multiple variables within the scope - * 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values + * **Example** (Binding Result values) * - * @see {@link Do} - * @see {@link bindTo} - * @see {@link let_ let} - * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * * const result = pipe( * Result.Do, * Result.bind("x", () => Result.succeed(2)), - * Result.bind("y", () => Result.succeed(3)), - * Result.let("sum", ({ x, y }) => x + y) + * Result.bind("y", ({ x }) => Result.succeed(x + 3)) * ) - * assert.deepStrictEqual(result, Result.succeed({ x: 2, y: 3, sum: 5 })) + * console.log(result) + * // Output: { _tag: "Success", success: { x: 2, y: 5 }, ... } * ``` * + * @see {@link Do} to start the do-notation chain + * @see {@link let_ let} for pure computed fields + * @see {@link bindTo} to wrap an initial Result into a named field + * * @category Do Notation * @since 4.0.0 */ @@ -1226,33 +1469,26 @@ export const bind: { } = doNotation.bind(map, flatMap) /** - * The "do simulation" in Effect allows you to write code in a more declarative style, similar to the "do notation" in other programming languages. It provides a way to define variables and perform operations on them using functions like `bind` and `let`. + * Wraps the success value of a `Result` into a named field, producing a + * `Result>`. This is typically used to start a do-notation + * chain from an existing `Result`. * - * Here's how the do simulation works: + * **Example** (Wrapping a value into a named field) * - * 1. Start the do simulation using the `Do` value - * 2. Within the do simulation scope, you can use the `bind` function to define variables and bind them to `Result` values - * 3. You can accumulate multiple `bind` statements to define multiple variables within the scope - * 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values - * - * @see {@link Do} - * @see {@link bind} - * @see {@link let_ let} - * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * * const result = pipe( - * Result.Do, - * Result.bind("x", () => Result.succeed(2)), - * Result.bind("y", () => Result.succeed(3)), - * Result.let("sum", ({ x, y }) => x + y) + * Result.succeed(42), + * Result.bindTo("answer") * ) - * assert.deepStrictEqual(result, Result.succeed({ x: 2, y: 3, sum: 5 })) + * console.log(result) + * // Output: { _tag: "Success", success: { answer: 42 }, ... } * ``` * + * @see {@link Do} to start from an empty object + * @see {@link bind} to add more fields + * * @category Do Notation * @since 4.0.0 */ @@ -1275,23 +1511,16 @@ const let_: { export { /** - * The "do simulation" in Effect allows you to write code in a more declarative style, similar to the "do notation" in other programming languages. It provides a way to define variables and perform operations on them using functions like `bind` and `let`. + * Adds a named field to the do-notation accumulator by computing a pure + * (non-Result) value from the current accumulated object. * - * Here's how the do simulation works: + * - Use {@link bind} when the computation returns a `Result` + * - The field name must not collide with existing keys * - * 1. Start the do simulation using the `Do` value - * 2. Within the do simulation scope, you can use the `bind` function to define variables and bind them to `Result` values - * 3. You can accumulate multiple `bind` statements to define multiple variables within the scope - * 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values + * **Example** (Adding a computed field) * - * @see {@link Do} - * @see {@link bindTo} - * @see {@link bind} - * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * * const result = pipe( * Result.Do, @@ -1299,8 +1528,13 @@ export { * Result.bind("y", () => Result.succeed(3)), * Result.let("sum", ({ x, y }) => x + y) * ) - * assert.deepStrictEqual(result, Result.succeed({ x: 2, y: 3, sum: 5 })) + * console.log(result) + * // Output: { _tag: "Success", success: { x: 2, y: 3, sum: 5 }, ... } * ``` + * + * @see {@link Do} to start the do-notation chain + * @see {@link bind} for Result-producing fields + * * @category Do Notation * @since 4.0.0 */ @@ -1308,28 +1542,28 @@ export { } /** - * Converts an `Option` of an `Result` into an `Result` of an `Option`. + * Transforms `Option>` into `Result, E>`. * - * **Details** + * - `None` becomes `Success(None)` + * - `Some(Success(a))` becomes `Success(Some(a))` + * - `Some(Failure(e))` becomes `Failure(e)` * - * This function transforms an `Option>` into an - * `Result, E>`. If the `Option` is `None`, the resulting `Result` - * will be a `Success` with a `None` value. If the `Option` is `Some`, the - * inner `Result` will be executed, and its result wrapped in a `Some`. + * **Example** (Transposing an Option of a Result) * - * @example * ```ts * import { Option, Result } from "effect" * - * // ┌─── Option> - * // ▼ - * const maybe = Option.some(Result.succeed(42)) + * const some = Option.some(Result.succeed(42)) + * console.log(Result.transposeOption(some)) + * // Output: { _tag: "Success", success: { _tag: "Some", value: 42 }, ... } * - * // ┌─── Result, never, never> - * // ▼ - * const result = Result.transposeOption(maybe) + * const none = Option.none>() + * console.log(Result.transposeOption(none)) + * // Output: { _tag: "Success", success: { _tag: "None" }, ... } * ``` * + * @see {@link transposeMapOption} to map and transpose in one step + * * @since 3.14.0 * @category Transposing */ @@ -1340,28 +1574,32 @@ export const transposeOption = ( } /** - * Transforms an `Option` by applying a function that returns a `Result`, and then transposes the structure. + * Maps an `Option` value with a `Result`-producing function, then transposes + * the structure from `Option>` to `Result, E>`. + * + * - `None` becomes `Success(None)` (the function is never called) + * - `Some(a)` where `f(a)` is `Success(b)` becomes `Success(Some(b))` + * - `Some(a)` where `f(a)` is `Failure(e)` becomes `Failure(e)` + * + * **Example** (Map and transpose in one step) * - * @example * ```ts * import { Option, Result } from "effect" * - * const parseNumber = (s: string) => - * isNaN(Number(s)) ? Result.fail("Invalid number") : Result.succeed(Number(s)) + * const parse = (s: string) => + * isNaN(Number(s)) + * ? Result.fail("not a number" as const) + * : Result.succeed(Number(s)) * - * // Transform Some value - * const some = Result.transposeMapOption(Option.some("42"), parseNumber) - * // Result.succeed(Option.some(42)) + * console.log(Result.transposeMapOption(Option.some("42"), parse)) + * // Output: { _tag: "Success", success: { _tag: "Some", value: 42 }, ... } * - * // Transform None value - * const none = Result.transposeMapOption(Option.none(), parseNumber) - * // Result.succeed(Option.none()) - * - * // Handle invalid input - * const invalid = Result.transposeMapOption(Option.some("abc"), parseNumber) - * // Result.fail("Invalid number") + * console.log(Result.transposeMapOption(Option.none(), parse)) + * // Output: { _tag: "Success", success: { _tag: "None" }, ... } * ``` * + * @see {@link transposeOption} when the Option already contains a Result + * * @since 3.15.0 * @category Transposing */ @@ -1376,59 +1614,75 @@ export const transposeMapOption = dual< >(2, (self, f) => option_.isNone(self) ? succeedNone : map(f(self.value), option_.some)) /** - * Creates a `Result` that succeeds with a `None` value. + * A pre-built `Result>` that succeeds with `None`. + * + * - Equivalent to `Result.succeed(Option.none())` but avoids an extra allocation + * - Useful with {@link transposeOption} patterns + * + * **Example** (Using succeedNone) * - * @example * ```ts - * import { Option, Result } from "effect" - * import * as assert from "node:assert" + * import { Result } from "effect" * - * const result = Result.succeedNone - * assert.deepStrictEqual(result, Result.succeed(Option.none())) + * console.log(Result.isSuccess(Result.succeedNone)) + * // Output: true * ``` * + * @see {@link succeedSome} for the `Some` counterpart + * * @category Constructors * @since 4.0.0 */ export const succeedNone = succeed(option_.none) /** - * Creates a `Result` that succeeds with a `Some` value. + * Creates a `Result>` that succeeds with `Some(a)`. + * + * - Equivalent to `Result.succeed(Option.some(a))` + * - Useful with {@link transposeOption} patterns + * + * **Example** (Wrapping a value in Some inside a Result) * - * @example * ```ts - * import { Option, Result } from "effect" - * import * as assert from "node:assert" + * import { Result } from "effect" * * const result = Result.succeedSome(42) - * assert.deepStrictEqual(result, Result.succeed(Option.some(42))) + * console.log(result) + * // Output: { _tag: "Success", success: { _tag: "Some", value: 42 }, ... } * ``` * + * @see {@link succeedNone} for the `None` counterpart + * * @category Constructors * @since 4.0.0 */ export const succeedSome = (a: A): Result, E> => succeed(option_.some(a)) /** - * Applies a side-effect to the success value of a `Result` without changing the `Result` itself. + * Runs a side-effect on the success value without altering the `Result`. + * + * - If the result is a `Success`, calls `f` with the value (return value is ignored) + * - If the result is a `Failure`, `f` is not called + * - Returns the original `Result` unchanged (same reference) + * - Useful for logging, debugging, or performing mutations outside the Result chain + * + * **Example** (Logging a success value) * - * @example * ```ts * import { pipe, Result } from "effect" - * import * as assert from "node:assert" * - * let sideEffect = 0 - * const success = pipe( + * const result = pipe( * Result.succeed(42), - * Result.tap((n) => { - * sideEffect = n - * }) + * Result.tap((n) => console.log("Got:", n)) * ) + * // Output: "Got: 42" * - * assert.deepStrictEqual(success, Result.succeed(42)) - * assert.deepStrictEqual(sideEffect, 42) + * console.log(Result.isSuccess(result)) + * // Output: true * ``` * + * @see {@link map} to transform the success value + * * @category Mapping * @since 4.0.0 */ diff --git a/packages/effect/src/index.ts b/packages/effect/src/index.ts index fe5c06dd97..077bdd5b4e 100644 --- a/packages/effect/src/index.ts +++ b/packages/effect/src/index.ts @@ -2386,6 +2386,72 @@ export * as Request from "./Request.ts" export * as RequestResolver from "./RequestResolver.ts" /** + * A synchronous, pure type for representing computations that can succeed + * (`Success`) or fail (`Failure`). Unlike `Effect`, `Result` is + * evaluated eagerly and carries no side effects. + * + * **Mental model** + * + * - `Result` is a discriminated union: `Success | Failure` + * - `Success` wraps a value of type `A`, accessed via `.success` + * - `Failure` wraps an error of type `E`, accessed via `.failure` + * - `Result` is a monad: chain operations with {@link flatMap}, compose pipelines with `pipe` + * - All operations are pure and return new `Result` values; the input is never mutated + * - `Result` is yieldable in `Effect.gen`, producing the inner value or short-circuiting on failure + * + * **Common tasks** + * + * - Create from a value: {@link succeed}, {@link fail} + * - Create from nullable: {@link fromNullishOr} + * - Create from Option: {@link fromOption} + * - Create from throwing code: {@link try_ try} + * - Create from predicate: {@link liftPredicate} + * - Transform: {@link map}, {@link mapError}, {@link mapBoth} + * - Unwrap: {@link getOrElse}, {@link getOrNull}, {@link getOrUndefined}, {@link getOrThrow} + * - Pattern match: {@link match} + * - Sequence: {@link flatMap}, {@link andThen}, {@link all} + * - Recover: {@link orElse} + * - Filter: {@link filterOrFail} + * - Convert to Option: {@link getSuccess}, {@link getFailure} + * - Generator syntax: {@link gen} + * - Do notation: {@link Do}, {@link bind}, {@link let_ let} + * - Check variant: {@link isResult}, {@link isSuccess}, {@link isFailure} + * + * **Gotchas** + * + * - `E` defaults to `never`, so `Result` means a result that cannot fail + * - {@link andThen} accepts a `Result`, a function returning a `Result`, a plain value, or a function returning a plain value; {@link flatMap} only accepts a function returning a `Result` + * - {@link all} short-circuits on the first `Failure` and returns it; later elements are not inspected + * - {@link getOrThrow} throws the raw failure value `E`; use {@link getOrThrowWith} for custom error objects + * - {@link tap} runs a side-effect but does not change the result; its return value is ignored + * + * **Quickstart** + * + * **Example** (Parsing and validating with Result) + * + * ```ts + * import { Result } from "effect" + * + * const parse = (input: string): Result.Result => + * isNaN(Number(input)) + * ? Result.fail("not a number") + * : Result.succeed(Number(input)) + * + * const ensurePositive = (n: number): Result.Result => + * n > 0 ? Result.succeed(n) : Result.fail("not positive") + * + * const result = Result.flatMap(parse("42"), ensurePositive) + * + * console.log(Result.getOrElse(result, (err) => `Error: ${err}`)) + * // Output: 42 + * ``` + * + * **See also** + * + * - {@link succeed} / {@link fail} to create values + * - {@link match} to fold both branches + * - {@link gen} for generator-based composition + * * @since 4.0.0 */ export * as Result from "./Result.ts" diff --git a/packages/effect/test/Result.test.ts b/packages/effect/test/Result.test.ts index 0cb275fdef..8d42780ac4 100644 --- a/packages/effect/test/Result.test.ts +++ b/packages/effect/test/Result.test.ts @@ -17,6 +17,18 @@ import { describe("Result", () => { describe("Constructors", () => { + it("succeed", () => { + const result = Result.succeed(42) + assertTrue(Result.isSuccess(result)) + strictEqual(result.success, 42) + }) + + it("fail", () => { + const result = Result.fail("err") + assertTrue(Result.isFailure(result)) + strictEqual(result.failure, "err") + }) + it("void", () => { deepStrictEqual(Result.void, Result.succeed(undefined)) }) @@ -41,15 +53,29 @@ describe("Result", () => { ) }) - it("fromNullable", () => { + it("fromNullishOr", () => { deepStrictEqual(Result.fromNullishOr(null, () => "fallback"), Result.fail("fallback")) deepStrictEqual(Result.fromNullishOr(undefined, () => "fallback"), Result.fail("fallback")) deepStrictEqual(Result.fromNullishOr(1, () => "fallback"), Result.succeed(1)) + // data-last + deepStrictEqual(pipe(null as string | null, Result.fromNullishOr(() => "fallback")), Result.fail("fallback")) + deepStrictEqual(pipe(1 as number | null, Result.fromNullishOr(() => "fallback")), Result.succeed(1)) }) it("fromOption", () => { deepStrictEqual(Result.fromOption(Option.none(), () => "none"), Result.fail("none")) deepStrictEqual(Result.fromOption(Option.some(1), () => "none"), Result.succeed(1)) + // data-last + deepStrictEqual(pipe(Option.none(), Result.fromOption(() => "none")), Result.fail("none")) + deepStrictEqual(pipe(Option.some(1), Result.fromOption(() => "none")), Result.succeed(1)) + }) + + it("succeedNone", () => { + deepStrictEqual(Result.succeedNone, Result.succeed(Option.none())) + }) + + it("succeedSome", () => { + deepStrictEqual(Result.succeedSome(1), Result.succeed(Option.some(1))) }) }) @@ -140,6 +166,9 @@ describe("Result", () => { it("getOrElse", () => { strictEqual(Result.getOrElse(Result.succeed(1), (error) => error + "!"), 1) strictEqual(Result.getOrElse(Result.fail("not a number"), (error) => error + "!"), "not a number!") + // data-last + strictEqual(pipe(Result.succeed(1), Result.getOrElse((error) => error + "!")), 1) + strictEqual(pipe(Result.fail("not a number"), Result.getOrElse((error) => error + "!")), "not a number!") }) it("getOrNull", () => { @@ -155,6 +184,9 @@ describe("Result", () => { it("getOrThrowWith", () => { strictEqual(pipe(Result.succeed(1), Result.getOrThrowWith((e) => new Error(`Unexpected Err: ${e}`))), 1) throws(() => pipe(Result.fail("e"), Result.getOrThrowWith((e) => new Error(`Unexpected Err: ${e}`)))) + // data-first + strictEqual(Result.getOrThrowWith(Result.succeed(1), (e) => new Error(`Unexpected Err: ${e}`)), 1) + throws(() => Result.getOrThrowWith(Result.fail("e"), (e) => new Error(`Unexpected Err: ${e}`))) }) it("getOrThrow", () => { @@ -173,6 +205,9 @@ describe("Result", () => { const f = Result.map(Str.length) assertSuccess(pipe(Result.succeed("abc"), f), 3) assertFailure(pipe(Result.fail("s"), f), "s") + // data-first + assertSuccess(Result.map(Result.succeed("abc"), Str.length), 3) + assertFailure(Result.map(Result.fail("s"), Str.length), "s") }) it("mapBoth", () => { @@ -182,12 +217,61 @@ describe("Result", () => { }) assertSuccess(pipe(Result.succeed(1), f), false) assertFailure(pipe(Result.fail("a"), f), 1) + // data-first + assertSuccess( + Result.mapBoth(Result.succeed(1), { + onFailure: Str.length, + onSuccess: (n: number) => n > 2 + }), + false + ) + assertFailure( + Result.mapBoth(Result.fail("a"), { + onFailure: Str.length, + onSuccess: (n: number) => n > 2 + }), + 1 + ) }) it("mapError", () => { const f = Result.mapError((n: number) => n * 2) assertSuccess(pipe(Result.succeed("a"), f), "a") assertFailure(pipe(Result.fail(1), f), 2) + // data-first + assertSuccess(Result.mapError(Result.succeed("a"), (n: number) => n * 2), "a") + assertFailure(Result.mapError(Result.fail(1), (n) => n * 2), 2) + }) + + it("tap", () => { + let sideEffect = 0 + const success = pipe( + Result.succeed(42), + Result.tap((n) => { + sideEffect = n + }) + ) + assertSuccess(success, 42) + strictEqual(sideEffect, 42) + + // failure path: side effect should not run + let failureSideEffect = 0 + const failure = pipe( + Result.fail("err") as Result.Result, + Result.tap((n) => { + failureSideEffect = n + }) + ) + assertFailure(failure, "err") + strictEqual(failureSideEffect, 0) + + // data-first + let sideEffect2 = 0 + const success2 = Result.tap(Result.succeed(10), (n) => { + sideEffect2 = n + }) + assertSuccess(success2, 10) + strictEqual(sideEffect2, 10) }) }) @@ -198,6 +282,9 @@ describe("Result", () => { const match = Result.match({ onFailure, onSuccess }) strictEqual(match(Result.fail("abc")), "failure3") strictEqual(match(Result.succeed("abc")), "success3") + // data-first + strictEqual(Result.match(Result.fail("abc"), { onFailure, onSuccess }), "failure3") + strictEqual(Result.match(Result.succeed("abc"), { onFailure, onSuccess }), "success3") }) }) @@ -281,6 +368,9 @@ describe("Result", () => { const f = Result.flatMap(flow(Str.length, Result.succeed)) assertSuccess(pipe(Result.succeed("abc"), f), 3) assertFailure(pipe(Result.fail("maError"), f), "maError") + // data-first + assertSuccess(Result.flatMap(Result.succeed("abc"), flow(Str.length, Result.succeed)), 3) + assertFailure(Result.flatMap(Result.fail("maError"), flow(Str.length, Result.succeed)), "maError") }) it("andThen", () => { @@ -307,6 +397,20 @@ describe("Result", () => { assertSuccess(Result.all({ a: Result.succeed(1) }), { a: 1 }) assertSuccess(Result.all({ a: Result.succeed(1), b: Result.succeed(true) }), { a: 1, b: true }) assertFailure(Result.all({ a: Result.succeed(1), b: Result.fail("e") }), "e") + // iterables + const iterable = function*() { + yield Result.succeed(1) + yield Result.succeed(2) + yield Result.succeed(3) + } + assertSuccess(Result.all(iterable()), [1, 2, 3]) + + const iterableWithFailure = function*() { + yield Result.succeed(1) + yield Result.fail("e") + yield Result.succeed(3) + } + assertFailure(Result.all(iterableWithFailure()), "e") }) }) @@ -316,6 +420,10 @@ describe("Result", () => { assertSuccess(pipe(Result.succeed(1), Result.orElse(() => Result.fail("b"))), 1) assertSuccess(pipe(Result.fail("a"), Result.orElse(() => Result.succeed(2))), 2) assertFailure(pipe(Result.fail("a"), Result.orElse(() => Result.fail("b"))), "b") + // data-first + assertSuccess(Result.orElse(Result.succeed(1), () => Result.succeed(2)), 1) + assertSuccess(Result.orElse(Result.fail("a"), () => Result.succeed(2)), 2) + assertFailure(Result.orElse(Result.fail("a"), () => Result.fail("b")), "b") }) }) @@ -408,10 +516,9 @@ describe("Result", () => { assertSuccess(Result.transposeMapOption(Option.some(Result.succeed(1)), identity), Option.some(1)) assertSuccess(Result.transposeMapOption(Option.none(), identity), Option.none()) assertFailure(Result.transposeMapOption(Option.some(Result.fail("e")), identity), "e") + // data-last + assertSuccess(pipe(Option.some(1), Result.transposeMapOption(Result.succeed)), Option.some(1)) + assertSuccess(pipe(Option.none(), Result.transposeMapOption(Result.succeed)), Option.none()) }) }) - - it("succeedSome", () => { - deepStrictEqual(Result.succeedSome(1), Result.succeed(Option.some(1))) - }) })