-
Notifications
You must be signed in to change notification settings - Fork 27
/
_map.js
29 lines (27 loc) · 1.03 KB
/
_map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
'use strict';
var a = require('./a');
var once = require('./once');
var concurrent = require('./concurrent');
var CONCURRENTLY = require('./CONCURRENTLY');
var SERIAL = require('./SERIAL');
module.exports = function _map (cap, then, attached) {
function api (collection, concurrency, iterator, done) {
var args = arguments;
if (args.length === 2) { iterator = concurrency; concurrency = CONCURRENTLY; }
if (args.length === 3 && typeof concurrency !== 'number') { done = iterator; iterator = concurrency; concurrency = CONCURRENTLY; }
var keys = Object.keys(collection);
var tasks = a(collection) ? [] : {};
keys.forEach(function insert (key) {
tasks[key] = function iterate (cb) {
if (iterator.length === 3) {
iterator(collection[key], key, cb);
} else {
iterator(collection[key], cb);
}
};
});
concurrent(tasks, cap || concurrency, then ? then(collection, once(done)) : done);
}
if (!attached) { api.series = _map(SERIAL, then, true); }
return api;
};