This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also derived from Either
- Loading branch information
Showing
4 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { identity, pipe } from "../core/index" | ||
import { | ||
Right, | ||
Left, | ||
cata as cata_, | ||
fold as fold_, | ||
chain as chain_, | ||
map as map_, | ||
isLeft, | ||
isRight, | ||
} from "./Either" | ||
|
||
export type Ok<B> = Right<B> | ||
export type Err<A> = Left<A> | ||
export type Result<A, B> = Err<A> | Ok<B> | ||
|
||
export const Ok = <B, A = unknown>(value: B): Result<A, B> => Right(value) | ||
export const Err = <A, B = unknown>(value: A): Result<A, B> => Left(value) | ||
|
||
export const of = Ok | ||
|
||
export const cata = <A, B, U>(handlers: { | ||
Err: (a: A) => U | ||
Ok: (v: B) => U | ||
}) => | ||
cata_({ | ||
Left: handlers.Err, | ||
Right: handlers.Ok, | ||
}) as (result: Result<A, B>) => U | ||
|
||
export const fold = <A, B, U>(errorFn: (a: A) => U, okFn: (b: B) => U) => | ||
fold_(errorFn, okFn) as (result: Result<A, B>) => U | ||
|
||
// Chain | ||
export const chain = <A, B, U>(fn: (a: B) => Result<A, U>) => | ||
chain_(fn) as (result: Result<A, B>) => Result<A, U> | ||
|
||
// Functor | ||
export const map = <A, B, U>(fn: (a: B) => U) => | ||
map_(fn) as (result: Result<A, B>) => Result<A, U> | ||
|
||
// Error handling | ||
export const orElse = <A, B>(fn: (a: A) => B) => | ||
pipe(fold<A, B, B>(fn, identity), (v: B) => Ok<B, A>(v)) | ||
|
||
export const isOk = <A, B>(result: Result<A, B>): result is Ok<B> => | ||
isRight(result) | ||
|
||
export const isError = <A, B>(result: Result<A, B>): result is Err<A> => | ||
isLeft(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { | ||
Ok, | ||
Err, | ||
cata, | ||
fold, | ||
of, | ||
map, | ||
chain, | ||
orElse, | ||
isOk, | ||
isError, | ||
} from "../Result" | ||
|
||
describe("Result", () => { | ||
test("Ok", () => { | ||
const O = jest.fn() | ||
const E = jest.fn() | ||
|
||
fold(E, O)(Ok(5)) | ||
|
||
expect(O).toBeCalledWith(5) | ||
expect(E).not.toHaveBeenCalled() | ||
}) | ||
|
||
test("Err", () => { | ||
const O = jest.fn() | ||
const E = jest.fn() | ||
|
||
fold(E, O)(Err("whoops")) | ||
|
||
expect(O).not.toHaveBeenCalled() | ||
expect(E).toHaveBeenCalled() | ||
}) | ||
|
||
test("of", () => expect(of(5)).toEqual(Ok(5))) | ||
|
||
test("cata(Ok)", () => { | ||
const O = jest.fn() | ||
const E = jest.fn() | ||
|
||
cata({ Ok: O, Err: E })(Ok(5)) | ||
|
||
expect(O).toBeCalledWith(5) | ||
expect(E).not.toHaveBeenCalled() | ||
}) | ||
|
||
test("cata(Err)", () => { | ||
const O = jest.fn() | ||
const E = jest.fn() | ||
|
||
cata({ Ok: O, Err: E })(Err("whoops")) | ||
|
||
expect(O).not.toHaveBeenCalled() | ||
expect(E).toHaveBeenCalled() | ||
}) | ||
|
||
test("map(Ok)", () => | ||
expect(map((v: number) => v * 2)(Ok(5))).toEqual(Ok(10))) | ||
test("map(Err)", () => | ||
expect(map(jest.fn())(Err("whoops"))).toEqual(Err("whoops"))) | ||
|
||
test("chain(Ok)", () => | ||
expect(chain((v: number) => Ok(v * 2))(Ok(5))).toEqual(Ok(10))) | ||
test("chain(Err)", () => | ||
expect(chain(jest.fn())(Err("whoops"))).toEqual(Err("whoops"))) | ||
|
||
test("orElse", () => expect(orElse(() => 10)(Err("whoops"))).toEqual(Ok(10))) | ||
|
||
test("isOk(Ok)", () => expect(isOk(Ok(5))).toBe(true)) | ||
test("isOk(Err)", () => expect(isOk(Err("whoops"))).toBe(false)) | ||
|
||
test("isError(Ok)", () => expect(isError(Ok(5))).toBe(false)) | ||
test("isError(Err)", () => expect(isError(Err("whoops"))).toBe(true)) | ||
}) |