eachOf.js 3.2 KB

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