elpete/CFCollection

Add a `tap` helper method

Closed this issue · 1 comments

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.

Tests are implemented in this branch.

  1. Fork the repo
  2. Create a new branch off of issues/2/tap-helper-method
  3. npm install or yarn
  4. box install
  5. box server start
  6. gulp watch
  7. Open up a web browser at http://localhost:3000/tests/runner.cfm
  8. See the tests that are failing.
  9. Write the code to make the tests pass in models.Collection
  10. Push the changes to YOUR_GITHUB_USERNAME/YOUR_BRANCH_NAME
  11. 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. 😉

Fixed by #13