Skip to content

Commit

Permalink
Add fromReaderEither and fromReaderTaskEither
Browse files Browse the repository at this point in the history
  • Loading branch information
konker committed Sep 16, 2022
1 parent 6a6a9d5 commit c5764d2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/modules/__tests__/reader-either.ts
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));
});
});
31 changes: 31 additions & 0 deletions src/modules/__tests__/reader-task-either.ts
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));
});
});
4 changes: 3 additions & 1 deletion src/modules/__tests__/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const exampleTask: Task<Answer> = async () => {

describe('ruinTask', () => {
it('should execute side effects', async () => {
await expect(ruins.fromTask(exampleTask).then(() => mutableState)).resolves.toEqual(true);
await expect(ruins.fromTask(exampleTask).then(() => mutableState)).resolves.toEqual(
true,
);
});

it('should return computation return value', async () => {
Expand Down
9 changes: 9 additions & 0 deletions src/modules/reader-either.ts
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);
9 changes: 9 additions & 0 deletions src/modules/reader-task-either.ts
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);

0 comments on commit c5764d2

Please sign in to comment.