From fcb536ff87c41f8664fa8678df01c554b0a80719 Mon Sep 17 00:00:00 2001 From: Konrad Markus Date: Fri, 16 Sep 2022 23:31:37 +0300 Subject: [PATCH] Add fromReaderEither and fromReaderTaskEither --- src/modules/__tests__/reader-either.ts | 27 +++++++++++++++++++ src/modules/__tests__/reader-task-either.ts | 29 +++++++++++++++++++++ src/modules/reader-either.ts | 11 ++++++++ src/modules/reader-task-either.ts | 6 +++++ 4 files changed, 73 insertions(+) create mode 100644 src/modules/__tests__/reader-either.ts create mode 100644 src/modules/__tests__/reader-task-either.ts create mode 100644 src/modules/reader-either.ts create mode 100644 src/modules/reader-task-either.ts diff --git a/src/modules/__tests__/reader-either.ts b/src/modules/__tests__/reader-either.ts new file mode 100644 index 0000000..4306803 --- /dev/null +++ b/src/modules/__tests__/reader-either.ts @@ -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; +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..a459baf --- /dev/null +++ b/src/modules/__tests__/reader-task-either.ts @@ -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; +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/reader-either.ts b/src/modules/reader-either.ts new file mode 100644 index 0000000..4863c04 --- /dev/null +++ b/src/modules/reader-either.ts @@ -0,0 +1,11 @@ +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..2179b7d --- /dev/null +++ b/src/modules/reader-task-either.ts @@ -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 = (model: M, aReaderTaskEither: ReaderTaskEither): Promise => + pipe(model, aReaderTaskEither, fromTaskEither);