Skip to content

Commit

Permalink
feat: introduce unwrapResultAsObservable (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmichalina authored Dec 21, 2022
1 parent b360d32 commit 744a69c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { maybe, Maybe, either, Either, ok, fail, Result, reader, Reader, listOf, List } from './index'
import { maybe, Maybe, either, Either, ok, fail, Result, reader, Reader, listOf, List, unwrapResultAsObservable } from './index'

describe('package api', () => {
it('should export maybe', () => {
Expand All @@ -21,4 +21,8 @@ describe('package api', () => {
it('should export reader', () => {
expect(listOf(1, 2)).toBeInstanceOf(List)
})

it('should export unwrapResult', () => {
expect(unwrapResultAsObservable()).toBeInstanceOf(Function)
})
})
1 change: 1 addition & 0 deletions src/result/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './result.factory'
export * from './result.interface'
export * from './transformers/result-to-promise'
export * from './transformers/try-catch-to-result'
export * from './transformers/unwrap-result'
5 changes: 5 additions & 0 deletions src/result/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ describe('result', () => {
const sut = result(() => 1 + 1 === 2, true, 'FAILURE!')
expect(sut.isOk()).toEqual(true)
})

it('should return fail when predicate yields false', () => {
const sut = result(() => 1 + 1 === 1, true, 'FAILURE!')
expect(sut.isFail()).toEqual(true)
})
})

describe('toFailIfExists', () => {
Expand Down
42 changes: 42 additions & 0 deletions src/result/transformers/unwrap-result.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { map, of } from 'rxjs'
import { ok, fail } from '../result.factory'
import { unwrapResultAsObservable } from './unwrap-result'

describe('unwrapResult', () => {
it('should unwrap ok', (done) => {
const sut = of(ok<number, string>(1))

sut.pipe(
unwrapResultAsObservable(),
map(a => a + 1)
).subscribe({
next: v => {
expect(v).toEqual(2)
done()
},
error: e => {
expect('should not emit from .error').toEqual(false)
done(e)
}
})
})

it('should unwrap fail', done => {
const sut = of(fail<number, string>('i failed'))

sut.pipe(
unwrapResultAsObservable(),
map(a => a + 2)
).subscribe({
error: (v) => {
expect(v).toEqual('i failed')
done()
},
next: val => {
expect('should not emit from .next').toEqual(false)
done(val)
}
})
})

})
15 changes: 15 additions & 0 deletions src/result/transformers/unwrap-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { map, Observable } from 'rxjs'
import { IResult } from '../result.interface'

export function unwrapResultAsObservable<T, E>() {
return function unwrapResultAsObservable1(
source: Observable<IResult<T, E>>
): Observable<T> {
return source.pipe(
map(result => {
if (result.isOk()) return result.unwrap()
throw result.unwrapFail()
})
) as Observable<T>
}
}

0 comments on commit 744a69c

Please sign in to comment.