eachOf.js 3.3 KB

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