UNPKG

itertools-ts

Version:

Extended itertools port for TypeScript and JavaScript. Provides a huge set of functions for working with iterable collections (including async ones)

1,263 lines 95.3 kB
"use strict"; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i; function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; } function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.map = map; exports.mapAsync = mapAsync; exports.compress = compress; exports.compressAsync = compressAsync; exports.dropWhile = dropWhile; exports.dropWhileAsync = dropWhileAsync; exports.takeWhile = takeWhile; exports.takeWhileAsync = takeWhileAsync; exports.repeat = repeat; exports.repeatAsync = repeatAsync; exports.flatMap = flatMap; exports.flatMapAsync = flatMapAsync; exports.flatten = flatten; exports.flattenAsync = flattenAsync; exports.filter = filter; exports.filterAsync = filterAsync; exports.chunkwiseOverlap = chunkwiseOverlap; exports.chunkwiseOverlapAsync = chunkwiseOverlapAsync; exports.chunkwise = chunkwise; exports.chunkwiseAsync = chunkwiseAsync; exports.pairwise = pairwise; exports.pairwiseAsync = pairwiseAsync; exports.limit = limit; exports.limitAsync = limitAsync; exports.enumerate = enumerate; exports.enumerateAsync = enumerateAsync; exports.slice = slice; exports.sliceAsync = sliceAsync; exports.keys = keys; exports.keysAsync = keysAsync; exports.skip = skip; exports.skipAsync = skipAsync; exports.values = values; exports.valuesAsync = valuesAsync; exports.groupBy = groupBy; exports.groupByAsync = groupByAsync; exports.sort = sort; exports.sortAsync = sortAsync; var transform_1 = require("./transform"); var exceptions_1 = require("./exceptions"); var summary_1 = require("./summary"); var set_1 = require("./set"); var multi_1 = require("./multi"); /** * Map a function onto every element of the iteration. * * @param data * @param mapper */ function map(data, mapper) { var _a, _b, datum, e_1_1; var e_1, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: _d.trys.push([0, 5, 6, 7]); _a = __values((0, transform_1.toIterable)(data)), _b = _a.next(); _d.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 4]; datum = _b.value; return [4 /*yield*/, mapper(datum)]; case 2: _d.sent(); _d.label = 3; case 3: _b = _a.next(); return [3 /*break*/, 1]; case 4: return [3 /*break*/, 7]; case 5: e_1_1 = _d.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 7]; case 6: try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 7: return [2 /*return*/]; } }); } /** * Map a function onto every element of the iteration for async collections. * * Mapper may be also async. * * @param data * @param mapper */ function mapAsync(data, mapper) { return __asyncGenerator(this, arguments, function mapAsync_1() { var _a, _b, _c, datum, e_2_1; var _d, e_2, _e, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: _g.trys.push([0, 8, 9, 14]); _a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data)); _g.label = 1; case 1: return [4 /*yield*/, __await(_b.next())]; case 2: if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 7]; _f = _c.value; _a = false; datum = _f; return [4 /*yield*/, __await(mapper(datum))]; case 3: return [4 /*yield*/, __await.apply(void 0, [_g.sent()])]; case 4: return [4 /*yield*/, _g.sent()]; case 5: _g.sent(); _g.label = 6; case 6: _a = true; return [3 /*break*/, 1]; case 7: return [3 /*break*/, 14]; case 8: e_2_1 = _g.sent(); e_2 = { error: e_2_1 }; return [3 /*break*/, 14]; case 9: _g.trys.push([9, , 12, 13]); if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 11]; return [4 /*yield*/, __await(_e.call(_b))]; case 10: _g.sent(); _g.label = 11; case 11: return [3 /*break*/, 13]; case 12: if (e_2) throw e_2.error; return [7 /*endfinally*/]; case 13: return [7 /*endfinally*/]; case 14: return [2 /*return*/]; } }); }); } /** * Compress an iterable by filtering out data that is not selected. * * Selectors indicate which data. True value selects item. False value filters out data. * * @param data * @param selectors */ function compress(data, selectors) { var _a, _b, _c, datum, selector, e_3_1; var e_3, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 5, 6, 7]); _a = __values((0, multi_1.zip)(data, selectors)), _b = _a.next(); _e.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 4]; _c = __read(_b.value, 2), datum = _c[0], selector = _c[1]; if (!selector) return [3 /*break*/, 3]; return [4 /*yield*/, datum]; case 2: _e.sent(); _e.label = 3; case 3: _b = _a.next(); return [3 /*break*/, 1]; case 4: return [3 /*break*/, 7]; case 5: e_3_1 = _e.sent(); e_3 = { error: e_3_1 }; return [3 /*break*/, 7]; case 6: try { if (_b && !_b.done && (_d = _a.return)) _d.call(_a); } finally { if (e_3) throw e_3.error; } return [7 /*endfinally*/]; case 7: return [2 /*return*/]; } }); } /** * Compress an async iterable by filtering out data that is not selected. * * Selectors indicate which data. True value selects item. False value filters out data. * * Selectors may be also async collection. * * @param data * @param selectors */ function compressAsync(data, selectors) { return __asyncGenerator(this, arguments, function compressAsync_1() { var _a, _b, _c, _d, datum, selector, e_4_1; var _e, e_4, _f, _g; return __generator(this, function (_h) { switch (_h.label) { case 0: _h.trys.push([0, 7, 8, 13]); _a = true, _b = __asyncValues((0, multi_1.zipAsync)(data, selectors)); _h.label = 1; case 1: return [4 /*yield*/, __await(_b.next())]; case 2: if (!(_c = _h.sent(), _e = _c.done, !_e)) return [3 /*break*/, 6]; _g = _c.value; _a = false; _d = __read(_g, 2), datum = _d[0], selector = _d[1]; if (!selector) return [3 /*break*/, 5]; return [4 /*yield*/, __await(datum)]; case 3: return [4 /*yield*/, _h.sent()]; case 4: _h.sent(); _h.label = 5; case 5: _a = true; return [3 /*break*/, 1]; case 6: return [3 /*break*/, 13]; case 7: e_4_1 = _h.sent(); e_4 = { error: e_4_1 }; return [3 /*break*/, 13]; case 8: _h.trys.push([8, , 11, 12]); if (!(!_a && !_e && (_f = _b.return))) return [3 /*break*/, 10]; return [4 /*yield*/, __await(_f.call(_b))]; case 9: _h.sent(); _h.label = 10; case 10: return [3 /*break*/, 12]; case 11: if (e_4) throw e_4.error; return [7 /*endfinally*/]; case 12: return [7 /*endfinally*/]; case 13: return [2 /*return*/]; } }); }); } /** * Drop elements from the iterable while the predicate function is true. * * Once the predicate function returns false once, all remaining elements are returned. * * @param data * @param predicate */ function dropWhile(data, predicate) { var drop, _a, _b, datum, e_5_1; var e_5, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: drop = true; _d.label = 1; case 1: _d.trys.push([1, 9, 10, 11]); _a = __values((0, transform_1.toIterable)(data)), _b = _a.next(); _d.label = 2; case 2: if (!!_b.done) return [3 /*break*/, 8]; datum = _b.value; if (!drop) return [3 /*break*/, 5]; if (!!predicate(datum)) return [3 /*break*/, 4]; drop = false; return [4 /*yield*/, datum]; case 3: _d.sent(); return [3 /*break*/, 7]; case 4: return [3 /*break*/, 7]; case 5: return [4 /*yield*/, datum]; case 6: _d.sent(); _d.label = 7; case 7: _b = _a.next(); return [3 /*break*/, 2]; case 8: return [3 /*break*/, 11]; case 9: e_5_1 = _d.sent(); e_5 = { error: e_5_1 }; return [3 /*break*/, 11]; case 10: try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_5) throw e_5.error; } return [7 /*endfinally*/]; case 11: return [2 /*return*/]; } }); } /** * Drop elements from the async iterable while the predicate function is true. * * Once the predicate function returns false once, all remaining elements are returned. * * Predicate may be also async. * * @param data * @param predicate */ function dropWhileAsync(data, predicate) { return __asyncGenerator(this, arguments, function dropWhileAsync_1() { var drop, _a, _b, _c, datum, e_6_1; var _d, e_6, _e, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: drop = true; _g.label = 1; case 1: _g.trys.push([1, 13, 14, 19]); _a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data)); _g.label = 2; case 2: return [4 /*yield*/, __await(_b.next())]; case 3: if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 12]; _f = _c.value; _a = false; datum = _f; if (!drop) return [3 /*break*/, 8]; return [4 /*yield*/, __await(predicate(datum))]; case 4: if (!!(_g.sent())) return [3 /*break*/, 7]; drop = false; return [4 /*yield*/, __await(datum)]; case 5: return [4 /*yield*/, _g.sent()]; case 6: _g.sent(); return [3 /*break*/, 11]; case 7: return [3 /*break*/, 11]; case 8: return [4 /*yield*/, __await(datum)]; case 9: return [4 /*yield*/, _g.sent()]; case 10: _g.sent(); _g.label = 11; case 11: _a = true; return [3 /*break*/, 2]; case 12: return [3 /*break*/, 19]; case 13: e_6_1 = _g.sent(); e_6 = { error: e_6_1 }; return [3 /*break*/, 19]; case 14: _g.trys.push([14, , 17, 18]); if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 16]; return [4 /*yield*/, __await(_e.call(_b))]; case 15: _g.sent(); _g.label = 16; case 16: return [3 /*break*/, 18]; case 17: if (e_6) throw e_6.error; return [7 /*endfinally*/]; case 18: return [7 /*endfinally*/]; case 19: return [2 /*return*/]; } }); }); } /** * Return elements from the iterable as long as the predicate is true. * * If no predicate is provided, the boolean value of the data is used. * * @param data * @param predicate */ function takeWhile(data, predicate) { var _a, _b, datum, e_7_1; var e_7, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: _d.trys.push([0, 6, 7, 8]); _a = __values((0, transform_1.toIterable)(data)), _b = _a.next(); _d.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 5]; datum = _b.value; if (!predicate(datum)) return [3 /*break*/, 3]; return [4 /*yield*/, datum]; case 2: _d.sent(); return [3 /*break*/, 4]; case 3: return [3 /*break*/, 5]; case 4: _b = _a.next(); return [3 /*break*/, 1]; case 5: return [3 /*break*/, 8]; case 6: e_7_1 = _d.sent(); e_7 = { error: e_7_1 }; return [3 /*break*/, 8]; case 7: try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_7) throw e_7.error; } return [7 /*endfinally*/]; case 8: return [2 /*return*/]; } }); } /** * Return elements from the async iterable as long as the predicate is true. * * Predicate may be also async. * * If no predicate is provided, the boolean value of the data is used. * * @param data * @param predicate */ function takeWhileAsync(data, predicate) { return __asyncGenerator(this, arguments, function takeWhileAsync_1() { var _a, _b, _c, datum, e_8_1; var _d, e_8, _e, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: _g.trys.push([0, 9, 10, 15]); _a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data)); _g.label = 1; case 1: return [4 /*yield*/, __await(_b.next())]; case 2: if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 8]; _f = _c.value; _a = false; datum = _f; return [4 /*yield*/, __await(predicate(datum))]; case 3: if (!_g.sent()) return [3 /*break*/, 6]; return [4 /*yield*/, __await(datum)]; case 4: return [4 /*yield*/, _g.sent()]; case 5: _g.sent(); return [3 /*break*/, 7]; case 6: return [3 /*break*/, 8]; case 7: _a = true; return [3 /*break*/, 1]; case 8: return [3 /*break*/, 15]; case 9: e_8_1 = _g.sent(); e_8 = { error: e_8_1 }; return [3 /*break*/, 15]; case 10: _g.trys.push([10, , 13, 14]); if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 12]; return [4 /*yield*/, __await(_e.call(_b))]; case 11: _g.sent(); _g.label = 12; case 12: return [3 /*break*/, 14]; case 13: if (e_8) throw e_8.error; return [7 /*endfinally*/]; case 14: return [7 /*endfinally*/]; case 15: return [2 /*return*/]; } }); }); } /** * Repeat an item. * * @param item * @param repetitions */ function repeat(item, repetitions) { var i; return __generator(this, function (_a) { switch (_a.label) { case 0: if (repetitions < 0) { throw new exceptions_1.InvalidArgumentError("Number of repetitions cannot be negative: ".concat(repetitions)); } i = repetitions; _a.label = 1; case 1: if (!(i > 0)) return [3 /*break*/, 4]; return [4 /*yield*/, item]; case 2: _a.sent(); _a.label = 3; case 3: --i; return [3 /*break*/, 1]; case 4: return [2 /*return*/]; } }); } /** * Repeat an item given as promise. * * @param item * @param repetitions */ function repeatAsync(item, repetitions) { return __asyncGenerator(this, arguments, function repeatAsync_1() { var value, i; return __generator(this, function (_a) { switch (_a.label) { case 0: if (repetitions < 0) { throw new exceptions_1.InvalidArgumentError("Number of repetitions cannot be negative: ".concat(repetitions)); } return [4 /*yield*/, __await(item)]; case 1: value = _a.sent(); i = repetitions; _a.label = 2; case 2: if (!(i > 0)) return [3 /*break*/, 6]; return [4 /*yield*/, __await(value)]; case 3: return [4 /*yield*/, _a.sent()]; case 4: _a.sent(); _a.label = 5; case 5: --i; return [3 /*break*/, 2]; case 6: return [2 /*return*/]; } }); }); } /** * Returns a new collection formed by applying a given callback mapper function to each element * of the given collection, and then flattening the result by one level. * * The mapper function can return scalar or collections as a result. * * @param data * @param mapper */ function flatMap(data, mapper) { var _a, _b, datum, unflattened, _c, _d, flattenedItem, e_9_1, e_10_1; var e_10, _e, e_9, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: _g.trys.push([0, 14, 15, 16]); _a = __values((0, transform_1.toIterable)(data)), _b = _a.next(); _g.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 13]; datum = _b.value; unflattened = mapper(datum, mapper); if (!(0, summary_1.isIterable)(unflattened)) return [3 /*break*/, 10]; _g.label = 2; case 2: _g.trys.push([2, 7, 8, 9]); _c = (e_9 = void 0, __values((0, transform_1.toIterable)(unflattened))), _d = _c.next(); _g.label = 3; case 3: if (!!_d.done) return [3 /*break*/, 6]; flattenedItem = _d.value; return [4 /*yield*/, flattenedItem]; case 4: _g.sent(); _g.label = 5; case 5: _d = _c.next(); return [3 /*break*/, 3]; case 6: return [3 /*break*/, 9]; case 7: e_9_1 = _g.sent(); e_9 = { error: e_9_1 }; return [3 /*break*/, 9]; case 8: try { if (_d && !_d.done && (_f = _c.return)) _f.call(_c); } finally { if (e_9) throw e_9.error; } return [7 /*endfinally*/]; case 9: return [3 /*break*/, 12]; case 10: return [4 /*yield*/, unflattened]; case 11: _g.sent(); _g.label = 12; case 12: _b = _a.next(); return [3 /*break*/, 1]; case 13: return [3 /*break*/, 16]; case 14: e_10_1 = _g.sent(); e_10 = { error: e_10_1 }; return [3 /*break*/, 16]; case 15: try { if (_b && !_b.done && (_e = _a.return)) _e.call(_a); } finally { if (e_10) throw e_10.error; } return [7 /*endfinally*/]; case 16: return [2 /*return*/]; } }); } /** * Returns a new async collection formed by applying a given callback mapper function to each element * of the given async collection, and then flattening the result by one level. * * The mapper function can return scalar or collections as a result. * * The mapper function may be also async. * * @param data * @param mapper */ function flatMapAsync(data, mapper) { return __asyncGenerator(this, arguments, function flatMapAsync_1() { var _a, _b, _c, datum, unflattened, _d, _e, _f, flattenedItem, e_11_1, e_12_1; var _g, e_12, _h, _j, _k, e_11, _l, _m; return __generator(this, function (_o) { switch (_o.label) { case 0: _o.trys.push([0, 23, 24, 29]); _a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data)); _o.label = 1; case 1: return [4 /*yield*/, __await(_b.next())]; case 2: if (!(_c = _o.sent(), _g = _c.done, !_g)) return [3 /*break*/, 22]; _j = _c.value; _a = false; datum = _j; return [4 /*yield*/, __await(mapper(datum, mapper))]; case 3: unflattened = _o.sent(); if (!((0, summary_1.isIterable)(unflattened) || (0, summary_1.isAsyncIterable)(unflattened))) return [3 /*break*/, 18]; _o.label = 4; case 4: _o.trys.push([4, 11, 12, 17]); _d = true, _e = (e_11 = void 0, __asyncValues((0, transform_1.toAsyncIterable)(unflattened))); _o.label = 5; case 5: return [4 /*yield*/, __await(_e.next())]; case 6: if (!(_f = _o.sent(), _k = _f.done, !_k)) return [3 /*break*/, 10]; _m = _f.value; _d = false; flattenedItem = _m; return [4 /*yield*/, __await(flattenedItem)]; case 7: return [4 /*yield*/, _o.sent()]; case 8: _o.sent(); _o.label = 9; case 9: _d = true; return [3 /*break*/, 5]; case 10: return [3 /*break*/, 17]; case 11: e_11_1 = _o.sent(); e_11 = { error: e_11_1 }; return [3 /*break*/, 17]; case 12: _o.trys.push([12, , 15, 16]); if (!(!_d && !_k && (_l = _e.return))) return [3 /*break*/, 14]; return [4 /*yield*/, __await(_l.call(_e))]; case 13: _o.sent(); _o.label = 14; case 14: return [3 /*break*/, 16]; case 15: if (e_11) throw e_11.error; return [7 /*endfinally*/]; case 16: return [7 /*endfinally*/]; case 17: return [3 /*break*/, 21]; case 18: return [4 /*yield*/, __await(unflattened)]; case 19: return [4 /*yield*/, _o.sent()]; case 20: _o.sent(); _o.label = 21; case 21: _a = true; return [3 /*break*/, 1]; case 22: return [3 /*break*/, 29]; case 23: e_12_1 = _o.sent(); e_12 = { error: e_12_1 }; return [3 /*break*/, 29]; case 24: _o.trys.push([24, , 27, 28]); if (!(!_a && !_g && (_h = _b.return))) return [3 /*break*/, 26]; return [4 /*yield*/, __await(_h.call(_b))]; case 25: _o.sent(); _o.label = 26; case 26: return [3 /*break*/, 28]; case 27: if (e_12) throw e_12.error; return [7 /*endfinally*/]; case 28: return [7 /*endfinally*/]; case 29: return [2 /*return*/]; } }); }); } /** * Flatten an iterable by a number of dimensions. * * Ex: [[1, 2], [3, 4], 5] => [1, 2, 3, 4, 5] // Flattened by one dimension * * @param data * @param dimensions */ function flatten(data, dimensions) { var _a, _b, datum, e_13_1, _c, _d, datum, _e, _f, subDatum, e_14_1, e_15_1; var e_13, _g, e_15, _h, e_14, _j; if (dimensions === void 0) { dimensions = Infinity; } return __generator(this, function (_k) { switch (_k.label) { case 0: if (!(dimensions < 1)) return [3 /*break*/, 9]; _k.label = 1; case 1: _k.trys.push([1, 6, 7, 8]); _a = __values((0, transform_1.toIterable)(data)), _b = _a.next(); _k.label = 2; case 2: if (!!_b.done) return [3 /*break*/, 5]; datum = _b.value; if (data instanceof Map) { datum = datum[1]; } return [4 /*yield*/, datum]; case 3: _k.sent(); _k.label = 4; case 4: _b = _a.next(); return [3 /*break*/, 2]; case 5: return [3 /*break*/, 8]; case 6: e_13_1 = _k.sent(); e_13 = { error: e_13_1 }; return [3 /*break*/, 8]; case 7: try { if (_b && !_b.done && (_g = _a.return)) _g.call(_a); } finally { if (e_13) throw e_13.error; } return [7 /*endfinally*/]; case 8: return [2 /*return*/]; case 9: _k.trys.push([9, 23, 24, 25]); _c = __values((0, transform_1.toIterable)(data)), _d = _c.next(); _k.label = 10; case 10: if (!!_d.done) return [3 /*break*/, 22]; datum = _d.value; if (data instanceof Map) { datum = datum[1]; } if (!(((0, summary_1.isIterable)(datum) || (0, summary_1.isIterator)(datum)) && !(0, summary_1.isString)(datum))) return [3 /*break*/, 19]; _k.label = 11; case 11: _k.trys.push([11, 16, 17, 18]); _e = (e_14 = void 0, __values(flatten(datum, dimensions - 1))), _f = _e.next(); _k.label = 12; case 12: if (!!_f.done) return [3 /*break*/, 15]; subDatum = _f.value; return [4 /*yield*/, subDatum]; case 13: _k.sent(); _k.label = 14; case 14: _f = _e.next(); return [3 /*break*/, 12]; case 15: return [3 /*break*/, 18]; case 16: e_14_1 = _k.sent(); e_14 = { error: e_14_1 }; return [3 /*break*/, 18]; case 17: try { if (_f && !_f.done && (_j = _e.return)) _j.call(_e); } finally { if (e_14) throw e_14.error; } return [7 /*endfinally*/]; case 18: return [3 /*break*/, 21]; case 19: return [4 /*yield*/, datum]; case 20: _k.sent(); _k.label = 21; case 21: _d = _c.next(); return [3 /*break*/, 10]; case 22: return [3 /*break*/, 25]; case 23: e_15_1 = _k.sent(); e_15 = { error: e_15_1 }; return [3 /*break*/, 25]; case 24: try { if (_d && !_d.done && (_h = _c.return)) _h.call(_c); } finally { if (e_15) throw e_15.error; } return [7 /*endfinally*/]; case 25: return [2 /*return*/]; } }); } /** * Flatten an async iterable by a number of dimensions. * * Ex: [[1, 2], [3, 4], 5] => [1, 2, 3, 4, 5] // Flattened by one dimension * * @param data * @param dimensions */ function flattenAsync(data_1) { return __asyncGenerator(this, arguments, function flattenAsync_1(data, dimensions) { var _a, _b, _c, datum, e_16_1, _d, _e, _f, datum, _g, _h, _j, subDatum, e_17_1, e_18_1; var _k, e_16, _l, _m, _o, e_18, _p, _q, _r, e_17, _s, _t; if (dimensions === void 0) { dimensions = Infinity; } return __generator(this, function (_u) { switch (_u.label) { case 0: if (!(dimensions < 1)) return [3 /*break*/, 16]; _u.label = 1; case 1: _u.trys.push([1, 8, 9, 14]); _a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data)); _u.label = 2; case 2: return [4 /*yield*/, __await(_b.next())]; case 3: if (!(_c = _u.sent(), _k = _c.done, !_k)) return [3 /*break*/, 7]; _m = _c.value; _a = false; datum = _m; if (data instanceof Map) { datum = datum[1]; } return [4 /*yield*/, __await(datum)]; case 4: return [4 /*yield*/, _u.sent()]; case 5: _u.sent(); _u.label = 6; case 6: _a = true; return [3 /*break*/, 2]; case 7: return [3 /*break*/, 14]; case 8: e_16_1 = _u.sent(); e_16 = { error: e_16_1 }; return [3 /*break*/, 14]; case 9: _u.trys.push([9, , 12, 13]); if (!(!_a && !_k && (_l = _b.return))) return [3 /*break*/, 11]; return [4 /*yield*/, __await(_l.call(_b))]; case 10: _u.sent(); _u.label = 11; case 11: return [3 /*break*/, 13]; case 12: if (e_16) throw e_16.error; return [7 /*endfinally*/]; case 13: return [7 /*endfinally*/]; case 14: return [4 /*yield*/, __await(void 0)]; case 15: return [2 /*return*/, _u.sent()]; case 16: _u.trys.push([16, 38, 39, 44]); _d = true, _e = __asyncValues((0, transform_1.toAsyncIterable)(data)); _u.label = 17; case 17: return [4 /*yield*/, __await(_e.next())]; case 18: if (!(_f = _u.sent(), _o = _f.done, !_o)) return [3 /*break*/, 37]; _q = _f.value; _d = false; datum = _q; if (data instanceof Map) { datum = datum[1]; } if (!(((0, summary_1.isAsyncIterable)(datum) || (0, summary_1.isIterable)(datum) || (0, summary_1.isIterator)(datum)) && !(0, summary_1.isString)(datum))) return [3 /*break*/, 33]; _u.label = 19; case 19: _u.trys.push([19, 26, 27, 32]); _g = true, _h = (e_17 = void 0, __asyncValues(flattenAsync(datum, dimensions - 1))); _u.label = 20; case 20: return [4 /*yield*/, __await(_h.next())]; case 21: if (!(_j = _u.sent(), _r = _j.done, !_r)) return [3 /*break*/, 25]; _t = _j.value; _g = false; subDatum = _t; return [4 /*yield*/, __await(subDatum)]; case 22: return [4 /*yield*/, _u.sent()]; case 23: _u.sent(); _u.label = 24; case 24: _g = true; return [3 /*break*/, 20]; case 25: return [3 /*break*/, 32]; case 26: e_17_1 = _u.sent(); e_17 = { error: e_17_1 }; return [3 /*break*/, 32]; case 27: _u.trys.push([27, , 30, 31]); if (!(!_g && !_r && (_s = _h.return))) return [3 /*break*/, 29]; return [4 /*yield*/, __await(_s.call(_h))]; case 28: _u.sent(); _u.label = 29; case 29: return [3 /*break*/, 31]; case 30: if (e_17) throw e_17.error; return [7 /*endfinally*/]; case 31: return [7 /*endfinally*/]; case 32: return [3 /*break*/, 36]; case 33: return [4 /*yield*/, __await(datum)]; case 34: return [4 /*yield*/, _u.sent()]; case 35: _u.sent(); _u.label = 36; case 36: _d = true; return [3 /*break*/, 17]; case 37: return [3 /*break*/, 44]; case 38: e_18_1 = _u.sent(); e_18 = { error: e_18_1 }; return [3 /*break*/, 44]; case 39: _u.trys.push([39, , 42, 43]); if (!(!_d && !_o && (_p = _e.return))) return [3 /*break*/, 41]; return [4 /*yield*/, __await(_p.call(_e))]; case 40: _u.sent(); _u.label = 41; case 41: return [3 /*break*/, 43]; case 42: if (e_18) throw e_18.error; return [7 /*endfinally*/]; case 43: return [7 /*endfinally*/]; case 44: return [2 /*return*/]; } }); }); } /** * Filter out elements from the iterable only returning elements where there predicate function is true. * * @param data * @param predicate */ function filter(data, predicate) { var _a, _b, datum, e_19_1; var e_19, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: _d.trys.push([0, 5, 6, 7]); _a = __values((0, transform_1.toIterable)(data)), _b = _a.next(); _d.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 4]; datum = _b.value; if (!predicate(datum)) return [3 /*break*/, 3]; return [4 /*yield*/, datum]; case 2: _d.sent(); _d.label = 3; case 3: _b = _a.next(); return [3 /*break*/, 1]; case 4: return [3 /*break*/, 7]; case 5: e_19_1 = _d.sent(); e_19 = { error: e_19_1 }; return [3 /*break*/, 7]; case 6: try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_19) throw e_19.error; } return [7 /*endfinally*/]; case 7: return [2 /*return*/]; } }); } /** * Filter out elements from the async iterable only returning elements where there predicate function is true. * * Predicate may be also async. * * @param data * @param predicate */ function filterAsync(data, predicate) { return __asyncGenerator(this, arguments, function filterAsync_1() { var _a, _b, _c, datum, e_20_1; var _d, e_20, _e, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: _g.trys.push([0, 8, 9, 14]); _a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data)); _g.label = 1; case 1: return [4 /*yield*/, __await(_b.next())]; case 2: if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 7]; _f = _c.value; _a = false; datum = _f; return [4 /*yield*/, __await(predicate(datum))]; case 3: if (!_g.sent()) return [3 /*break*/, 6]; return [4 /*yield*/, __await(datum)]; case 4: return [4 /*yield*/, _g.sent()]; case 5: _g.sent(); _g.label = 6; case 6: _a = true; return [3 /*break*/, 1]; case 7: return [3 /*break*/, 14]; case 8: e_20_1 = _g.sent(); e_20 = { error: e_20_1 }; return [3 /*break*/, 14]; case 9: _g.trys.push([9, , 12, 13]); if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 11]; return [4 /*yield*/, __await(_e.call(_b))]; case 10: _g.sent(); _g.label = 11; case 11: return [3 /*break*/, 13]; case 12: if (e_20) throw e_20.error; return [7 /*endfinally*/]; case 13: return [7 /*endfinally*/]; case 14: return [2 /*return*/]; } }); }); } /** * Return overlapped chunks of elements from given collection. * * Chunk size must be at least 1. * * Overlap size must be less than chunk size. * * @param data * @param chunkSize * @param overlapSize * @param includeIncompleteTail */ function chunkwiseOverlap(data, chunkSize, overlapSize, includeIncompleteTail) { var chunk, isLastIterationYielded, _a, _b, datum, e_21_1; var e_21, _c; if (includeIncompleteTail === void 0) { includeIncompleteTail = true; } return __generator(this, function (_d) { switch (_d.label) { case 0: if (chunkSize < 1) { throw new exceptions_1.InvalidArgumentError("Chunk size must be \u2265 1. Got ".concat(chunkSize)); } if (overlapSize >= chunkSize) { throw new exceptions_1.InvalidArgumentError("Overlap size must be less than chunk size"); } chunk = []; isLastIterationYielded = false; _d.label = 1; case 1: _d.trys.push([1, 6, 7, 8]); _a = __values((0, transform_1.toIterable)(data)), _b = _a.next(); _d.label = 2; case 2: if (!!_b.done) return [3 /*break*/, 5]; datum = _b.value; isLastIterationYielded = false; chunk.push(datum); if (!(chunk.length === chunkSize)) return [3 /*break*/, 4]; return [4 /*yield*/, chunk]; case 3: _d.sent(); chunk = chunk.slice(chunkSize - overlapSize); isLastIterationYielded = true; _d.label = 4; case 4: _b = _a.next(); return [3 /*break*/, 2]; case 5: return [3 /*break*/, 8]; case 6: e_21_1 = _d.sent(); e_21 = { error: e_21_1 }; return [3 /*break*/, 8]; case 7: try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_21) throw e_21.error; } return [7 /*endfinally*/]; case 8: if (!(!isLastIterationYielded && chunk.length > 0 && includeIncompleteTail)) return [3 /*break*/, 10]; return [4 /*yield*/, chunk]; case 9: _d.sent(); _d.label = 10; case 10: return [2 /*return*/]; } }); } /** * Return overlapped chunks of elements from given async collection. * * Chunk size must be at least 1. * * Overlap size must be less than chunk size. * * @param data * @param chunkSize * @param overlapSize * @param includeIncompleteTail */ function chunkwiseOverlapAsync(data_1, chunkSize_1, overlapSize_1) { return __asyncGenerator(this, arguments, function chunkwiseOverlapAsync_1(data, chunkSize, overlapSize, includeIncompleteTail) { var chunk, isLastIterationYielded, _a, _b, _c, datum, e_22_1; var _d, e_22, _e, _f; if (includeIncompleteTail === void 0) { includeIncompleteTail = true; } return __generator(this, function (_g) { switch (_g.label) { case 0: if (chunkSize < 1) { throw new exceptions_1.InvalidArgumentError("Chunk size must be \u2265 1. Got ".concat(chunkSize)); } if (overlapSize >= chunkSize) { throw new exceptions_1.InvalidArgumentError("Overlap size must be less than chunk size"); } chunk = []; isLastIterationYielded = false; _g.label = 1; case 1: _g.trys.push([1, 8, 9, 14]); _a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data)); _g.label = 2; case 2: return [4 /*yield*/, __await(_b.next())]; case 3: if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 7]; _f = _c.value; _a = false; datum = _f; isLastIterationYielded = false; chunk.push(datum); if (!(chunk.length === chunkSize)) return [3 /*break*/, 6]; return [4 /*yield*/, __await(chunk)]; case 4: return [4 /*yield*/, _g.sent()]; case 5: _g.sent(); chunk = chunk.slice(chunkSize - overlapSize); isLastIterationYielded = true; _g.label = 6; case 6: _a = true; return [3 /*break*/, 2]; case 7: return [3 /*break*/, 14]; case 8: e_22_1 = _g.sent(); e_22 = { error: e_22_1 }; return [3 /*break*/, 14]; case 9: _g.trys.push([9, , 12, 13]); if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 11]; return [4 /*yield*/, __await(_e.call(_b))]; case 10: _g.sent(); _g.label = 11; case 11: return [3 /*break*/, 13]; case 12: if (e_22) throw e_22.error; return [7 /*endfinally*/]; case 13: return [7 /*endfinally*/]; case 14: if (!(!isLastIterationYielded && chunk.length > 0 && includeIncompleteTail)) return [3 /*break*/, 17]; return [4 /*yield*/, __await(chunk)]; case 15: return [4 /*yield*/, _g.sent()]; case 16: _g.sent(); _g.label = 17; case 1