$ cnpm install mapsome
Find maped element of an array that will satisfy condition of some.
Map + Some + early return of an element;
npm i mapsome --save
Lets look at regular map-filter example:
const map = (a) => ++a;
const condition = (a) => a > 2;
[1, 2, 3, 4]
.map(map) /* loop full array */
.filter(condition) /* loop full array again */
.pop(); /* get value */
// returns
3
With mapsome this would look this way:
const map = (a) => ++a;
const condition = (a) => a > 2;
mapsome(map, condition, [1, 2, 3, 4]);
// returns
3
mapsome works much faster because no need map full array, first satisfied condition will break the loop.
mapsome could be used with map function only:
const map = (a) => ++a;
mapsome(map, [0, 0, 3, 0]);
// returns
3
// same as
mapsome(map, a => a, [0, 0, 3, 0]);
// returns
3
In old node.js environments that supports es5 only, mapsome could be used with:
var mapsome = require('mapsome/legacy');
mapsome could be installed via bower with:
bower install mapsome --save
When loaded in browser mapsome uses global variable with same name (when browserify or webpack did not used).
MIT
Copyright 2013 - present © cnpmjs.org | Home |