seasonedcc / composable-functions

Types and functions to make composition easy and safe
MIT License
653 stars 13 forks source link

Add merge utility to domainFunctions #39

Closed gustavoguichard closed 1 year ago

gustavoguichard commented 1 year ago

Reasoning

In our projects, composing functions with all is becoming more and more of a pattern.

The problem is that when we have long tuples it is harder to keep track of the variable names:

const result = await all(dfA, dfB, dfC, dfD)()
const [resultA, resultB, resultC, resultD] = result.data

This problem has led us to start always returning an object as the result of a DF so we have named variables:

const result = await all(dfA, dfB, dfC, dfD)()
const [{ resultA }, { resultB }, { resultC }, { resultD }] = result.data

When following this pattern we had the idea to start merging the resulting tuple into one big object with named keys.

This new merge function will change the code above to:

const result = await merge(dfA, dfB, dfC, dfD)()
const { resultA, resultB, resultC, resultD } = result.data

Just one more utility, no breaking changes added 🧞‍♂️

Ps

When one of the domain functions result does not conform R extends Record<any, any> the merge will return an ErrorResult with the following error message: "Invalid data format returned from some domainFunction"