Skip to content

Commit

Permalink
fix: add missing methods to asyncresult
Browse files Browse the repository at this point in the history
  • Loading branch information
josh- committed Dec 4, 2023
1 parent c825d97 commit 2f49fef
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
19 changes: 19 additions & 0 deletions __tests__/AsyncResult/isError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expectType } from 'ts-expect'

import { AR, pipe } from '../..'

describe('isError', () => {
it('provides correct types', () => {
expectType<boolean>(AR.isError(AR.resolve("")))
})

it('returns true', () => {
expect(AR.isError(AR.reject(""))).toBeTruthy()
expect(pipe(AR.reject(""), AR.isError)).toBeTruthy()
})

it('returns false', () => {
expect(AR.isError(AR.resolve(""))).toBeFalsy()
expect(pipe(AR.resolve(""), AR.isError)).toBeFalsy()
})
})
19 changes: 19 additions & 0 deletions __tests__/AsyncResult/isOk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expectType } from 'ts-expect'

import { AR, pipe } from '../..'

describe('isOk', () => {
it('provides correct types', () => {
expectType<boolean>(AR.isOk(AR.resolve("")))
})

it('returns true', () => {
expect(AR.isOk(AR.resolve(""))).toBeTruthy()
expect(pipe(AR.resolve(""), AR.isOk)).toBeTruthy()
})

it('returns false', () => {
expect(AR.isOk(AR.reject(""))).toBeFalsy()
expect(pipe(AR.reject(""), AR.isOk)).toBeFalsy()
})
})
16 changes: 16 additions & 0 deletions src/AsyncResult/AsyncResult.res
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ let filter = (promise, predicateFn) => {
)
}

@gentype
let isOk = result => {
switch result {
| Ok(_) => true
| Error(_) => false
}
}

@gentype
let isError = result => {
switch result {
| Ok(_) => false
| Error(_) => true
}
}

@gentype
let match = (promise, okFn, errorFn) => {
promise->thenResolve(option => {
Expand Down

0 comments on commit 2f49fef

Please sign in to comment.