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