Few days back, I wrote an article about how helpful the Pipeline Operator could be for some use cases.
I shared this article on multiple forums and a lot of users from Reddit gave interesting thought, but one of them gave a rough sketch of writing pipeline decorator using curry. So I went ahead and gave it a try, and here I have it. I just thought it will be useful for anyone looking ways for writing cleaner code.
It is a very tiny library with only 45 lines of code. It has room for loads of improvements. I welcome you to contribute to the codebase or you can use this decorator with your favourite libraries like lodash, underscore etc.
Syntax
Notice how it ends with an empty invocation, this will make sure it returns the result.
I knew similar functionality exists with libraries like RamdaJS. But, I just thought it will be good to learn and create a smaller library than Ramda.
Promise
The library also handles promise, for example, you can do pre and post-processing of data along with Promise
to make it more useful.
Notice how step 2 passes two arguments to _.difference
function and notice step 4, get results from Promise
and pass it on to _.filter
function along with the email to be found as a second argument. Ā
Use cases
- Makes your code clean and easy to read
- Better than chaining or at least in my opinion
- Makes multiple Promise handling much cleaner and readable.
- Pre and post-processing of data for a promise gets easier and cleaner.
Summary
It's always good to share with a wider JavaScript community to get a different perspective. I'm for sure going to give it a try on my next project to reduces the number of lines of code I write.
I welcome you to contribute and make it even more useful covering more use cases.
Finally, please share your thought. Thank you.