Add a `tap` helper method
Closed this issue · 1 comments
elpete commented
Then tap
helper method provides a way to have side effects for collections without modifying the actual collection. The most common example of this would be logging the values of a collection throughout its transformations:
collect( [ 1, 2, 3, 4 ] )
.tap( function( c ) {
writeDump( c.toArray() );
} )
.map( function( item ) {
return item * 2;
} )
.tap( function( c ) {
writeDump( c.toArray() );
} )
.filter( function( item ) {
return item % 4 == 0;
} )
.tap( function( c ) {
writeDump( c.toArray() );
} );
// This would dump out:
// [ 1, 2, 3, 4 ]
// [ 2, 4, 6, 8 ]
// [ 4, 8 ]
The properties of tap
are as follows:
ta[
has the following function signature:
public any function tap(
required any callback
);
- The
callback
is executed passing the collection as the only argument. - No matter what the callback does, the original collection should be returned from
tap
.- Be careful of passing the collection by reference to the
callback
.
- Be careful of passing the collection by reference to the
Tests are implemented in this branch.
- Fork the repo
- Create a new branch off of
issues/2/tap-helper-method
npm install
oryarn
box install
box server start
gulp watch
- Open up a web browser at
http://localhost:3000/tests/runner.cfm
- See the tests that are failing.
- Write the code to make the tests pass in
models.Collection
- Push the changes to
YOUR_GITHUB_USERNAME/YOUR_BRANCH_NAME
- Open a pull request back to
elpete/CFCollection
When you are done, don't forget to add tap
to the docs on the README.md
page.
I'll give feedback and do my best to point and prod instead of show and do. 😉