mekanika-utils-each

Functional style immutable collection iterator each( fn, collection )

mekanika-utils-each

Functional style immutable collection iterator.

Install with npm:

$ npm install mekanika-utils-each

Or install with component(1):

$ component install mekanika/utils-each

Applies a function to a collection (either an Object or Array) and returns the unmodified collection.

each( iteratorFn, collection );
  • iteratorFn {Function} Called for each element in the collection. Receives params:

    • value {Mixed} The value of the current element in the collection
    • index {Number|String} Current index, Number for array, String for object
    • collection {Object|Array} A reference to the collection being iterated
  • collection {Object|Array} The collection to be iterated

  • collection {Object|Array} A reference to the original collection

Using node:

var each = require('mekanika-utils-each');

To use in a browser:

$ make component

and then include as:

<script src="build/mekanika-utils-each.min.js"></script>

Iterating arrays:

each( function(v) { console.log( v*3 ) }, [1,2,3] );
// 3 
// 6 
// 9 
// -> [1,2,3] 

Iterating object properties:

each( function(v) { console.log( v*2 ) }, {a:2,b:3,c:4} );
// 4 
// 6 
// 8 
// -> {a:2,b:3,c:4} 

Functional style: partial application and subsequent calling:

var logSqrt = each( function(v) { console.log( Math.sqrt(v) ) } );
logSqrt( [1,2,3] );
// 1 
// 1.41421... 
// 1.73205... 
// -> [1,2,3] 

MIT