'use strict';
const { EachLimit } = require('./eachLimit');
const { setLimit } = require('./internal/collection');
class GroupByLimit extends EachLimit {
constructor(collection, limit, iterator) {
super(collection, limit, iterator, set);
this._result = {};
}
}
module.exports = { groupByLimit, GroupByLimit };
function set(collection) {
setLimit.call(this, collection);
this._callResolve = this._keys === undefined ? callResolveArray : callResolveObject;
return this;
}
function callResolveArray(key, index) {
if (this._result[key]) {
this._result[key].push(this._coll[index]);
} else {
this._result[key] = [this._coll[index]];
}
if (--this._rest === 0) {
this._promise._resolve(this._result);
} else if (this._callRest-- > 0) {
this._iterate();
}
}
function callResolveObject(key, index) {
if (this._result[key]) {
this._result[key].push(this._coll[this._keys[index]]);
} else {
this._result[key] = [this._coll[this._keys[index]]];
}
if (--this._rest === 0) {
this._promise._resolve(this._result);
} else if (this._callRest-- > 0) {
this._iterate();
}
}
/**
* @param {Array|Object} collection
* @param {integer} [limit=8]
* @param {Function} iterator
* @return {Aigle} Returns an Aigle instance
* @example
* const order = [];
* const collection = [1, 5, 3, 4, 2];
* const iterator = (num, index) => {
* return Aigle.delay(num * 10)
* .then(() => {
* order.push(num);
* return num % 2;
* });
* };
* Aigle.groupByLimit(collection, 2, iterator)
* .then(object => {
* console.log(object); // { '0': [2, 4], '1': [1, 3, 5] };
* console.log(order); // [1, 3, 5, 2, 4];
* });
*
* @example
* const order = [];
* const collection = {
* task1: 1,
* task2: 5,
* task3: 3,
* task4: 4,
* task5: 2
* };
* const iterator = (num, key) => {
* return Aigle.delay(num * 10)
* .then(() => {
* order.push(num);
* return num % 2;
* });
* };
* Aigle.groupByLimit(collection, 2, iterator)
* .then(object => {
* console.log(object); // { '0': [2, 4], '1': [1, 3, 5] };
* console.log(order); // [1, 3, 5, 2, 4];
* });
*
* @example
* const order = [];
* const collection = [1, 5, 3, 4, 2];
* const iterator = num => {
* return Aigle.delay(num * 10)
* .then(() => {
* order.push(num);
* return num % 2;
* });
* };
* Aigle.groupByLimit(collection, iterator)
* .then(object => {
* console.log(object); // { '0': [2, 4], '1': [1, 3, 5] };
* console.log(order); // [1, 2, 3, 4, 5];
* });
*/
function groupByLimit(collection, limit, iterator) {
return new GroupByLimit(collection, limit, iterator)._execute();
}