'use strict';12Object.defineProperty(exports, "__esModule", {3value: true4});56var _isArrayLike = require('./internal/isArrayLike.js');78var _isArrayLike2 = _interopRequireDefault(_isArrayLike);910var _breakLoop = require('./internal/breakLoop.js');1112var _breakLoop2 = _interopRequireDefault(_breakLoop);1314var _eachOfLimit = require('./eachOfLimit.js');1516var _eachOfLimit2 = _interopRequireDefault(_eachOfLimit);1718var _once = require('./internal/once.js');1920var _once2 = _interopRequireDefault(_once);2122var _onlyOnce = require('./internal/onlyOnce.js');2324var _onlyOnce2 = _interopRequireDefault(_onlyOnce);2526var _wrapAsync = require('./internal/wrapAsync.js');2728var _wrapAsync2 = _interopRequireDefault(_wrapAsync);2930var _awaitify = require('./internal/awaitify.js');3132var _awaitify2 = _interopRequireDefault(_awaitify);3334function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }3536// eachOf implementation optimized for array-likes37function eachOfArrayLike(coll, iteratee, callback) {38callback = (0, _once2.default)(callback);39var index = 0,40completed = 0,41{ length } = coll,42canceled = false;43if (length === 0) {44callback(null);45}4647function iteratorCallback(err, value) {48if (err === false) {49canceled = true;50}51if (canceled === true) return;52if (err) {53callback(err);54} else if (++completed === length || value === _breakLoop2.default) {55callback(null);56}57}5859for (; index < length; index++) {60iteratee(coll[index], index, (0, _onlyOnce2.default)(iteratorCallback));61}62}6364// a generic version of eachOf which can handle array, object, and iterator cases.65function eachOfGeneric(coll, iteratee, callback) {66return (0, _eachOfLimit2.default)(coll, Infinity, iteratee, callback);67}6869/**70* Like [`each`]{@link module:Collections.each}, except that it passes the key (or index) as the second argument71* to the iteratee.72*73* @name eachOf74* @static75* @memberOf module:Collections76* @method77* @alias forEachOf78* @category Collection79* @see [async.each]{@link module:Collections.each}80* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.81* @param {AsyncFunction} iteratee - A function to apply to each82* item in `coll`.83* The `key` is the item's key, or index in the case of an array.84* Invoked with (item, key, callback).85* @param {Function} [callback] - A callback which is called when all86* `iteratee` functions have finished, or an error occurs. Invoked with (err).87* @returns {Promise} a promise, if a callback is omitted88* @example89*90* // dev.json is a file containing a valid json object config for dev environment91* // dev.json is a file containing a valid json object config for test environment92* // prod.json is a file containing a valid json object config for prod environment93* // invalid.json is a file with a malformed json object94*95* let configs = {}; //global variable96* let validConfigFileMap = {dev: 'dev.json', test: 'test.json', prod: 'prod.json'};97* let invalidConfigFileMap = {dev: 'dev.json', test: 'test.json', invalid: 'invalid.json'};98*99* // asynchronous function that reads a json file and parses the contents as json object100* function parseFile(file, key, callback) {101* fs.readFile(file, "utf8", function(err, data) {102* if (err) return calback(err);103* try {104* configs[key] = JSON.parse(data);105* } catch (e) {106* return callback(e);107* }108* callback();109* });110* }111*112* // Using callbacks113* async.forEachOf(validConfigFileMap, parseFile, function (err) {114* if (err) {115* console.error(err);116* } else {117* console.log(configs);118* // configs is now a map of JSON data, e.g.119* // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json}120* }121* });122*123* //Error handing124* async.forEachOf(invalidConfigFileMap, parseFile, function (err) {125* if (err) {126* console.error(err);127* // JSON parse error exception128* } else {129* console.log(configs);130* }131* });132*133* // Using Promises134* async.forEachOf(validConfigFileMap, parseFile)135* .then( () => {136* console.log(configs);137* // configs is now a map of JSON data, e.g.138* // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json}139* }).catch( err => {140* console.error(err);141* });142*143* //Error handing144* async.forEachOf(invalidConfigFileMap, parseFile)145* .then( () => {146* console.log(configs);147* }).catch( err => {148* console.error(err);149* // JSON parse error exception150* });151*152* // Using async/await153* async () => {154* try {155* let result = await async.forEachOf(validConfigFileMap, parseFile);156* console.log(configs);157* // configs is now a map of JSON data, e.g.158* // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json}159* }160* catch (err) {161* console.log(err);162* }163* }164*165* //Error handing166* async () => {167* try {168* let result = await async.forEachOf(invalidConfigFileMap, parseFile);169* console.log(configs);170* }171* catch (err) {172* console.log(err);173* // JSON parse error exception174* }175* }176*177*/178function eachOf(coll, iteratee, callback) {179var eachOfImplementation = (0, _isArrayLike2.default)(coll) ? eachOfArrayLike : eachOfGeneric;180return eachOfImplementation(coll, (0, _wrapAsync2.default)(iteratee), callback);181}182183exports.default = (0, _awaitify2.default)(eachOf, 3);184module.exports = exports['default'];185186