1.0.0 • Published 12 years ago
mekanika-utils-each v1.0.0
mekanika-utils-each
Functional style immutable collection iterator.
Installation
Install with npm:
$ npm install mekanika-utils-eachOr install with component(1):
$ component install mekanika/utils-eachAPI
Applies a function to a collection (either an Object or Array) and returns the unmodified collection.
each( iteratorFn, collection );Params
iteratorFn{Function} Called for each element in the collection. Receives params:value{Mixed} The value of the current element in the collectionindex{Number|String} Current index, Number for array, String for objectcollection{Object|Array} A reference to the collection being iterated
collection{Object|Array} The collection to be iterated
Returns
collection{Object|Array} A reference to the original collection
Usage
Setup
Using node:
var each = require('mekanika-utils-each');To use in a browser:
$ make componentand then include as:
<script src="build/mekanika-utils-each.min.js"></script>Examples
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]License
MIT