mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-02 03:15:52 +08:00
Result: tests + type-level tests + jsdocs (#1248)
This commit is contained in:
@@ -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<string, string>
|
||||
declare const number$string: Result.Result<number, string>
|
||||
declare const boolean$string: Result.Result<boolean, string>
|
||||
declare const boolean$Error: Result.Result<boolean, Error>
|
||||
declare const literal$Error: Result.Result<"a", Error>
|
||||
|
||||
describe("Result", () => {
|
||||
it("flip", () => {
|
||||
expect(Result.flip(number$string)).type.toBe<Result.Result<string, number>>()
|
||||
expect(pipe(number$string, Result.flip)).type.toBe<Result.Result<string, number>>()
|
||||
})
|
||||
|
||||
it("try", () => {
|
||||
expect(Result.try(() => 1)).type.toBe<Result.Result<number, unknown>>()
|
||||
expect(Result.try({ try: () => 1, catch: () => new Error() })).type.toBe<Result.Result<number, Error>>()
|
||||
})
|
||||
|
||||
describe("all", () => {
|
||||
it("tuple", () => {
|
||||
expect(Result.all([])).type.toBe<Result.Result<[], never>>()
|
||||
expect(Result.all([number$string])).type.toBe<Result.Result<[number], string>>()
|
||||
expect(Result.all([number$string, boolean$string])).type.toBe<Result.Result<[number, boolean], string>>()
|
||||
expect(Result.all([number$string, boolean$Error])).type.toBe<Result.Result<[number, boolean], string | Error>>()
|
||||
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<Result.Result<{}, never>>()
|
||||
expect(Result.all({ a: number$string })).type.toBe<Result.Result<{ a: number }, string>>()
|
||||
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<Array<Result.Result<number, string>>>()
|
||||
expect(Result.all(resultArray)).type.toBe<Result.Result<Array<number>, string>>()
|
||||
expect(pipe(resultArray, Result.all)).type.toBe<Result.Result<Array<number>, string>>()
|
||||
})
|
||||
|
||||
it("record", () => {
|
||||
const resultRecord = hole<Record<string, Result.Result<number, string>>>()
|
||||
expect(Result.all(resultRecord)).type.toBe<Result.Result<{ [x: string]: number }, string>>()
|
||||
expect(pipe(resultRecord, Result.all)).type.toBe<Result.Result<{ [x: string]: number }, string>>()
|
||||
})
|
||||
})
|
||||
|
||||
it("andThen", () => {
|
||||
expect(Result.andThen(string$string, number$string)).type.toBe<Result.Result<number, string>>()
|
||||
expect(string$string.pipe(Result.andThen(number$string))).type.toBe<Result.Result<number, string>>()
|
||||
|
||||
expect(Result.andThen(string$string, () => number$string)).type.toBe<Result.Result<number, string>>()
|
||||
expect(string$string.pipe(Result.andThen(() => number$string))).type.toBe<Result.Result<number, string>>()
|
||||
})
|
||||
|
||||
it("liftPredicate", () => {
|
||||
const primitiveNumber = hole<number>()
|
||||
const stringOrNumber = hole<string | number>()
|
||||
const predicateNumberOrString = hole<Predicate.Predicate<number | string>>()
|
||||
const refinementNumberOrStringToNumber = hole<Predicate.Refinement<number | string, number>>()
|
||||
|
||||
expect(
|
||||
Result.liftPredicate(predicateNumberOrString, (sn) => {
|
||||
expect(sn).type.toBe<string | number>()
|
||||
return "b" as const
|
||||
})
|
||||
).type.toBe<(a: string | number) => Result.Result<string | number, "b">>()
|
||||
expect(
|
||||
Result.liftPredicate(refinementNumberOrStringToNumber, (sn) => {
|
||||
expect(sn).type.toBe<string | number>()
|
||||
return "b" as const
|
||||
})
|
||||
).type.toBe<(a: string | number) => Result.Result<number, "b">>()
|
||||
|
||||
expect(
|
||||
Result.liftPredicate(
|
||||
stringOrNumber,
|
||||
(sn): sn is number => {
|
||||
expect(sn).type.toBe<string | number>()
|
||||
return typeof sn === "number"
|
||||
},
|
||||
(sn) => {
|
||||
expect(sn).type.toBe<string | number>()
|
||||
return "b" as const
|
||||
}
|
||||
)
|
||||
).type.toBe<Result.Result<number, "b">>()
|
||||
expect(
|
||||
pipe(
|
||||
stringOrNumber,
|
||||
Result.liftPredicate(
|
||||
(sn): sn is number => {
|
||||
expect(sn).type.toBe<string | number>()
|
||||
return typeof sn === "number"
|
||||
},
|
||||
(sn) => {
|
||||
expect(sn).type.toBe<string | number>()
|
||||
return "b" as const
|
||||
}
|
||||
)
|
||||
)
|
||||
).type.toBe<Result.Result<number, "b">>()
|
||||
|
||||
expect(
|
||||
Result.liftPredicate(stringOrNumber, predicateNumberOrString, (sn) => {
|
||||
expect(sn).type.toBe<string | number>()
|
||||
return "b" as const
|
||||
})
|
||||
).type.toBe<Result.Result<string | number, "b">>()
|
||||
expect(
|
||||
pipe(
|
||||
stringOrNumber,
|
||||
Result.liftPredicate(predicateNumberOrString, (sn) => {
|
||||
expect(sn).type.toBe<string | number>()
|
||||
return "b" as const
|
||||
})
|
||||
)
|
||||
).type.toBe<Result.Result<string | number, "b">>()
|
||||
|
||||
expect(
|
||||
Result.liftPredicate(primitiveNumber, predicateNumberOrString, (sn) => {
|
||||
expect(sn).type.toBe<string | number>()
|
||||
return "b" as const
|
||||
})
|
||||
).type.toBe<Result.Result<number, "b">>()
|
||||
expect(
|
||||
pipe(
|
||||
primitiveNumber,
|
||||
Result.liftPredicate(predicateNumberOrString, (sn) => {
|
||||
expect(sn).type.toBe<string | number>()
|
||||
return "b" as const
|
||||
})
|
||||
)
|
||||
).type.toBe<Result.Result<number, "b">>()
|
||||
})
|
||||
|
||||
it("fromNullishOr", () => {
|
||||
const nullableString = hole<string | null>()
|
||||
const nullableObject = hole<{ a: string } | undefined>()
|
||||
|
||||
expect(
|
||||
Result.fromNullishOr(
|
||||
nullableString,
|
||||
() => new Error()
|
||||
)
|
||||
).type.toBe<Result.Result<string, Error>>()
|
||||
|
||||
expect(
|
||||
pipe(
|
||||
nullableString,
|
||||
Result.fromNullishOr(() => new Error())
|
||||
)
|
||||
).type.toBe<Result.Result<string, Error>>()
|
||||
|
||||
expect(
|
||||
Result.fromNullishOr(nullableObject, () => new Error())
|
||||
).type.toBe<Result.Result<{ a: string }, Error>>()
|
||||
|
||||
expect(
|
||||
pipe(
|
||||
nullableObject,
|
||||
Result.fromNullishOr(() => new Error())
|
||||
)
|
||||
).type.toBe<
|
||||
Result.Result<{ a: string }, Error>
|
||||
>()
|
||||
})
|
||||
|
||||
it("filterOrFail", () => {
|
||||
const predicateUnknown = hole<Predicate.Predicate<unknown>>()
|
||||
|
||||
const arrayOfStrings$Error = hole<Result.Result<Array<string>, Error>>()
|
||||
expect(
|
||||
Result.filterOrFail(arrayOfStrings$Error, Array.isArrayNonEmpty, (ss) => {
|
||||
expect(ss).type.toBe<Array<string>>()
|
||||
return "b" as const
|
||||
})
|
||||
).type.toBe<Result.Result<[string, ...Array<string>], "b" | Error>>()
|
||||
expect(
|
||||
pipe(
|
||||
arrayOfStrings$Error,
|
||||
Result.filterOrFail(Array.isArrayNonEmpty, (ss) => {
|
||||
expect(ss).type.toBe<Array<string>>()
|
||||
return "b" as const
|
||||
})
|
||||
)
|
||||
).type.toBe<Result.Result<[string, ...Array<string>], "b" | Error>>()
|
||||
|
||||
const readonlyArrayOfStrings$Error = hole<Result.Result<ReadonlyArray<string>, Error>>()
|
||||
expect(
|
||||
Result.filterOrFail(readonlyArrayOfStrings$Error, Array.isReadonlyArrayNonEmpty, (ss) => {
|
||||
expect(ss).type.toBe<ReadonlyArray<string>>()
|
||||
return "b" as const
|
||||
})
|
||||
).type.toBe<Result.Result<readonly [string, ...Array<string>], "b" | Error>>()
|
||||
expect(
|
||||
pipe(
|
||||
readonlyArrayOfStrings$Error,
|
||||
Result.filterOrFail(Array.isReadonlyArrayNonEmpty, (ss) => {
|
||||
expect(ss).type.toBe<ReadonlyArray<string>>()
|
||||
return "b" as const
|
||||
})
|
||||
)
|
||||
).type.toBe<Result.Result<readonly [string, ...Array<string>], "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<Result.Result<"a", "b" | Error>>()
|
||||
expect(
|
||||
pipe(
|
||||
literal$Error,
|
||||
Result.filterOrFail(Predicate.isString, (a) => {
|
||||
expect(a).type.toBe<"a">()
|
||||
return "b" as const
|
||||
})
|
||||
)
|
||||
).type.toBe<Result.Result<"a", "b" | Error>>()
|
||||
|
||||
// @tstyche fixme -- This doesn't work but it should
|
||||
expect(
|
||||
Result.filterOrFail(literal$Error, Predicate.isString, (_s: string) => "b" as const)
|
||||
).type.toBe<Result.Result<"a", "b" | Error>>()
|
||||
expect(
|
||||
pipe(
|
||||
literal$Error,
|
||||
Result.filterOrFail(Predicate.isString, (_s: string) => "b" as const)
|
||||
)
|
||||
).type.toBe<Result.Result<"a", "b" | Error>>()
|
||||
|
||||
expect(
|
||||
Result.filterOrFail(literal$Error, predicateUnknown, (a) => {
|
||||
expect(a).type.toBe<"a">()
|
||||
return "b" as const
|
||||
})
|
||||
).type.toBe<Result.Result<"a", "b" | Error>>()
|
||||
expect(
|
||||
pipe(
|
||||
literal$Error,
|
||||
Result.filterOrFail(predicateUnknown, (a) => {
|
||||
expect(a).type.toBe<"a">()
|
||||
return "b" as const
|
||||
})
|
||||
)
|
||||
).type.toBe<Result.Result<"a", "b" | Error>>()
|
||||
|
||||
expect(
|
||||
Result.filterOrFail(literal$Error, predicateUnknown, (_s: string) => "b" as const)
|
||||
).type.toBe<Result.Result<"a", "b" | Error>>()
|
||||
expect(
|
||||
pipe(
|
||||
literal$Error,
|
||||
Result.filterOrFail(predicateUnknown, (_s: string) => "b" as const)
|
||||
)
|
||||
).type.toBe<Result.Result<"a", "b" | Error>>()
|
||||
})
|
||||
|
||||
it("type level helpers", () => {
|
||||
type S = Result.Result.Success<typeof number$string>
|
||||
type F = Result.Result.Failure<typeof number$string>
|
||||
expect<S>().type.toBe<number>()
|
||||
expect<F>().type.toBe<string>()
|
||||
})
|
||||
|
||||
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<Result.Result<{ a: number; b: string; c: boolean }, never>>()
|
||||
|
||||
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<Result.Result<{ a: number; b: string; c: boolean }, never>>()
|
||||
})
|
||||
})
|
||||
|
||||
it("transposeMapOption", () => {
|
||||
expect(Result.transposeMapOption(Option.none(), (value) => {
|
||||
expect(value).type.toBe<never>()
|
||||
return string$string
|
||||
})).type.toBe<
|
||||
Result.Result<Option.Option<string>, string>
|
||||
>()
|
||||
expect(pipe(
|
||||
Option.none(),
|
||||
Result.transposeMapOption((value) => {
|
||||
expect(value).type.toBe<never>()
|
||||
return string$string
|
||||
})
|
||||
)).type.toBe<
|
||||
Result.Result<Option.Option<string>, string>
|
||||
>()
|
||||
expect(Result.transposeMapOption(Option.some(42), (value) => {
|
||||
expect(value).type.toBe<number>()
|
||||
return string$string
|
||||
})).type.toBe<
|
||||
Result.Result<Option.Option<string>, string>
|
||||
>()
|
||||
expect(pipe(
|
||||
Option.some(42),
|
||||
Result.transposeMapOption((value) => {
|
||||
expect(value).type.toBe<number>()
|
||||
return string$string
|
||||
})
|
||||
)).type.toBe<
|
||||
Result.Result<Option.Option<string>, string>
|
||||
>()
|
||||
})
|
||||
+729
-475
File diff suppressed because it is too large
Load Diff
@@ -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<A>`) or fail (`Failure<E>`). Unlike `Effect`, `Result` is
|
||||
* evaluated eagerly and carries no side effects.
|
||||
*
|
||||
* **Mental model**
|
||||
*
|
||||
* - `Result<A, E>` is a discriminated union: `Success<A, E> | Failure<A, E>`
|
||||
* - `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<number>` 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<number, string> =>
|
||||
* isNaN(Number(input))
|
||||
* ? Result.fail("not a number")
|
||||
* : Result.succeed(Number(input))
|
||||
*
|
||||
* const ensurePositive = (n: number): Result.Result<number, string> =>
|
||||
* 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"
|
||||
|
||||
@@ -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<number, string>,
|
||||
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<number>(), Result.transposeMapOption(Result.succeed)), Option.none())
|
||||
})
|
||||
})
|
||||
|
||||
it("succeedSome", () => {
|
||||
deepStrictEqual(Result.succeedSome(1), Result.succeed(Option.some(1)))
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user