mekanika-utils-map

Functional style map iterator `map( fn, collection )`

npm install mekanika-utils-map
6 downloads in the last week
12 downloads in the last month

mekanika-utils-map

Functional style map iterator map( fn, collection [, this] )

Overview

A standard map iterator for Objects and Arrays:

  • Steps through each element in collection
  • Applies the transform function fn( value )
  • Returns the mutated collection

Main difference is that it flips traditional map parameters around to support currying, for partial application of mutator functions for mapping:

var square = map( function(v) { return v*v; } );
// -> {Function} (partially applied map)
square( [1,2,3] );
// -> [1,4,9]

Installation

Install with npm:

$ npm install mekanika-utils-map

Install with component(1):

$ component install mekanika/utils-map

API

map( mutatorFn, collection [, thisBinding] );

Requires

Params

  • mutatorFn {Function} Called for each element of collection. Passed:

    • value {Mixed} The value of the current iterator element
    • index _{Number|String} The element index (Number for array, String for Object)
    • collection {Object|Array}
  • collection {Object|Array} The collection of elements to iterate

  • thisBinding (*) Optional this binding for the mutatorFn callback

Returns

  • modifiedCollection {Object|Array} The mutated collection of elements

Usage

For node:

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

For use in a browser, first build the file:

$ make component

Then include

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

Examples

Arrays and objects:

map( function(v) { return v*v; }, [1,2,3] );
// -> [1,4,9]

map( function(v) { return v+5 }, {a:1, b:2, c:3} );
// -> [6,7,8]

Functional programming partial application:

var square = map( function(v) { return v*v; } );
// -> {Function} (partially applied map)
square( [1,2,3] );
// -> [1,4,9]

License

MIT

npm loves you