Tap
Tap your functions into method chains for near-seamless interoperability.
Why
Functions give us flexibility about granularity and locality, but they can be awkward to compose with an value's methods. tap
makes using functions & methods together idiomatic, natural, and very easy to read. See here for a fuller discussion.
Example
var { return a + b } { return a / b } var people = name: 'bob' age: 55 name: 'susan' age: 44 name: 'charles' age: 20 name: 'bex' age: 30 var averageAge = people averageAge//= 37
This is equivalent to:
var averageAge = Math
API
tap.mixin (object, [objects]) -> object
Mixes the .tap
method into any number of objects. If the environment supports es5, then the property will be set to non-enumerable.
var tap = // mix in to base backbone prototypestap // mix in to a newly created objectvar player = tap // mix in to a 'blank' object (one with no [[Prototype]])var map = tap // mix in to everything inheriting from Object.prototype// making .tap work on all non-null/undefined values,// including primitivestap
.tap (fn, [secondary-args]) -> anyValue
The mixed-in .tap
method calls a function, using the context of the method as the first argument:
var { return v + 1 } num = 5 num//= 6
.tap
also takes optional secondary arguments:
var { return a / b } num = 10 num//= 5
Install
npm install tap-chain
or download. Creates a CommonJS module if available, otherwise exports a global named tap
.