Skip to content

Commit

Permalink
feat: remove "ResultBox" type
Browse files Browse the repository at this point in the history
  • Loading branch information
acd02 committed Feb 22, 2020
1 parent cab3221 commit f66b7a1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ Then you can import these:

`import { err, ok, result, Result } from 'acd-utils'`

```typescript
type Result<E, S> = Err<E> | Ok<S>
```
#### `err`

Returns an `Err`
Expand Down Expand Up @@ -61,15 +57,15 @@ const success: Ok = ok(data)

#### `result`

Wraps a value of two possible types (`Result`) and returns a [`ResultBox`](https://github.com/acd02/utils/blob/master/src/result/result.ts#L1) object
Wraps a value of two possible types (`Ok | Err`) and returns a [`Result`](https://github.com/acd02/utils/blob/master/src/result/result.ts#L1) object
allowing you to unfold the value to handle both cases.

An instance of `Result<E, S>` is either an instance of `Err` or `Ok`.
The first type is used for failure (E), the second for success (S).

Sort of like a really really lightweight outlaw Result monad.

Methods available on the `ResultBox` object are:
Methods available on the `Result` object are:

- `fold`, takes two functions
- a first function that will get executed if the value is an `Err`
Expand All @@ -87,11 +83,12 @@ type Error = {
code: number
}

let data: Error | Item[]

function setData<T>(value: T) {
data = value
}

const data: Result<Error, Item[]>

fetch('someapi')
.then((res: Item[]) => setData(ok(res)))
Expand Down
14 changes: 7 additions & 7 deletions src/result/result.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type ResultBox<E, S> = {
export type Result<E, S> = {
/**
* Takes two functions. a first function that will get executed if the value
* is an `Err`, and a second function, that will get executed if the value
Expand All @@ -18,17 +18,17 @@ interface Ok<S> {
readonly ok: S
}

export type Result<E, S> = Err<E> | Ok<S>
// export type Result<E, S> = Err<E> | Ok<S>

export function err<E = never, A = never>(e: E): Result<E, A> {
export function err<E = never>(e: E): Err<E> {
return { _tag_: 'Err', err: e }
}

export function ok<E = never, S = never>(a: S): Result<E, S> {
export function ok<S = never>(a: S): Ok<S> {
return { _tag_: 'Ok', ok: a }
}

function isErr<E, S>(a: Result<E, S>): a is Err<E> {
function isErr<E, S>(a: Err<E> | Ok<S>): a is Err<E> {
switch (a._tag_) {
case 'Err':
return true
Expand Down Expand Up @@ -61,8 +61,8 @@ function isErr<E, S>(a: Result<E, S>): a is Err<E> {
* )
*
*/
export function result<E, S>(a: Result<E, S>) {
const self = {} as ResultBox<E, S>
export function result<E, S>(a: Err<E> | Ok<S>) {
const self = {} as Result<E, S>

function fold<U>(onErr: (a: E) => U, onSuccess: (a: S) => U) {
if (isErr(a)) return onErr(a.err)
Expand Down
12 changes: 5 additions & 7 deletions test/result.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { result, Result, err, ok } from '../src/result/result'
import { result, err, ok } from '../src/result/result'

describe('result', () => {
describe('fold', () => {
Expand All @@ -10,18 +10,16 @@ describe('result', () => {
label: string
}

type Data = Result<Error, Success>

const makeData = (n: number) => {
if (n > 2) return ok({ label: 'great' })
if (n > 2) return ok<Success>({ label: 'great' })
else
return err({
return err<Error>({
code: 500,
})
}

const errData: Data = makeData(1)
const successData: Data = makeData(10)
const errData = makeData(1)
const successData = makeData(10)

it('should execute the first function if value is an Err', () => {
expect(
Expand Down

0 comments on commit f66b7a1

Please sign in to comment.