Tiny note; the pipe function behavior is not entirely as described here:
"Parameter 1 must be either of type Function[] or if multiple parameters are provided then each one should be of type Function but this requirement has not been met.");
With the current implementation, parameter 2 (or 3, or 4, ...) could also be a Function[].
For example, the following is valid, while it shouldn't be according to the above error:
I had some fun setting up my first CodeSandbox to demo this with 1 additional test (open the sandbox page separately, because the dev.to embed doesn't allow the tests to be shown...)
The reason is that the concat trick const parameters = fns.reduce((output, value) => output.concat(value), []); will flatten just any value it gets, not only the first one.
I'd say this behavior is desirable. It could be useful! But it's not as described.
I like to build cool things, work with nice people and help others where I can. Currently I'm an engineering manager for a fintech startup and historically a serial founder & freelancer software dev.
Location
München, Deutschland 🇩🇪
Education
The Open University
Work
Engineering Manager @ Deutsche Fintech Solutions GmbH
I updated the check because actually the jsdoc expects just an array of functions to be in the fns variable and so I updated the code accordingly, open to feedback but I think this is cleaner either way. I also added your extra test with some modifications to the article.
Looks good to me! Now it's consistent.
The spread operator can also be used when calling pipe, like pipe(...[f1, f2], ...[f3, f4], f5), so if you know for sure there will be an array (or arrays) of functions to input, that's still possible this way.
I like to build cool things, work with nice people and help others where I can. Currently I'm an engineering manager for a fintech startup and historically a serial founder & freelancer software dev.
Location
München, Deutschland 🇩🇪
Education
The Open University
Work
Engineering Manager @ Deutsche Fintech Solutions GmbH
I like to build cool things, work with nice people and help others where I can. Currently I'm an engineering manager for a fintech startup and historically a serial founder & freelancer software dev.
Location
München, Deutschland 🇩🇪
Education
The Open University
Work
Engineering Manager @ Deutsche Fintech Solutions GmbH
Tiny note; the pipe function behavior is not entirely as described here:
With the current implementation, parameter 2 (or 3, or 4, ...) could also be a Function[].
For example, the following is valid, while it shouldn't be according to the above error:
I had some fun setting up my first CodeSandbox to demo this with 1 additional test (open the sandbox page separately, because the dev.to embed doesn't allow the tests to be shown...)
The reason is that the concat trick
const parameters = fns.reduce((output, value) => output.concat(value), []);
will flatten just any value it gets, not only the first one.I'd say this behavior is desirable. It could be useful! But it's not as described.
I updated the check because actually the jsdoc expects just an array of functions to be in the
fns
variable and so I updated the code accordingly, open to feedback but I think this is cleaner either way. I also added your extra test with some modifications to the article.Looks good to me! Now it's consistent.
The spread operator can also be used when calling pipe, like
pipe(...[f1, f2], ...[f3, f4], f5)
, so if you know for sure there will be an array (or arrays) of functions to input, that's still possible this way.Exactly. Appreciate the input though! 👍
Exactly which still matches the initial intention anyway and keeps things more consistent too.