groupBy.js

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