[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 |
|
---|
| 7 | exports.default = function (coll, iteratee, callback) {
|
---|
| 8 | var eachOfImplementation = (0, _isArrayLike2.default)(coll) ? eachOfArrayLike : eachOfGeneric;
|
---|
| 9 | eachOfImplementation(coll, (0, _wrapAsync2.default)(iteratee), callback);
|
---|
| 10 | };
|
---|
| 11 |
|
---|
| 12 | var _isArrayLike = require('lodash/isArrayLike');
|
---|
| 13 |
|
---|
| 14 | var _isArrayLike2 = _interopRequireDefault(_isArrayLike);
|
---|
| 15 |
|
---|
| 16 | var _breakLoop = require('./internal/breakLoop');
|
---|
| 17 |
|
---|
| 18 | var _breakLoop2 = _interopRequireDefault(_breakLoop);
|
---|
| 19 |
|
---|
| 20 | var _eachOfLimit = require('./eachOfLimit');
|
---|
| 21 |
|
---|
| 22 | var _eachOfLimit2 = _interopRequireDefault(_eachOfLimit);
|
---|
| 23 |
|
---|
| 24 | var _doLimit = require('./internal/doLimit');
|
---|
| 25 |
|
---|
| 26 | var _doLimit2 = _interopRequireDefault(_doLimit);
|
---|
| 27 |
|
---|
| 28 | var _noop = require('lodash/noop');
|
---|
| 29 |
|
---|
| 30 | var _noop2 = _interopRequireDefault(_noop);
|
---|
| 31 |
|
---|
| 32 | var _once = require('./internal/once');
|
---|
| 33 |
|
---|
| 34 | var _once2 = _interopRequireDefault(_once);
|
---|
| 35 |
|
---|
| 36 | var _onlyOnce = require('./internal/onlyOnce');
|
---|
| 37 |
|
---|
| 38 | var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
|
---|
| 39 |
|
---|
| 40 | var _wrapAsync = require('./internal/wrapAsync');
|
---|
| 41 |
|
---|
| 42 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
---|
| 43 |
|
---|
| 44 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 45 |
|
---|
| 46 | // eachOf implementation optimized for array-likes
|
---|
| 47 | function eachOfArrayLike(coll, iteratee, callback) {
|
---|
| 48 | callback = (0, _once2.default)(callback || _noop2.default);
|
---|
| 49 | var index = 0,
|
---|
| 50 | completed = 0,
|
---|
| 51 | length = coll.length;
|
---|
| 52 | if (length === 0) {
|
---|
| 53 | callback(null);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | function iteratorCallback(err, value) {
|
---|
| 57 | if (err) {
|
---|
| 58 | callback(err);
|
---|
| 59 | } else if (++completed === length || value === _breakLoop2.default) {
|
---|
| 60 | callback(null);
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | for (; index < length; index++) {
|
---|
| 65 | iteratee(coll[index], index, (0, _onlyOnce2.default)(iteratorCallback));
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | // a generic version of eachOf which can handle array, object, and iterator cases.
|
---|
| 70 | var eachOfGeneric = (0, _doLimit2.default)(_eachOfLimit2.default, Infinity);
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Like [`each`]{@link module:Collections.each}, except that it passes the key (or index) as the second argument
|
---|
| 74 | * to the iteratee.
|
---|
| 75 | *
|
---|
| 76 | * @name eachOf
|
---|
| 77 | * @static
|
---|
| 78 | * @memberOf module:Collections
|
---|
| 79 | * @method
|
---|
| 80 | * @alias forEachOf
|
---|
| 81 | * @category Collection
|
---|
| 82 | * @see [async.each]{@link module:Collections.each}
|
---|
| 83 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
---|
| 84 | * @param {AsyncFunction} iteratee - A function to apply to each
|
---|
| 85 | * item in `coll`.
|
---|
| 86 | * The `key` is the item's key, or index in the case of an array.
|
---|
| 87 | * Invoked with (item, key, callback).
|
---|
| 88 | * @param {Function} [callback] - A callback which is called when all
|
---|
| 89 | * `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
---|
| 90 | * @example
|
---|
| 91 | *
|
---|
| 92 | * var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
|
---|
| 93 | * var configs = {};
|
---|
| 94 | *
|
---|
| 95 | * async.forEachOf(obj, function (value, key, callback) {
|
---|
| 96 | * fs.readFile(__dirname + value, "utf8", function (err, data) {
|
---|
| 97 | * if (err) return callback(err);
|
---|
| 98 | * try {
|
---|
| 99 | * configs[key] = JSON.parse(data);
|
---|
| 100 | * } catch (e) {
|
---|
| 101 | * return callback(e);
|
---|
| 102 | * }
|
---|
| 103 | * callback();
|
---|
| 104 | * });
|
---|
| 105 | * }, function (err) {
|
---|
| 106 | * if (err) console.error(err.message);
|
---|
| 107 | * // configs is now a map of JSON data
|
---|
| 108 | * doSomethingWith(configs);
|
---|
| 109 | * });
|
---|
| 110 | */
|
---|
| 111 | module.exports = exports['default']; |
---|