Subtheme of barrio
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.2 KiB

2 years ago
'use strict';
var once = require('once');
var helpers = require('./helpers');
function map(values, iterator, extensions, done) {
// Allow for extensions to not be specified
if (typeof extensions === 'function') {
done = extensions;
extensions = {};
}
// Handle no callback case
if (typeof done !== 'function') {
done = helpers.noop;
}
done = once(done);
// Will throw if non-object
var keys = Object.keys(values);
var length = keys.length;
var count = length;
var idx = 0;
// Return the same type as passed in
var results = helpers.initializeResults(values);
var exts = helpers.defaultExtensions(extensions);
if (length === 0) {
return done(null, results);
}
for (idx = 0; idx < length; idx++) {
var key = keys[idx];
next(key);
}
function next(key) {
var value = values[key];
var storage = exts.create(value, key) || {};
exts.before(storage);
iterator(value, key, once(handler));
function handler(err, result) {
if (err) {
exts.error(err, storage);
return done(err, results);
}
exts.after(result, storage);
results[key] = result;
if (--count === 0) {
done(err, results);
}
}
}
}
module.exports = map;