-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fromReaderEither and fromReaderTaskEither
- Loading branch information
Showing
5 changed files
with
84 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Either } from 'fp-ts/lib/Either'; | ||
import * as Either_ from 'fp-ts/lib/Either'; | ||
|
||
import { crashObject } from '../../helpers/crash'; | ||
import * as ruins from '../reader-either'; | ||
|
||
const exampleModel = { foo: 'model' }; | ||
|
||
type ExampleLeft = { error: number }; | ||
const exampleLeft: ExampleLeft = { error: 404 }; | ||
|
||
type ExampleRight = { result: number }; | ||
const exampleRight: ExampleRight = { result: 42 }; | ||
|
||
type ExampleEither = Either<ExampleLeft, ExampleRight>; | ||
const exampleEitherL: ExampleEither = Either_.left(exampleLeft); | ||
const exampleEitherR: ExampleEither = Either_.right(exampleRight); | ||
|
||
describe('ruinReaderEither', () => { | ||
it('should return right', () => { | ||
const result: ExampleRight = ruins.fromReaderEither( | ||
exampleModel, | ||
(_) => exampleEitherR, | ||
); | ||
expect(result).toEqual(exampleRight); | ||
}); | ||
it('should throw left', () => { | ||
expect(() => | ||
ruins.fromReaderEither(exampleModel, (_) => exampleEitherL), | ||
).toThrowError(crashObject(exampleLeft)); | ||
}); | ||
}); |
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,31 @@ | ||
import { TaskEither } from 'fp-ts/lib/TaskEither'; | ||
import * as TaskEither_ from 'fp-ts/lib/TaskEither'; | ||
|
||
import { crashObject } from '../../helpers/crash'; | ||
import * as ruins from '../reader-task-either'; | ||
|
||
const exampleModel = { foo: 'model' }; | ||
|
||
type ExampleLeft = { error: number }; | ||
const exampleLeft: ExampleLeft = { error: 404 }; | ||
|
||
type ExampleRight = { result: number }; | ||
const exampleRight: ExampleRight = { result: 42 }; | ||
|
||
type ExampleTaskEither = TaskEither<ExampleLeft, ExampleRight>; | ||
const exampleTaskEitherL: ExampleTaskEither = TaskEither_.left(exampleLeft); | ||
const exampleTaskEitherR: ExampleTaskEither = TaskEither_.right(exampleRight); | ||
|
||
describe('ruinReaderTaskEither', () => { | ||
it('should return right', async () => { | ||
await expect( | ||
ruins.fromReaderTaskEither(exampleModel, (_) => exampleTaskEitherR), | ||
).resolves.toEqual(exampleRight); | ||
}); | ||
|
||
it('should throw left', async () => { | ||
await expect( | ||
ruins.fromReaderTaskEither(exampleModel, (_) => exampleTaskEitherL), | ||
).rejects.toEqual(crashObject(exampleLeft)); | ||
}); | ||
}); |
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,9 @@ | ||
import { pipe } from 'fp-ts/lib/function'; | ||
import { ReaderEither } from 'fp-ts/ReaderEither'; | ||
|
||
import { fromEither } from './either'; | ||
|
||
export const fromReaderEither = <M, R>( | ||
model: M, | ||
aReaderEither: ReaderEither<M, unknown, R>, | ||
): R => pipe(model, aReaderEither, fromEither); |
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,9 @@ | ||
import { pipe } from 'fp-ts/lib/function'; | ||
import { ReaderTaskEither } from 'fp-ts/ReaderTaskEither'; | ||
|
||
import { fromTaskEither } from './task-either'; | ||
|
||
export const fromReaderTaskEither = <M, E, R>( | ||
model: M, | ||
aReaderTaskEither: ReaderTaskEither<M, E, R>, | ||
): Promise<R> => pipe(model, aReaderTaskEither, fromTaskEither); |