Skip to content

Commit

Permalink
Result.match (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessekelly881 authored Feb 29, 2024
1 parent 131dc1e commit 953ff58
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/mighty-pots-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@effect-rx/rx": patch
"@effect-rx/rx-react": patch
"@effect-rx/rx-vue": patch
---

added Result.match
4 changes: 2 additions & 2 deletions docs/rx-react/index.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Added in v1.0.0
**Signature**
```ts
export declare const useRxRefresh: <A>(rx: Rx.Rx<A> & Rx.Refreshable) => () => void
export declare const useRxRefresh: <A>(rx: any) => () => void
```
Added in v1.0.0
Expand Down Expand Up @@ -144,7 +144,7 @@ Added in v1.0.0
export declare const useRxSuspense: <A, E>(
rx: Rx.Rx<Result.Result<A, E>>,
options?: { readonly suspendOnWaiting?: boolean }
) => Result.Success<A, E> | Result.Failure<A, E>
) => any
```
Added in v1.0.0
Expand Down
25 changes: 25 additions & 0 deletions docs/rx/Result.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Added in v1.0.0
- [value](#value)
- [combinators](#combinators)
- [map](#map)
- [match](#match)
- [toExit](#toexit)
- [constructors](#constructors)
- [fail](#fail)
Expand Down Expand Up @@ -89,6 +90,30 @@ export declare const map: {
Added in v1.0.0
## match
**Signature**
```ts
export declare const match: {
<A, E, X, Y, Z>(options: {
readonly onInitial: (_: Initial<A, E>) => X
readonly onFailure: (_: Failure<A, E>) => Y
readonly onSuccess: (_: Success<A, E>) => Z
}): (self: Result<A, E>) => X | Y | Z
<A, E, X, Y, Z>(
self: Result<A, E>,
options: {
readonly onInitial: (_: Initial<A, E>) => X
readonly onFailure: (_: Failure<A, E>) => Y
readonly onSuccess: (_: Success<A, E>) => Z
}
): X | Y | Z
}
```
Added in v1.0.0
## toExit
**Signature**
Expand Down
30 changes: 30 additions & 0 deletions packages/rx/src/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,33 @@ export const map: {
return success(f(self.value), self.waiting)
}
})

/**
* @since 1.0.0
* @category combinators
*/
export const match: {
<A, E, X, Y, Z>(options: {
readonly onInitial: (_: Initial<A, E>) => X
readonly onFailure: (_: Failure<A, E>) => Y
readonly onSuccess: (_: Success<A, E>) => Z
}): (self: Result<A, E>) => X | Y | Z
<A, E, X, Y, Z>(self: Result<A, E>, options: {
readonly onInitial: (_: Initial<A, E>) => X
readonly onFailure: (_: Failure<A, E>) => Y
readonly onSuccess: (_: Success<A, E>) => Z
}): X | Y | Z
} = dual(2, <A, E, X, Y, Z>(self: Result<A, E>, options: {
readonly onInitial: (_: Initial<A, E>) => X
readonly onFailure: (_: Failure<A, E>) => Y
readonly onSuccess: (_: Success<A, E>) => Z
}): X | Y | Z => {
switch (self._tag) {
case "Initial":
return options.onInitial(self)
case "Failure":
return options.onFailure(self)
case "Success":
return options.onSuccess(self)
}
})
17 changes: 17 additions & 0 deletions packages/rx/test/Result.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as _ from "@effect-rx/rx/Result"
import { Cause } from "effect"
import { describe, expect, it } from "vitest"

describe("Result", () => {
it("match", () => {
const matcher = _.match({
onInitial: () => "init",
onFailure: () => "fail",
onSuccess: (s) => s.value
})

expect(matcher(_.initial(false))).toEqual("init")
expect(matcher(_.failure(Cause.empty))).toEqual("fail")
expect(matcher(_.success(1))).toEqual(1)
})
})

0 comments on commit 953ff58

Please sign in to comment.