tc39 / proposal-pipeline-operator

A proposal for adding a useful pipe operator to JavaScript.
http://tc39.github.io/proposal-pipeline-operator/
BSD 3-Clause "New" or "Revised" License
7.56k stars 108 forks source link

Consider mentioning fluent API tree-shakability as a technical motivation #309

Open TzviPM opened 2 days ago

TzviPM commented 2 days ago

In “ Implementation of a modular schema library in TypeScript with focus on bundle size and performance” (2003; see https://valibot.dev/thesis.pdf), Fabian Hiller presented valibot as an alternative to preexisting validation libraries that could achieve smaller bundle size by optimizing for tree shaking.

Many JavaScript apis such as RxJS, Valibot, Zod, JQuery, deal with the concept of pipelining. Existing approaches are to either:

If support for pipelining were built into the language in a way that its syntax became preferred or even increased popularity of piped apis, it would benefit the ecosystem insofar as tree shakable code leads to smaller bundle sizes and allows libraries to provide more features without requiring consumers to ship unused code.

adrian-gierakowski commented 2 days ago

💯

Jopie64 commented 2 days ago

fluent API:

z.string().email().optional('arg')

pipe function or method:

z.pipe(v.string(), v.email(), v.optional('arg'))

current hack pipe proposal:

z |> v.string(%) |> v.email(%) |> v.optional(%, 'arg')

Fluent API still looks the most clean. But considering its downside I'd still prefer pipe function over the current hack pipe syntax proposal...

adrian-gierakowski commented 2 days ago

Downside of pipe function is that it will always have a limit on the number of args if you want to have good typescript support

Being able to use point free style (couple do what curried, data last functions) in a hack pipe would be ideal

z |> v.string |> v.email |> v.optional('arg')