groupByLimit.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = function (coll, limit, iteratee, callback) {
  6. callback = callback || _noop2.default;
  7. var _iteratee = (0, _wrapAsync2.default)(iteratee);
  8. (0, _mapLimit2.default)(coll, limit, function (val, callback) {
  9. _iteratee(val, function (err, key) {
  10. if (err) return callback(err);
  11. return callback(null, { key: key, val: val });
  12. });
  13. }, function (err, mapResults) {
  14. var result = {};
  15. // from MDN, handle object having an `hasOwnProperty` prop
  16. var hasOwnProperty = Object.prototype.hasOwnProperty;
  17. for (var i = 0; i < mapResults.length; i++) {
  18. if (mapResults[i]) {
  19. var key = mapResults[i].key;
  20. var val = mapResults[i].val;
  21. if (hasOwnProperty.call(result, key)) {
  22. result[key].push(val);
  23. } else {
  24. result[key] = [val];
  25. }
  26. }
  27. }
  28. return callback(err, result);
  29. });
  30. };
  31. var _noop = require('lodash/noop');
  32. var _noop2 = _interopRequireDefault(_noop);
  33. var _mapLimit = require('./mapLimit');
  34. var _mapLimit2 = _interopRequireDefault(_mapLimit);
  35. var _wrapAsync = require('./internal/wrapAsync');
  36. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  37. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  38. ;
  39. /**
  40. * The same as [`groupBy`]{@link module:Collections.groupBy} but runs a maximum of `limit` async operations at a time.
  41. *
  42. * @name groupByLimit
  43. * @static
  44. * @memberOf module:Collections
  45. * @method
  46. * @see [async.groupBy]{@link module:Collections.groupBy}
  47. * @category Collection
  48. * @param {Array|Iterable|Object} coll - A collection to iterate over.
  49. * @param {number} limit - The maximum number of async operations at a time.
  50. * @param {AsyncFunction} iteratee - An async function to apply to each item in
  51. * `coll`.
  52. * The iteratee should complete with a `key` to group the value under.
  53. * Invoked with (value, callback).
  54. * @param {Function} [callback] - A callback which is called when all `iteratee`
  55. * functions have finished, or an error occurs. Result is an `Object` whoses
  56. * properties are arrays of values which returned the corresponding key.
  57. */
  58. module.exports = exports['default'];