From 5fdcd43365d839988a05d854b46aef0b34d5eb68 Mon Sep 17 00:00:00 2001 From: Diogo Biazus Date: Mon, 6 Nov 2023 20:24:24 -0500 Subject: [PATCH] Implement sequence (and therefore pipe) in terms of atmp. --- src/apply-environment.test.ts | 21 +++++++++++++++++++++ src/constructor.ts | 12 ++++++++++-- src/domain-functions.ts | 19 ++++--------------- src/errors.ts | 2 ++ 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/apply-environment.test.ts b/src/apply-environment.test.ts index 58c57faf..a0fef291 100644 --- a/src/apply-environment.test.ts +++ b/src/apply-environment.test.ts @@ -5,6 +5,27 @@ import { mdf } from './constructor.ts' import { applyEnvironment } from './domain-functions.ts' describe('applyEnvironment', () => { + it('fails when environment fails parser', async () => { + const getEnv = mdf(z.unknown(), z.number())((_, e) => e) + + const getEnvWithEnvironment = applyEnvironment( + getEnv, + 'invalid environment', + ) + + assertEquals(await getEnvWithEnvironment('some input'), { + success: false, + errors: [], + inputErrors: [], + environmentErrors: [ + { + message: 'Expected number, received string', + path: [], + }, + ], + }) + }) + it('should apply environment', async () => { const getEnv = mdf(z.unknown(), z.string())((_, e) => e) diff --git a/src/constructor.ts b/src/constructor.ts index 58f65fd7..d2efd75a 100644 --- a/src/constructor.ts +++ b/src/constructor.ts @@ -136,7 +136,10 @@ function makeDomainFunction( : formatSchemaErrors(envResult.error.issues), } } - return dfResultFromAtmp(atmp(handler))(result.data as I, envResult.data as E) + return dfResultFromAtmp(atmp(handler))( + result.data as I, + envResult.data as E, + ) } as DomainFunction> } } @@ -166,5 +169,10 @@ const undefinedSchema: ParserSchema = { }, } -export { makeDomainFunction, makeDomainFunction as mdf, safeResult, dfResultFromAtmp } +export { + dfResultFromAtmp, + makeDomainFunction, + makeDomainFunction as mdf, + safeResult, +} diff --git a/src/domain-functions.ts b/src/domain-functions.ts index 52113707..6e134587 100644 --- a/src/domain-functions.ts +++ b/src/domain-functions.ts @@ -179,21 +179,10 @@ function sequence( ...fns: Fns ): DomainFunction> { return function (input: unknown, environment?: unknown) { - return safeResult(async () => { - const results = [] - let currResult: undefined | Result - for await (const fn of fns) { - const result = await fn( - currResult?.success ? currResult.data : input, - environment, - ) - if (!result.success) throw new ResultError(result) - currResult = result - results.push(result.data) - } - - return results - }) + const [first, ...rest] = fns.map((df) => + atmp(fromSuccess(applyEnvironment(df, environment))), + ) + return dfResultFromAtmp(A.sequence(first, ...rest))(input) } as DomainFunction> } diff --git a/src/errors.ts b/src/errors.ts index 74313b7a..ec1c4d69 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -168,6 +168,8 @@ function failureToErrorResult({ errors }: Failure): ErrorResult { message: exception.message, }, ] + : exception instanceof ResultError + ? exception.result.environmentErrors : [], ), }