Turns non fluent APIs into fluent APIs
npm install taptaptap
Examples
Take Array.prototype.push
for example. It returns the new length of the array, making chaining impossible.
const numbers = numbersnumbers
Wrapping the array inside "tap" allows us to chain everything together nicely.
const tap = const numbers =
tap
uses ES6 proxies to make sure each function gets executed, but returns the initially passed value (in this case, the numbers array).
One more example using classes
name = null { thisname = name } { return thisid } { // persist data thisid = this // fill it with new id thiscreatedAt = return true // if everything went well }
The way the API was built forces us to access it like this.
{ const user = user user return user}
Using tap we can do
const tap = { return }
or using an arrow function
const tap = const createUser =
Getting values
Since every function just executes the initial value again, you either have to break out of the chain, or tap out of it.
const tap = { return tapOut }
Alternatively, you can also pass a function in the second argument to group common logic.
Turning
const user = User
to