diff --git a/README.md b/README.md index 3521595..5f9c817 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,6 @@ Then you can import these: `import { err, ok, result, Result } from 'acd-utils'` -```typescript -type Result = Err | Ok -``` - #### `err` Returns an `Err` @@ -61,7 +57,7 @@ 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` is either an instance of `Err` or `Ok`. @@ -69,7 +65,7 @@ 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` @@ -87,11 +83,12 @@ type Error = { code: number } +let data: Error | Item[] + function setData(value: T) { data = value } -const data: Result fetch('someapi') .then((res: Item[]) => setData(ok(res))) diff --git a/src/result/result.ts b/src/result/result.ts index adf43bc..89f018e 100644 --- a/src/result/result.ts +++ b/src/result/result.ts @@ -1,4 +1,4 @@ -type ResultBox = { +export type Result = { /** * 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 @@ -18,17 +18,17 @@ interface Ok { readonly ok: S } -export type Result = Err | Ok +// export type Result = Err | Ok -export function err(e: E): Result { +export function err(e: E): Err { return { _tag_: 'Err', err: e } } -export function ok(a: S): Result { +export function ok(a: S): Ok { return { _tag_: 'Ok', ok: a } } -function isErr(a: Result): a is Err { +function isErr(a: Err | Ok): a is Err { switch (a._tag_) { case 'Err': return true @@ -61,8 +61,8 @@ function isErr(a: Result): a is Err { * ) * */ -export function result(a: Result) { - const self = {} as ResultBox +export function result(a: Err | Ok) { + const self = {} as Result function fold(onErr: (a: E) => U, onSuccess: (a: S) => U) { if (isErr(a)) return onErr(a.err) diff --git a/test/result.spec.ts b/test/result.spec.ts index 46d22f2..24fa72d 100644 --- a/test/result.spec.ts +++ b/test/result.spec.ts @@ -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', () => { @@ -10,18 +10,16 @@ describe('result', () => { label: string } - type Data = Result - const makeData = (n: number) => { - if (n > 2) return ok({ label: 'great' }) + if (n > 2) return ok({ label: 'great' }) else - return err({ + return err({ 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(