# Poor man’s JS Pipe operator
_Idea from Timur Xazamov https://x.com/nanot1m/status/1720494020651020562_
## Summary
While waiting for the [JS pipe operator] `|>`, maybe we can use `($=…, $=…, $)` instead.
Either locally declare `let $;` or treat `$` as a shared global volatile var:
```js
const foo = (
$= 2,
$= bar($),
$= $.hello.world(),
$= await fetch($),
await $.json()
)
```
## Examples
Translating the official examples to this pattern:
[JS pipe operator]:https://github.com/tc39/proposal-pipeline-operator
| Pipeline | Equivalent |
|---|---|
| ```js Object.keys(envars) .map(envar => `${envar}=${envars[envar]}`) .join(' ') |> `$ ${%}` |> chalk.dim(%, 'node', args.join(' ')) |> console.log(%); ``` | ```js $= Object.keys(envars) .map(envar => `${envar}=${envars[envar]}`) .join(' ') $= `$ ${$}` $= chalk.dim($, 'node', args.join(' ')) console.log($) ``` |
| ```js const envVarFormat = vars => Object.keys(vars) .map(var => `${var}=${vars[var]}`) .join(' ') |> chalk.dim(%, 'node', args.join(' ')); ``` | ```js const envVarFormat = vars => ( $= Object.keys(vars) .map(var => `${var}=${vars[var]}`) .join(' '), chalk.dim($, 'node', args.join(' ')) ) ``` |
```jsx
return (
|
```jsx
return (
|
| ```js const x = foo() |> bar(%) |> () => log(%) // closure |> baz(%) |> done(%) ``` | ```js const x = ( $= foo(), $= bar($), $= ($$=> () => log($$) )($), $= baz($), done($) ) ``` |