Skip to content

Commit c5764d2

Browse files
committed
Add fromReaderEither and fromReaderTaskEither
1 parent 6a6a9d5 commit c5764d2

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Either } from 'fp-ts/lib/Either';
2+
import * as Either_ from 'fp-ts/lib/Either';
3+
4+
import { crashObject } from '../../helpers/crash';
5+
import * as ruins from '../reader-either';
6+
7+
const exampleModel = { foo: 'model' };
8+
9+
type ExampleLeft = { error: number };
10+
const exampleLeft: ExampleLeft = { error: 404 };
11+
12+
type ExampleRight = { result: number };
13+
const exampleRight: ExampleRight = { result: 42 };
14+
15+
type ExampleEither = Either<ExampleLeft, ExampleRight>;
16+
const exampleEitherL: ExampleEither = Either_.left(exampleLeft);
17+
const exampleEitherR: ExampleEither = Either_.right(exampleRight);
18+
19+
describe('ruinReaderEither', () => {
20+
it('should return right', () => {
21+
const result: ExampleRight = ruins.fromReaderEither(
22+
exampleModel,
23+
(_) => exampleEitherR,
24+
);
25+
expect(result).toEqual(exampleRight);
26+
});
27+
it('should throw left', () => {
28+
expect(() =>
29+
ruins.fromReaderEither(exampleModel, (_) => exampleEitherL),
30+
).toThrowError(crashObject(exampleLeft));
31+
});
32+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { TaskEither } from 'fp-ts/lib/TaskEither';
2+
import * as TaskEither_ from 'fp-ts/lib/TaskEither';
3+
4+
import { crashObject } from '../../helpers/crash';
5+
import * as ruins from '../reader-task-either';
6+
7+
const exampleModel = { foo: 'model' };
8+
9+
type ExampleLeft = { error: number };
10+
const exampleLeft: ExampleLeft = { error: 404 };
11+
12+
type ExampleRight = { result: number };
13+
const exampleRight: ExampleRight = { result: 42 };
14+
15+
type ExampleTaskEither = TaskEither<ExampleLeft, ExampleRight>;
16+
const exampleTaskEitherL: ExampleTaskEither = TaskEither_.left(exampleLeft);
17+
const exampleTaskEitherR: ExampleTaskEither = TaskEither_.right(exampleRight);
18+
19+
describe('ruinReaderTaskEither', () => {
20+
it('should return right', async () => {
21+
await expect(
22+
ruins.fromReaderTaskEither(exampleModel, (_) => exampleTaskEitherR),
23+
).resolves.toEqual(exampleRight);
24+
});
25+
26+
it('should throw left', async () => {
27+
await expect(
28+
ruins.fromReaderTaskEither(exampleModel, (_) => exampleTaskEitherL),
29+
).rejects.toEqual(crashObject(exampleLeft));
30+
});
31+
});

src/modules/__tests__/task.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ const exampleTask: Task<Answer> = async () => {
1717

1818
describe('ruinTask', () => {
1919
it('should execute side effects', async () => {
20-
await expect(ruins.fromTask(exampleTask).then(() => mutableState)).resolves.toEqual(true);
20+
await expect(ruins.fromTask(exampleTask).then(() => mutableState)).resolves.toEqual(
21+
true,
22+
);
2123
});
2224

2325
it('should return computation return value', async () => {

src/modules/reader-either.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { pipe } from 'fp-ts/lib/function';
2+
import { ReaderEither } from 'fp-ts/ReaderEither';
3+
4+
import { fromEither } from './either';
5+
6+
export const fromReaderEither = <M, R>(
7+
model: M,
8+
aReaderEither: ReaderEither<M, unknown, R>,
9+
): R => pipe(model, aReaderEither, fromEither);

src/modules/reader-task-either.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { pipe } from 'fp-ts/lib/function';
2+
import { ReaderTaskEither } from 'fp-ts/ReaderTaskEither';
3+
4+
import { fromTaskEither } from './task-either';
5+
6+
export const fromReaderTaskEither = <M, E, R>(
7+
model: M,
8+
aReaderTaskEither: ReaderTaskEither<M, E, R>,
9+
): Promise<R> => pipe(model, aReaderTaskEither, fromTaskEither);

0 commit comments

Comments
 (0)