'use strict';
const { EachLimit } = require('./eachLimit');
const { setLimit } = require('./internal/collection');
const { DEFAULT_LIMIT, call3, callProxyReciever, clone } = require('./internal/util');
class TransformLimit extends EachLimit {
constructor(collection, limit, iterator, accumulator) {
if (typeof limit === 'function') {
accumulator = iterator;
iterator = limit;
limit = DEFAULT_LIMIT;
}
super(collection, limit, iterator, set);
if (accumulator !== undefined) {
this._result = accumulator;
}
}
_callResolve(bool) {
if (bool === false) {
this._promise._resolve(clone(this._result));
} else if (--this._rest === 0) {
this._promise._resolve(this._result);
} else if (this._callRest-- > 0) {
this._iterate();
}
}
}
module.exports = { transformLimit, TransformLimit };
function set(collection) {
setLimit.call(this, collection);
if (this._keys !== undefined || this._coll === undefined) {
if (this._result === undefined) {
this._result = {};
}
this._iterate = iterateObject;
} else {
if (this._result === undefined) {
this._result = [];
}
this._iterate = iterateArray;
}
return this;
}
function iterateArray() {
const index = this._index++;
callProxyReciever(call3(this._iterator, this._result, this._coll[index], index), this, index);
}
function iterateObject() {
const index = this._index++;
const key = this._keys[index];
callProxyReciever(call3(this._iterator, this._result, this._coll[key], key), this, index);
}
/**
* @param {Array|Object} collection
* @param {integer} [limit]
* @param {Function} iterator
* @param {Array|Object} [accumulator]
* @example
* const order = [];
* const collection = [1, 5, 3, 4, 2];
* const iterator = (result, num, index) => {
* return Aigle.delay(num * 10)
* .then(() => {
* order.push(num);
* result[index] = num;
* });
* };
* Aigle.transformLimit(collection, 2, iterator, {})
* .then(object => {
* console.log(object); // { '0': 1, '1': 5, '2': 3, '3': 4, '4': 2 }
* console.log(order); // [1, 5, 3, 4, 2]
* });
*
* @example
* const order = [];
* const collection = [1, 5, 3, 4, 2];
* const iterator = (result, num, index) => {
* return Aigle.delay(num * 10)
* .then(() => {
* order.push(num);
* result.push(num);
* });
* };
* Aigle.transformLimit(collection, 2, iterator, {})
* .then(array => {
* console.log(array); // [1, 5, 3, 4, 2]
* console.log(order); // [1, 5, 3, 4, 2]
* });
*
* @example
* const order = [];
* const collection = { a: 1, b: 5, c: 3, d: 4, e: 2 };
* const iterator = (result, num, key) => {
* return Aigle.delay(num * 10)
* .then(() => {
* order.push(num);
* result.push(num);
* return num !== 4;
* });
* };
* Aigle.transformLimit(collection, 2, iterator, [])
* .then(array => {
* console.log(array); // [1, 5, 3, 4]
* console.log(order); // [1, 5, 3, 4]
* });
*
* @example
* const order = [];
* const collection = { a: 1, b: 5, c: 3, d: 4, e: 2 };
* const iterator = (result, num, key) => {
* return Aigle.delay(num * 10)
* .then(() => {
* order.push(num);
* result.push(num);
* return num !== 4;
* });
* };
* Aigle.transformLimit(collection, iterator, [])
* .then(array => {
* console.log(array); // [1, 2, 3, 4]
* console.log(order); // [1, 2, 3, 4]
* });
*/
function transformLimit(collection, limit, iterator, accumulator) {
return new TransformLimit(collection, limit, iterator, accumulator)._execute();
}