Skip to content

Commit

Permalink
A Composable map is a pipe where the mapper is first turned into anot…
Browse files Browse the repository at this point in the history
…her Composable. The code gets a bit shorter and easier to understand IMHO
  • Loading branch information
diogob committed Mar 10, 2024
1 parent cfd59f0 commit 4693b3e
Showing 1 changed file with 3 additions and 8 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,
}

0 comments on commit 4693b3e

Please sign in to comment.