groupBy.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _doLimit = require('./internal/doLimit');
  6. var _doLimit2 = _interopRequireDefault(_doLimit);
  7. var _groupByLimit = require('./groupByLimit');
  8. var _groupByLimit2 = _interopRequireDefault(_groupByLimit);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /**
  11. * Returns a new object, where each value corresponds to an array of items, from
  12. * `coll`, that returned the corresponding key. That is, the keys of the object
  13. * correspond to the values passed to the `iteratee` callback.
  14. *
  15. * Note: Since this function applies the `iteratee` to each item in parallel,
  16. * there is no guarantee that the `iteratee` functions will complete in order.
  17. * However, the values for each key in the `result` will be in the same order as
  18. * the original `coll`. For Objects, the values will roughly be in the order of
  19. * the original Objects' keys (but this can vary across JavaScript engines).
  20. *
  21. * @name groupBy
  22. * @static
  23. * @memberOf module:Collections
  24. * @method
  25. * @category Collection
  26. * @param {Array|Iterable|Object} coll - A collection to iterate over.
  27. * @param {AsyncFunction} iteratee - An async function to apply to each item in
  28. * `coll`.
  29. * The iteratee should complete with a `key` to group the value under.
  30. * Invoked with (value, callback).
  31. * @param {Function} [callback] - A callback which is called when all `iteratee`
  32. * functions have finished, or an error occurs. Result is an `Object` whoses
  33. * properties are arrays of values which returned the corresponding key.
  34. * @example
  35. *
  36. * async.groupBy(['userId1', 'userId2', 'userId3'], function(userId, callback) {
  37. * db.findById(userId, function(err, user) {
  38. * if (err) return callback(err);
  39. * return callback(null, user.age);
  40. * });
  41. * }, function(err, result) {
  42. * // result is object containing the userIds grouped by age
  43. * // e.g. { 30: ['userId1', 'userId3'], 42: ['userId2']};
  44. * });
  45. */
  46. exports.default = (0, _doLimit2.default)(_groupByLimit2.default, Infinity);
  47. module.exports = exports['default'];