UNPKG

async-ray

Version:

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

173 lines 5.1 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.AsyncArray = void 0; const Methods = require("./methods"); /** * AsyncArray Class * * @class AsyncArray * @extends {Array<T>} * @template T */ class AsyncArray extends Array { /** * Creates an instance of AsyncArray * @param {...T[]} args * @memberof AsyncArray */ constructor(...args) { /*** * Why we need this ? * if we pass one element array with an number as below, * const foo = AsyncArray(...[20]); * Based https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Syntax * a new Array will be created with 20 empty elements, as below. * [null, null, ...] * In order to avoid that, we need the particular workaround */ if (args.length === 1 && typeof args[0] === 'number') { super(1); super[0] = args[0]; } else { super(...args); } } /** * Async Every method * * @param {Methods.CallBackFilter<T>} cb * @returns {Promise<boolean>} * @memberof AsyncArray */ aEvery(cb) { return __awaiter(this, void 0, void 0, function* () { return Methods.aEvery(this, cb); }); } /** * Async FlatMap method * * @template R * @param {Methods.CallBackFlatMap<T, R>} cb * @returns {Promise<R[]>} * @memberof AsyncArray */ aFlatMap(cb) { return __awaiter(this, void 0, void 0, function* () { return new AsyncArray(...(yield Methods.aFlatMap(this, cb))); }); } /** * Async Filter method * * @param {Methods.CallBackFilter<T>} cb * @returns {Promise<T[]>} * @memberof AsyncArray */ aFilter(cb) { return __awaiter(this, void 0, void 0, function* () { return new AsyncArray(...(yield Methods.aFilter(this, cb))); }); } /** * Async find method * * @param {Methods.CallBackFind<T>} cb * @returns {Promise<T | undefined>} * @memberof AsyncArray */ aFind(cb) { return __awaiter(this, void 0, void 0, function* () { return Methods.aFind(this, cb); }); } /** * Async findIndex method * * @param {Methods.CallBackFindIndex<T>} cb * @returns {Promise<number>} * @memberof AsyncArray */ aFindIndex(cb) { return __awaiter(this, void 0, void 0, function* () { return Methods.aFindIndex(this, cb); }); } /** * Async ForEach method * * @param {Methods.CallBackForEach<T>} cb * @returns {Promise<void>} * @memberof AsyncArray */ aForEach(cb) { return __awaiter(this, void 0, void 0, function* () { yield Methods.aForEach(this, cb); }); } /** * Async Map method * * @template R * @param {Methods.CallBackMap<T, R>} cb * @returns {Promise<R[]>} * @memberof AsyncArray */ aMap(cb) { return __awaiter(this, void 0, void 0, function* () { return new AsyncArray(...(yield Methods.aMap(this, cb))); }); } /** * Async Reduce method * * @template R * @param {Methods.CallBackReduce<T, R>} cb * @param {R} [initialValue] * @returns {Promise<T | R>} * @memberof AsyncArray */ aReduce(cb, initialValue) { return __awaiter(this, void 0, void 0, function* () { return Methods.aReduce(this, cb, initialValue); }); } /** * Async ReduceRight method * * @template R * @param {Methods.CallBackReduceRight<T, R>} cb * @param {R} [initialValue] * @returns {Promise<T | R>} * @memberof AsyncArray */ aReduceRight(cb, initialValue) { return __awaiter(this, void 0, void 0, function* () { return Methods.aReduceRight(this, cb, initialValue); }); } /** * Async Some method * * @param {Methods.CallBackFilter<T>} cb * @returns {Promise<boolean>} * @memberof AsyncArray */ aSome(cb) { return __awaiter(this, void 0, void 0, function* () { return Methods.aSome(this, cb); }); } } exports.AsyncArray = AsyncArray; //# sourceMappingURL=async_array.js.map