diff --git a/src/modules/__tests__/reader-either.ts b/src/modules/__tests__/reader-either.ts new file mode 100644 index 0000000..5bf7871 --- /dev/null +++ b/src/modules/__tests__/reader-either.ts @@ -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; +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)); + }); +}); diff --git a/src/modules/__tests__/reader-task-either.ts b/src/modules/__tests__/reader-task-either.ts new file mode 100644 index 0000000..a7116f4 --- /dev/null +++ b/src/modules/__tests__/reader-task-either.ts @@ -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; +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)); + }); +}); diff --git a/src/modules/__tests__/task.ts b/src/modules/__tests__/task.ts index 0258b55..ff0f2eb 100644 --- a/src/modules/__tests__/task.ts +++ b/src/modules/__tests__/task.ts @@ -17,7 +17,9 @@ const exampleTask: Task = 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 () => { diff --git a/src/modules/reader-either.ts b/src/modules/reader-either.ts new file mode 100644 index 0000000..22de4fc --- /dev/null +++ b/src/modules/reader-either.ts @@ -0,0 +1,9 @@ +import { pipe } from 'fp-ts/lib/function'; +import { ReaderEither } from 'fp-ts/ReaderEither'; + +import { fromEither } from './either'; + +export const fromReaderEither = ( + model: M, + aReaderEither: ReaderEither, +): R => pipe(model, aReaderEither, fromEither); diff --git a/src/modules/reader-task-either.ts b/src/modules/reader-task-either.ts new file mode 100644 index 0000000..aa57241 --- /dev/null +++ b/src/modules/reader-task-either.ts @@ -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 = ( + model: M, + aReaderTaskEither: ReaderTaskEither, +): Promise => pipe(model, aReaderTaskEither, fromTaskEither);