-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce resultToObservable (#195)
- Loading branch information
1 parent
744a69c
commit bd78b80
Showing
4 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { Result, fail, ok, result, resultToPromise } from './public_api' | ||
import { Result, fail, ok, result, resultToPromise, resultToObservable } from './public_api' | ||
|
||
describe('result api', () => { | ||
it('should export', () => { | ||
expect(fail(Error('Test'))).toBeInstanceOf(Result) | ||
expect(ok(1)).toBeInstanceOf(Result) | ||
expect(result(() => true, 1, Error('Test'))).toBeInstanceOf(Result) | ||
expect(typeof resultToPromise).toEqual('function') | ||
expect(typeof resultToObservable).toEqual('function') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { fail, ok } from '../result.factory' | ||
import { resultToObservable } from './result-to-observable' | ||
|
||
describe(resultToObservable.name, () => { | ||
it('should be ok', done => { | ||
const result = ok<string, Error>('hello') | ||
const sut = resultToObservable(result) | ||
|
||
sut | ||
.subscribe({ | ||
next: v => { | ||
expect(v).toEqual('hello') | ||
done() | ||
}, | ||
error: done | ||
}) | ||
}) | ||
|
||
it('should be ok', done => { | ||
const result = fail<string, Error>(new Error('I failed, sorry.')) | ||
const sut = resultToObservable(result) | ||
|
||
sut | ||
.subscribe({ | ||
error: (v: Error) => { | ||
expect(v.message).toEqual('I failed, sorry.') | ||
done() | ||
}, | ||
next: done | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Observable, of, throwError } from 'rxjs' | ||
import { IResult } from '../result.interface' | ||
|
||
export function resultToObservable<TOk, TFail>(result: IResult<TOk, TFail>): Observable<TOk> { | ||
if (result.isOk()) { | ||
return of(result.unwrap()) | ||
} else { | ||
return throwError(() => result.unwrapFail()) | ||
} | ||
} |