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 fcb536f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/modules/__tests__/reader-either.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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));
});
});
29 changes: 29 additions & 0 deletions src/modules/__tests__/reader-task-either.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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),
);
});
});
11 changes: 11 additions & 0 deletions src/modules/reader-either.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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
);
6 changes: 6 additions & 0 deletions src/modules/reader-task-either.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {pipe} from 'fp-ts/lib/function';
import {fromTaskEither} from "./task-either";
import {ReaderTaskEither} from "fp-ts/ReaderTaskEither";

export const fromReaderTaskEither = <M, E, R>(model: M, aReaderTaskEither: ReaderTaskEither<M, E, R>): Promise<R> =>
pipe(model, aReaderTaskEither, fromTaskEither);

0 comments on commit fcb536f

Please sign in to comment.