DEV Community

loading...

Discussion on: Functional pipe

Collapse
th0rgall profile image
Thor Galle • Edited

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:

pipe(
        () => "first function",
        [
          () => "second function, inside array",
          () => "third function, inside array"
        ]
      )

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.

Collapse
jamesrweb profile image
James Robb Author

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.

Collapse
th0rgall profile image
Thor Galle

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.

Thread Thread
jamesrweb profile image
James Robb Author

Exactly. Appreciate the input though! 👍

Thread Thread
jamesrweb profile image
James Robb Author

Exactly which still matches the initial intention anyway and keeps things more consistent too.