Skip to content

Commit

Permalink
Merge pull request #135 from seasonedcc/implement-composable-map-in-t…
Browse files Browse the repository at this point in the history
…erms-of-pipe

Reimplement composable map in terms of pipe
  • Loading branch information
diogob authored Mar 15, 2024
2 parents cfd59f0 + 5942b8c commit 8df93c1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
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

0 comments on commit 8df93c1

Please sign in to comment.