Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement composable map in terms of pipe #135

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/composable/composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,9 @@ function map<T extends Composable, R>(
fn: T,
mapper: (res: UnpackResult<ReturnType<T>>) => R,
) {
return (async (...args) => {
const res = await fn(...args)
if (!res.success) return error(res.errors)
const mapped = await composable(mapper)(res.data)
if (!mapped.success) return error(mapped.errors)
return mapped
}) as Composable<(...args: Parameters<T>) => R>
return pipe(fn as Composable, composable(mapper) as Composable) as Composable<
(...args: Parameters<T>) => R
>
}

/**
Expand Down Expand Up @@ -226,4 +222,3 @@ export {
sequence,
success,
}

10 changes: 3 additions & 7 deletions src/domain-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,9 @@ function map<O, R>(
dfn: DomainFunction<O>,
mapper: (element: O) => R | Promise<R>,
): DomainFunction<R> {
return ((input, environment) =>
dfResultFromcomposable(
A.map(
A.composable(() => fromSuccess(dfn)(input, environment)),
mapper,
),
)()) as DomainFunction<R>
return dfResultFromcomposable(
A.map(A.composable(fromSuccess(dfn)), mapper),
) as DomainFunction<R>
}

/**
Expand Down
Loading