- Notifications
You must be signed in to change notification settings - Fork7.1k
Description
tc39/proposal-pipeline-operator made the hack pipe cool again.
If, when, and in what shape it is going to get through is an open question, butlodash
can deliver pretty much the same fun with one simple function.
Consider the above proposal's main usage example:
Object.keys(envars).map(envar=>`${envar}=${envars[envar]}`).join(' ')|> `$ ${%}`|>chalk.dim(%,'node',args.join(' '))|>console.log(%);
Almost same "coding experience" can be achieved utilizing modern JS features (not so modern as of 2025):
// async is omitted for brevity, see belowconstpipeline=(initial, ...lambdas)=>lambdas.reduce((acc,fn)=>fn(acc),initial)pipeline(Object.keys(envars).map(envar=>`${envar}=${envars[envar]}`).join(' '),$=>`$${$}`,$=>chalk.dim($,'node',args.join(' ')),$=>console.log($),)
Withlodash.pipeline
one could literally:
- copy-paste any snippet fromtc39/proposal-pipeline-operator
- replace all occurrences of
%
with$
or_
(one even has freedom to choose their personal favorite) - replace
|>
with$ =>
- add trailing comas
- wrap everything in a
pipeline(...)
call.
It can be done today, without waiting for TC39 decision, browser and tooling support, etc.
Only downside versus the proposal I could think of so far is apparently it cannot supportyield
.
I'm not sure if and how well the proposal will implementyield
, maybe others can weigh in.
Sample implementation I've offered above doesn't work for async code, async-aware version would look like this:
constpipeline=async(initial, ...lambdas)=>lambdas.reduce(async(acc,fn)=>fn(awaitacc),initial)
Open questions I hope others would weigh in on:
- Should it be called
pipeline
orpipe
or else? (with this approach we get rid of the proposal's%
bikeshedding, so let's bikeshed this one instead) - In case of sync code
await
is redundant. Is it that bad for performance? Is it a big enough reason not to go this route?- I think not, but if yes, what about having sync and async versions...
- Can anything be done about
yield
? I see it so rarely in the wild, not sure when it shines. - Am I missing something obvious?
What do you guys think?