biofriction-wp-theme/node_modules/now-and-later/lib/map.js

64 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-10-26 14:18:09 +02:00
'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;