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