Skip to content

Commit

Permalink
Implement sequence (and therefore pipe) in terms of atmp.
Browse files Browse the repository at this point in the history
  • Loading branch information
diogob committed Nov 7, 2023
1 parent eefd87d commit 5fdcd43
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
21 changes: 21 additions & 0 deletions src/apply-environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 10 additions & 2 deletions src/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ function makeDomainFunction<I, E>(
: 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<Awaited<Output>>
}
}
Expand Down Expand Up @@ -166,5 +169,10 @@ const undefinedSchema: ParserSchema<undefined> = {
},
}

export { makeDomainFunction, makeDomainFunction as mdf, safeResult, dfResultFromAtmp }
export {
dfResultFromAtmp,
makeDomainFunction,
makeDomainFunction as mdf,
safeResult,
}

19 changes: 4 additions & 15 deletions src/domain-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,10 @@ function sequence<Fns extends DomainFunction[]>(
...fns: Fns
): DomainFunction<UnpackAll<Fns>> {
return function (input: unknown, environment?: unknown) {
return safeResult(async () => {
const results = []
let currResult: undefined | Result<unknown>
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<UnpackAll<Fns>>
}

Expand Down
2 changes: 2 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ function failureToErrorResult({ errors }: Failure): ErrorResult {
message: exception.message,
},
]
: exception instanceof ResultError
? exception.result.environmentErrors
: [],
),
}
Expand Down

0 comments on commit 5fdcd43

Please sign in to comment.