Skip to content

Commit d2a5e4b

Browse files
committed
simpleMap utility so we don't pass unexpected args
and hopefully faster than built-in .map
1 parent 2dbdf07 commit d2a5e4b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/lib/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@ lib.identity = function(d) { return d; };
135135
// minor convenience helper
136136
lib.noop = function() {};
137137

138+
/*
139+
* simpleMap: alternative to Array.map that only
140+
* passes on the element and up to 2 extra args you
141+
* provide (but not the array index or the whole array)
142+
*
143+
* array: the array to map it to
144+
* func: the function to apply
145+
* x1, x2: optional extra args
146+
*/
147+
lib.simpleMap = function(array, func, x1, x2) {
148+
var len = array.length,
149+
out = new Array(len);
150+
for(var i = 0; i < len; i++) out[i] = func(array[i], x1, x2);
151+
return out;
152+
};
153+
138154
// random string generator
139155
lib.randstr = function randstr(existing, bits, base) {
140156
/*

0 commit comments

Comments
 (0)