I am looking for a way to create a type that allows me to chain functions together, but delay their execution until after the initial argument is provided. The desired functionality would be something like this:
const getStringFromNumber = pipe()
.then(add(1))
.then(toString)
.then(prepend('0', 5))
.value()
To provide more context, here are the imaginary functions I will be using:
add
is a function that takes 1 argument (a number) and returns another function which, when executed with a second argument, adds them together (e.g.add(1)(2) -> 3
)toString
transforms an argument into a string and returns it (e.g.toString(1) -> "1"
)prepend
is a function that takes two arguments and returns another function which, when executed with a string, prepends characters to reach a minimum length (e.g.
)"1" -> "00001"
In this scenario, getStringFromNumber
should be a function that can be called as follows:
const result = getStringFromNumber(5)
// result is "00006"
The return value of each function does not necessarily have to be a direct value - it might be preferable to have each function return a `thenable` interface for added flexibility. However, the key point is ensuring that the final argument triggering the execution is supplied last and in the correct format.
My question is, what would be the appropriate type definition for pipe
to achieve this behavior?