From 22baca6d2cde0f0f8f411920c2a10e1d678b5649 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Mon, 24 Oct 2022 10:31:23 +0100 Subject: [PATCH] Simplify a pipe Refs #388 --- src/write-review/write-review-change-author.ts | 15 ++++++++++----- src/write-review/write-review-remove-author.ts | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/write-review/write-review-change-author.ts b/src/write-review/write-review-change-author.ts index 901f91917..50bc47be4 100644 --- a/src/write-review/write-review-change-author.ts +++ b/src/write-review/write-review-change-author.ts @@ -1,9 +1,10 @@ import { format } from 'fp-ts-routing' import * as E from 'fp-ts/Either' +import { fromOption as fromOption_ } from 'fp-ts/FromEither' import * as O from 'fp-ts/Option' import { Reader } from 'fp-ts/Reader' import * as RA from 'fp-ts/ReadonlyArray' -import { constUndefined, flow, pipe } from 'fp-ts/function' +import { Lazy, constUndefined, flow, pipe } from 'fp-ts/function' import { Status, StatusOpen } from 'hyper-ts' import * as RM from 'hyper-ts/lib/ReaderMiddleware' import * as D from 'io-ts/Decoder' @@ -39,11 +40,11 @@ export const writeReviewChangeAuthor = (doi: PreprintId['doi'], index: number) = ), RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))), RM.bindW('author', ({ form }) => - RM.fromEither( + fromOption(() => 'not-found')( pipe( - form.otherAuthors ?? [], - E.fromOptionK(() => 'not-found')(RA.lookup(index)), - E.let('index', () => index), + O.fromNullable(form.otherAuthors), + O.chain(RA.lookup(index)), + O.let('index', () => index), ), ), ), @@ -282,3 +283,7 @@ function fromReaderK, B, I = StatusOpen, E = ): (...a: A) => RM.ReaderMiddleware { return (...a) => RM.rightReader(f(...a)) } + +// https://github.com/DenisFrezzato/hyper-ts/pull/89 +const fromOption: (onNone: Lazy) => (ma: O.Option) => RM.ReaderMiddleware = + fromOption_(RM.FromEither) diff --git a/src/write-review/write-review-remove-author.ts b/src/write-review/write-review-remove-author.ts index 0db98446c..c90e9daa5 100644 --- a/src/write-review/write-review-remove-author.ts +++ b/src/write-review/write-review-remove-author.ts @@ -1,9 +1,10 @@ import { format } from 'fp-ts-routing' import * as E from 'fp-ts/Either' +import { fromOption as fromOption_ } from 'fp-ts/FromEither' import * as O from 'fp-ts/Option' import { Reader } from 'fp-ts/Reader' import * as RA from 'fp-ts/ReadonlyArray' -import { flow, pipe } from 'fp-ts/function' +import { Lazy, flow, pipe } from 'fp-ts/function' import { Status, StatusOpen } from 'hyper-ts' import * as M from 'hyper-ts/lib/Middleware' import * as RM from 'hyper-ts/lib/ReaderMiddleware' @@ -44,11 +45,11 @@ export const writeReviewRemoveAuthor = (doi: PreprintId['doi'], index: number) = ), RM.bindW('form', ({ user }) => RM.rightReaderTask(getForm(user.orcid, preprint.doi))), RM.bindW('author', ({ form }) => - RM.fromEither( + fromOption(() => 'not-found')( pipe( - form.otherAuthors ?? [], - E.fromOptionK(() => 'not-found')(RA.lookup(index)), - E.let('index', () => index), + O.fromNullable(form.otherAuthors), + O.chain(RA.lookup(index)), + O.let('index', () => index), ), ), ), @@ -271,3 +272,7 @@ function fromReaderK, B, I = StatusOpen, E = ): (...a: A) => RM.ReaderMiddleware { return (...a) => RM.rightReader(f(...a)) } + +// https://github.com/DenisFrezzato/hyper-ts/pull/89 +const fromOption: (onNone: Lazy) => (ma: O.Option) => RM.ReaderMiddleware = + fromOption_(RM.FromEither)