UNPKG

async-ray

Version:

Allow perform every, filter, find, findIndex, forEach, map, reduce, reduceRight and some on array using Async callback

221 lines 6.17 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Chainable = void 0; const async_array_1 = require("./async_array"); const methods = require("./methods"); /** * Chainable function handler * * @export * @class Chainable * @template T */ class Chainable { /** * Creates an instance of Chainable. * @param {T[]} input * @memberof Chainable */ constructor(input) { this.input = input; /** * Call Stack * * @private * @type {SingleCall[]} * @memberof Chainable */ this.callQueue = []; } /** * aEvery method of Async-Ray lib * * @param {methods.CallBackEvery<T>} cb * @returns {Promise<any>} * @memberof Chainable */ aEvery(cb) { return this.addNoneChainableMethod(methods.aEvery, cb); } /** * aFilter method of Async-Ray lib * * @param {methods.CallBackFilter<T>} cb * @returns {Chainable<T>} * @memberof Chainable */ aFilter(cb) { return this.addChainableMethod(methods.aFilter, cb); } /** * aFindIndex method of Async-Ray lib * * @param {methods.CallBackFindIndex<T>} cb * @returns {Promise<any>} * @memberof Chainable */ aFindIndex(cb) { return this.addNoneChainableMethod(methods.aFindIndex, cb); } /** * aFind method of Async-Ray lib * * @param {methods.CallBackFind<T>} cb * @returns {Promise<any>} * @memberof Chainable */ aFind(cb) { return this.addNoneChainableMethod(methods.aFind, cb); } /** * aForEach method of Async-Ray lib * * @param {methods.CallBackForEach<T>} cb * @returns {Promise<any>} * @memberof Chainable */ aForEach(cb) { return this.addNoneChainableMethod(methods.aForEach, cb); } /** * aMap method of Async-Ray lib * * @template R * @param {methods.CallBackMap<T, R>} cb * @returns {Chainable<T>} * @memberof Chainable */ aMap(cb) { return this.addChainableMethod(methods.aMap, cb); } /** * aReduceRight method of Async-Ray lib * * @template R * @param {methods.CallBackReduceRight<T, R>} cb * @param {R} [initialValue] * @returns {Promise<any>} * @memberof Chainable */ aReduceRight(cb, initialValue) { return this.addNoneChainableMethod(methods.aReduceRight, cb, initialValue); } /** * aReduce method of Async-Ray lib * * @template R * @param {methods.CallBackReduce<T, R>} cb * @param {R} [initialValue] * @returns {Promise<any>} * @memberof Chainable */ aReduce(cb, initialValue) { return this.addNoneChainableMethod(methods.aReduce, cb, initialValue); } /** * aSome method of Async-Ray lib * * @template R * @param {methods.CallBackSome<T>} cb * @returns {Promise<any>} * @memberof Chainable */ aSome(cb) { return this.addNoneChainableMethod(methods.aSome, cb); } /** * aFlatMap method of Async-Ray lib * * @template R * @param {CallBackFlatMap<T, R>} cb * @returns {Chainable<T>} * @memberof Chainable */ aFlatMap(cb) { return this.addChainableMethod(methods.aFlatMap, cb); } /** * Process the call stack * * @returns {Promise<any>} * @memberof Chainable */ process() { return __awaiter(this, void 0, void 0, function* () { let currentInput = this.input; while (this.callQueue.length) { try { const nextCall = this.callQueue.shift(); currentInput = yield nextCall.method.call(null, currentInput, nextCall.callBack, nextCall.additional); } catch (error) { this.clear(); throw error; } } if (Array.isArray(currentInput)) { return new async_array_1.AsyncArray(...currentInput); } return currentInput; }); } /** * Add chainable method to the call queue * * @private * @param {Function} method * @param {CallBacks} callBack * @param {*} [additional] * @returns {Chainable<T>} * @memberof Chainable */ addChainableMethod(method, callBack, additional) { this.add(method, callBack, additional); return this; } /** * Add none chainable method to the queue and execute the chaining process * * @private * @param {Function} method * @param {CallBacks} callBack * @param {*} [additional] * @returns {Promise<any>} * @memberof Chainable */ addNoneChainableMethod(method, callBack, additional) { this.add(method, callBack, additional); return this.process(); } /** * Add element to the call queue * * @private * @param {Function} method * @param {CallBacks} callBack * @memberof Chainable */ add(method, callBack, additional) { this.callQueue.push({ method, callBack, additional }); } /** * clear the call queue * * @private * @memberof Chainable */ clear() { this.callQueue = []; } } exports.Chainable = Chainable; //# sourceMappingURL=chainable.js.map