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,232 lines • 45.2 kB
JavaScript
"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());
});
};
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 __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;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncStream = void 0;
var transform_1 = require("./transform");
var single_1 = require("./single");
var multi_1 = require("./multi");
var math_1 = require("./math");
var set_1 = require("./set");
var combinatorics_1 = require("./combinatorics");
var reduce_1 = require("./reduce");
var summary_1 = require("./summary");
var index_1 = require("./index");
/**
* Provides fluent interface for working with async iterables.
*/
var AsyncStream = /** @class */ (function () {
/**
* Stream constructor.
*
* @param iterable
*/
function AsyncStream(iterable) {
this.data = iterable;
}
/**
* Creates iterable instance with fluent interface.
*
* @param data
*/
AsyncStream.of = function (data) {
return new AsyncStream((0, transform_1.toAsyncIterable)(data));
};
/**
* Creates iterable instance with fluent interface from empty iterable source.
*/
AsyncStream.ofEmpty = function () {
return new AsyncStream((0, transform_1.toAsyncIterable)([]));
};
/**
* Creates iterable instance with fluent interface from infinite count iterable.
*
* @param start (optional, default 1)
* @param step (optional, default 1)
*/
AsyncStream.ofCount = function (start, step) {
if (start === void 0) { start = 1; }
if (step === void 0) { step = 1; }
return new AsyncStream((0, transform_1.toAsyncIterable)(index_1.infinite.count(start, step)));
};
/**
* Creates iterable instance with fluent interface from infinite collection items repeating.
*
* @param iterable
*/
AsyncStream.ofCycle = function (iterable) {
return new AsyncStream(index_1.infinite.cycleAsync(iterable));
};
/**
* Creates iterable instance with fluent interface from infinite item repeating.
*
* @param item
*/
AsyncStream.ofRepeat = function (item) {
return new AsyncStream((0, transform_1.toAsyncIterable)(index_1.infinite.repeat(item)));
};
/**
* Iterate stream collection with another iterable collections simultaneously.
*
* Make an iterator that aggregates items from multiple iterators.
* Similar to Python's zip function.
*
* For uneven lengths, iterations stops when the shortest iterable is exhausted.
*
* @param iterables
*
* @see multi.zipAsync
*/
AsyncStream.prototype.zipWith = function () {
var iterables = [];
for (var _i = 0; _i < arguments.length; _i++) {
iterables[_i] = arguments[_i];
}
this.data = multi_1.zipAsync.apply(void 0, __spreadArray([this.data], __read(iterables), false));
return this;
};
/**
* Iterate stream collection with another iterable collections simultaneously.
*
* Make an iterator that aggregates items from multiple iterators.
* Similar to Python's zip_longest function.
*
* Iteration continues until the longest iterable is exhausted.
* For uneven lengths, the exhausted iterables will produce `filler` value for the remaining iterations.
*
* @param filler
* @param iterables
*
* @see multi.zipLongestAsync
*/
AsyncStream.prototype.zipFilledWith = function (filler) {
var iterables = [];
for (var _i = 1; _i < arguments.length; _i++) {
iterables[_i - 1] = arguments[_i];
}
this.data = multi_1.zipFilledAsync.apply(void 0, __spreadArray([filler, this.data], __read(iterables), false));
return this;
};
/**
* Iterate stream collection with another iterable collections simultaneously.
*
* Make an iterator that aggregates items from multiple iterators.
* Similar to Python's zip_longest function.
*
* Iteration continues until the longest iterable is exhausted.
* For uneven lengths, the exhausted iterables will produce `undefined` for the remaining iterations.
*
* @param iterables
*
* @see multi.zipLongestAsync
*/
AsyncStream.prototype.zipLongestWith = function () {
var iterables = [];
for (var _i = 0; _i < arguments.length; _i++) {
iterables[_i] = arguments[_i];
}
this.data = multi_1.zipLongestAsync.apply(void 0, __spreadArray([this.data], __read(iterables), false));
return this;
};
/**
* Iterate stream collection with another iterable collections of equal lengths simultaneously.
*
* Works like multi.zip() method but throws LengthException if lengths not equal,
* i.e., at least one iterator ends before the others.
*
* @param iterables
*
* @see multi.zipEqualAsync
*/
AsyncStream.prototype.zipEqualWith = function () {
var iterables = [];
for (var _i = 0; _i < arguments.length; _i++) {
iterables[_i] = arguments[_i];
}
this.data = multi_1.zipEqualAsync.apply(void 0, __spreadArray([this.data], __read(iterables), false));
return this;
};
/**
* Chain stream collection withs given iterables together into a single iteration.
*
* Makes a single continuous sequence out of multiple sequences.
*
* @param iterables
*
* @see multi.chainAsync
*/
AsyncStream.prototype.chainWith = function () {
var iterables = [];
for (var _i = 0; _i < arguments.length; _i++) {
iterables[_i] = arguments[_i];
}
this.data = multi_1.chainAsync.apply(void 0, __spreadArray([this.data], __read(iterables), false));
return this;
};
/**
* Return overlapped chunks of elements from iterable source.
*
* Chunk size must be at least 1.
*
* Overlap size must be less than chunk size.
*
* @param chunkSize
* @param overlapSize
* @param includeIncompleteTail
*
* @see single.chunkwiseOverlapAsync
*/
AsyncStream.prototype.chunkwiseOverlap = function (chunkSize, overlapSize, includeIncompleteTail) {
if (includeIncompleteTail === void 0) { includeIncompleteTail = true; }
this.data = (0, single_1.chunkwiseOverlapAsync)(this.data, chunkSize, overlapSize, includeIncompleteTail);
return this;
};
/**
* Return chunks of elements from iterable source.
*
* Chunk size must be at least 1.
*
* @param chunkSize
*
* @see single.chunkwiseAsync
*/
AsyncStream.prototype.chunkwise = function (chunkSize) {
this.data = (0, single_1.chunkwiseAsync)(this.data, chunkSize);
return this;
};
/**
* Compress an iterable source by filtering out data that is not selected.
*
* Selectors indicate which data. True value selects item. False value filters out data.
*
* @param selectors
*
* @see single.compressAsync
*/
AsyncStream.prototype.compress = function (selectors) {
this.data = (0, single_1.compressAsync)(this.data, selectors);
return this;
};
/**
* Drop elements from the iterable source while the predicate function is true.
*
* Once the predicate function returns false once, all remaining elements are returned.
*
* @param predicate
*
* @see single.dropWhileAsync
*/
AsyncStream.prototype.dropWhile = function (predicate) {
this.data = (0, single_1.dropWhileAsync)(this.data, predicate);
return this;
};
/**
* Filter out elements from the iterable source only returning elements where there predicate function is true.
*
* @param predicate
*
* @see single.filterAsync
*/
AsyncStream.prototype.filter = function (predicate) {
this.data = (0, single_1.filterAsync)(this.data, predicate);
return this;
};
/**
* Enumerates items of given collection.
*
* @see single.enumerateAsync
*/
AsyncStream.prototype.enumerate = function () {
this.data = (0, single_1.enumerateAsync)(this.data);
return this;
};
/**
* Iterates keys from the collection of key-value pairs.
*
* @see single.keysAsync
*/
AsyncStream.prototype.keys = function () {
this.data = (0, single_1.keysAsync)(this.data);
return this;
};
/**
* Limit iteration to a max size limit.
*
* @param count
*
* @see single.limitAsync
*/
AsyncStream.prototype.limit = function (count) {
this.data = (0, single_1.limitAsync)(this.data, count);
return this;
};
/**
* Map a function onto every element of the stream
*
* @param mapper
*
* @see single.mapAsync
*/
AsyncStream.prototype.map = function (mapper) {
this.data = (0, single_1.mapAsync)(this.data, mapper);
return this;
};
/**
* Returns a new collection formed by applying a given callback function
* to each element of the stream, and then flattening the result by one level.
*
* @param mapper
*
* @see single.flatMapAsync
*/
AsyncStream.prototype.flatMap = function (mapper) {
this.data = (0, single_1.flatMapAsync)(this.data, mapper);
return this;
};
/**
* Flatten a stream.
*
* @param dimensions
*
* @see single.flattenAsync
*/
AsyncStream.prototype.flatten = function (dimensions) {
if (dimensions === void 0) { dimensions = Infinity; }
this.data = (0, single_1.flattenAsync)(this.data, dimensions);
return this;
};
/**
* Group stream data by a common data element.
*
* Iterate pairs of group name and collection of grouped items.
*
* Collection of grouped items may be an array or an object (depends on presence of `itemKeyFunction` param).
*
* The `groupKeyFunction` determines the key (or multiple keys) to group elements by.
*
* The `itemKeyFunction` (optional) determines the key of element in group.
*
* @param groupKeyFunction
* @param itemKeyFunction
*
* @see single.groupByAsync
*/
AsyncStream.prototype.groupBy = function (groupKeyFunction, itemKeyFunction) {
this.data = (0, single_1.groupByAsync)(this.data, groupKeyFunction, itemKeyFunction);
return this;
};
/**
* Return pairs of elements from iterable source.
*
* Produces empty generator if given collection contains less than 2 elements.
*
* @see single.pairwiseAsync
*/
AsyncStream.prototype.pairwise = function () {
this.data = (0, single_1.pairwiseAsync)(this.data);
return this;
};
/**
* Accumulate the running average (mean) over the stream.
*
* @param initialValue (Optional) If provided, the running average leads off with the initial value.
*
* @see math.runningAverageAsync
*/
AsyncStream.prototype.runningAverage = function (initialValue) {
this.data = (0, math_1.runningAverageAsync)(this.data, initialValue);
return this;
};
/**
* Accumulate the running difference over the stream.
*
* @param initialValue (Optional) If provided, the running difference leads off with the initial value.
*
* @see math.runningDifferenceAsync
*/
AsyncStream.prototype.runningDifference = function (initialValue) {
this.data = (0, math_1.runningDifferenceAsync)(this.data, initialValue);
return this;
};
/**
* Accumulate the running max over the stream.
*
* @param initialValue (Optional) If provided, the running max leads off with the initial value.
*
* @see math.runningMaxAsync
*/
AsyncStream.prototype.runningMax = function (initialValue) {
this.data = (0, math_1.runningMaxAsync)(this.data, initialValue);
return this;
};
/**
* Accumulate the running min over the stream.
*
* @param initialValue (Optional) If provided, the running min leads off with the initial value.
*
* @see math.runningMinAsync
*/
AsyncStream.prototype.runningMin = function (initialValue) {
this.data = (0, math_1.runningMinAsync)(this.data, initialValue);
return this;
};
/**
* Accumulate the running product over the stream.
*
* @param initialValue (Optional) If provided, the running product leads off with the initial value.
*
* @see math.runningProductAsync
*/
AsyncStream.prototype.runningProduct = function (initialValue) {
this.data = (0, math_1.runningProductAsync)(this.data, initialValue);
return this;
};
/**
* Accumulate the running total over the stream.
*
* @param initialValue (Optional) If provided, the running total leads off with the initial value.
*
* @see math.runningTotalAsync
*/
AsyncStream.prototype.runningTotal = function (initialValue) {
this.data = (0, math_1.runningTotalAsync)(this.data, initialValue);
return this;
};
/**
* Skip n elements in the stream after optional offset.
*
* @param count
* @param offset
*
* @see single.skipAsync
*/
AsyncStream.prototype.skip = function (count, offset) {
if (offset === void 0) { offset = 0; }
this.data = (0, single_1.skipAsync)(this.data, count, offset);
return this;
};
/**
* Extract a slice of the stream.
*
* @param start
* @param count
* @param step
*
* @see single.sliceAsync
*/
AsyncStream.prototype.slice = function (start, count, step) {
if (start === void 0) { start = 0; }
if (step === void 0) { step = 1; }
this.data = (0, single_1.sliceAsync)(this.data, start, count, step);
return this;
};
/**
* Return elements from the iterable source as long as the predicate is true.
*
* If no predicate is provided, the boolean value of the data is used.
*
* @param predicate
*
* @see single.takeWhileAsync
*/
AsyncStream.prototype.takeWhile = function (predicate) {
this.data = (0, single_1.takeWhileAsync)(this.data, predicate);
return this;
};
/**
* Iterates values from the collection of key-value pairs.
*
* @see single.valuesAsync
*/
AsyncStream.prototype.values = function () {
this.data = (0, single_1.valuesAsync)(this.data);
return this;
};
/**
* Sorts the stream.
*
* If comparator is `undefined`, then elements of the iterable source must be comparable.
*
* @see single.sort
*/
AsyncStream.prototype.sort = function (comparator) {
this.data = (0, single_1.sortAsync)(this.data, comparator);
return this;
};
/**
* Filter out elements from the iterable source only returning unique elements.
*
* @param compareBy
*
* @see set.distinctAsync
*/
AsyncStream.prototype.distinct = function (compareBy) {
this.data = (0, set_1.distinctAsync)(this.data, compareBy);
return this;
};
/**
* Iterates the intersection of iterable source and given iterables.
*
* Always treats different instances of objects and arrays as unequal.
*
* @param iterables
*
* @see set.intersectionAsync
*/
AsyncStream.prototype.intersectionWith = function () {
var iterables = [];
for (var _i = 0; _i < arguments.length; _i++) {
iterables[_i] = arguments[_i];
}
this.data = set_1.intersectionAsync.apply(void 0, __spreadArray([this.data], __read(iterables), false));
return this;
};
/**
* Iterates partial intersection of iterable source and given iterables.
*
* Always treats different instances of objects and arrays as unequal.
*
* @param minIntersectionCount
* @param iterables
*
* @see set.partialIntersectionAsync
*/
AsyncStream.prototype.partialIntersectionWith = function (minIntersectionCount) {
var iterables = [];
for (var _i = 1; _i < arguments.length; _i++) {
iterables[_i - 1] = arguments[_i];
}
this.data = set_1.partialIntersectionAsync.apply(void 0, __spreadArray([minIntersectionCount,
this.data], __read(iterables), false));
return this;
};
/**
* Iterates the symmetric difference of iterable source and given iterables.
*
* Always treats different instances of objects and arrays as unequal.
*
* @param iterables
*
* @see set.symmetricDifferenceAsync
*/
AsyncStream.prototype.symmetricDifferenceWith = function () {
var iterables = [];
for (var _i = 0; _i < arguments.length; _i++) {
iterables[_i] = arguments[_i];
}
this.data = set_1.symmetricDifferenceAsync.apply(void 0, __spreadArray([this.data], __read(iterables), false));
return this;
};
/**
* Iterates union of iterable source and given iterables.
*
* Always treats different instances of objects and arrays as unequal.
*
* @param iterables
*
* @see set.unionAsync
*/
AsyncStream.prototype.unionWith = function () {
var iterables = [];
for (var _i = 0; _i < arguments.length; _i++) {
iterables[_i] = arguments[_i];
}
this.data = set_1.unionAsync.apply(void 0, __spreadArray([this.data], __read(iterables), false));
return this;
};
/**
* Iterates cartesian product of iterable source and given iterables.
*
* @param iterables
*
* @see combinatorics.cartesianProductAsync
*/
AsyncStream.prototype.cartesianProductWith = function () {
var iterables = [];
for (var _i = 0; _i < arguments.length; _i++) {
iterables[_i] = arguments[_i];
}
this.data = combinatorics_1.cartesianProductAsync.apply(void 0, __spreadArray([this.data], __read(iterables), false));
return this;
};
/**
* Iterates all permutations of iterable source.
*
* @param length
*
* @see combinatorics.permutations
*/
AsyncStream.prototype.permutations = function (length) {
this.data = (0, combinatorics_1.permutationsAsync)(this.data, length);
return this;
};
/**
* Iterates all combinations of iterable source.
*
* @param length
*
* @see combinatorics.combinations
*/
AsyncStream.prototype.combinations = function (length) {
this.data = (0, combinatorics_1.combinationsAsync)(this.data, length);
return this;
};
/**
* Peek at each element between other Stream operations to do some action without modifying the stream.
*
* Useful for debugging purposes.
*
* @param callback
*/
AsyncStream.prototype.peek = function (callback) {
var _this = this;
var _a = __read((0, transform_1.teeAsync)(this.data, 2), 2), data = _a[0], peekable = _a[1];
this.data = data;
(function () { return __awaiter(_this, void 0, void 0, function () {
var _a, peekable_1, peekable_1_1, element, e_1_1;
var _b, e_1, _c, _d;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
_e.trys.push([0, 5, 6, 11]);
_a = true, peekable_1 = __asyncValues(peekable);
_e.label = 1;
case 1: return [4 /*yield*/, peekable_1.next()];
case 2:
if (!(peekable_1_1 = _e.sent(), _b = peekable_1_1.done, !_b)) return [3 /*break*/, 4];
_d = peekable_1_1.value;
_a = false;
element = _d;
callback(element);
_e.label = 3;
case 3:
_a = true;
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 11];
case 5:
e_1_1 = _e.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 11];
case 6:
_e.trys.push([6, , 9, 10]);
if (!(!_a && !_b && (_c = peekable_1.return))) return [3 /*break*/, 8];
return [4 /*yield*/, _c.call(peekable_1)];
case 7:
_e.sent();
_e.label = 8;
case 8: return [3 /*break*/, 10];
case 9:
if (e_1) throw e_1.error;
return [7 /*endfinally*/];
case 10: return [7 /*endfinally*/];
case 11: return [2 /*return*/];
}
});
}); })();
return this;
};
/**
* Peek at the entire stream between other Stream operations to do some action without modifying the stream.
*
* Useful for debugging purposes.
*
* @param callback
*/
AsyncStream.prototype.peekStream = function (callback) {
var _a = __read((0, transform_1.teeAsync)(this.data, 2), 2), data = _a[0], peekable = _a[1];
this.data = data;
callback(AsyncStream.of(peekable));
return this;
};
/**
* Reduces iterable source like `array.reduce()` function.
*
* @param reducer
* @param initialValue
*
* @see reduce.toValueAsync
*/
AsyncStream.prototype.toValue = function (reducer, initialValue) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, reduce_1.toValueAsync)(this, reducer, initialValue)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Reduces iterable source to the mean average of its items.
*
* Returns `undefined` if iterable source is empty.
*
* @see reduce.toAverageAsync
*/
AsyncStream.prototype.toAverage = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, reduce_1.toAverageAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Reduces iterable source to its length.
*
* @see reduce.toCountAsync
*/
AsyncStream.prototype.toCount = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, reduce_1.toCountAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Reduces iterable source to its max value.
*
* Callable param `compareBy` must return comparable value.
*
* If `compareBy` is not proposed then items of iterable source must be comparable.
*
* Returns `undefined` if iterable source is empty.
*
* @param compareBy
*
* @see reduce.toMaxAsync
*/
AsyncStream.prototype.toMax = function (compareBy) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, reduce_1.toMaxAsync)(this, compareBy)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Reduces iterable source to its min value.
*
* Callable param `compareBy` must return comparable value.
*
* If `compareBy` is not proposed then items of iterable source must be comparable.
*
* Returns `undefined` if iterable source is empty.
*
* @param compareBy
*
* @see reduce.toMinAsync
*/
AsyncStream.prototype.toMin = function (compareBy) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, reduce_1.toMinAsync)(this, compareBy)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Reduces given collection to array of its upper and lower bounds.
*
* Callable param `compareBy` must return comparable value.
*
* If `compareBy` is not proposed then items of given collection must be comparable.
*
* Returns `[undefined, undefined]` if given collection is empty.
*
* @param compareBy
*
* @see reduce.toMinMaxAsync
*/
AsyncStream.prototype.toMinMax = function (compareBy) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, reduce_1.toMinMaxAsync)(this, compareBy)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Returns the first element of stream.
*
* @throws LengthError if stream is empty.
*
* @see reduce.toFirstAsync
*/
AsyncStream.prototype.toFirst = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, reduce_1.toFirstAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Returns the first and last elements of stream.
*
* @throws LengthError if stream is empty.
*
* @see reduce.toFirstAndLastAsync
*/
AsyncStream.prototype.toFirstAndLast = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, reduce_1.toFirstAndLastAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Returns the first element of stream.
*
* @throws LengthError if stream is empty.
*
* @see reduce.toLastAsync
*/
AsyncStream.prototype.toLast = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, reduce_1.toLastAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Reduces iterable source to the sum of its items.
*
* @see reduce.toSumAsync
*/
AsyncStream.prototype.toSum = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, reduce_1.toSumAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Reduces iterable source to the product of its items.
*
* Returns `undefined` if iterable source is empty.
*
* @see reduce.toProductAsync
*/
AsyncStream.prototype.toProduct = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, reduce_1.toProductAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Reduces given collection to its range.
*
* Returns 0 if given collection is empty.
*
* @see reduce.toRangeAsync
*/
AsyncStream.prototype.toRange = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, reduce_1.toRangeAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Returns true if all elements of stream match the predicate function.
*
* For empty stream returns true.
*
* @param predicate
*
* @see summary.allMatchAsync
*/
AsyncStream.prototype.allMatch = function (predicate) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, summary_1.allMatchAsync)(this, predicate)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Returns true if all elements of stream are unique.
*
* For empty stream returns true.
*
* Considers different instances of data containers to be different, even if they have the same content.
*
* @see summary.allUniqueAsync
*/
AsyncStream.prototype.allUnique = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, summary_1.allUniqueAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Returns true if any element of stream matches the predicate function.
*
* For empty stream returns false.
*
* @param predicate
*
* @see summary.anyMatchAsync
*/
AsyncStream.prototype.anyMatch = function (predicate) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, summary_1.anyMatchAsync)(this, predicate)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Returns true if exactly n items in the async iterable are true where the predicate function is true.
*
* Default predicate if not provided is the boolean value of each data item.
*
* @param n
* @param predicate
*
* @see summary.exactlyNAsync
*/
AsyncStream.prototype.exactlyN = function (n, predicate) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, (0, summary_1.exactlyNAsync)(this, n, predicate)];
});
});
};
/**
* Returns true if stream is sorted in ascending order; otherwise false.
*
* Items of stream source must be comparable.
*
* Also returns true if stream is empty or has only one element.
*
* @see summary.isSortedAsync
*/
AsyncStream.prototype.isSorted = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, summary_1.isSortedAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Returns true if stream is sorted in descending order; otherwise false.
*
* Items of stream source must be comparable.
*
* Also returns true if stream is empty or has only one element.
*
* @see summary.isReversedAsync
*/
AsyncStream.prototype.isReversed = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, summary_1.isReversedAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Returns true if no element of stream matches the predicate function.
*
* For empty stream returns true.
*
* @param predicate
*
* @see summary.noneMatchAsync
*/
AsyncStream.prototype.noneMatch = function (predicate) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, summary_1.noneMatchAsync)(this, predicate)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Returns true if stream collection and all given collections are the same.
*
* For empty collections list returns true.
*
* @param collections
*
* @see summary.sameAsync
*/
AsyncStream.prototype.sameWith = function () {
var collections = [];
for (var _i = 0; _i < arguments.length; _i++) {
collections[_i] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, summary_1.sameAsync.apply(void 0, __spreadArray([this.data], __read(collections), false))];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Returns true if stream collection and all given collections have the same lengths.
*
* For empty collections list returns true.
*
* @param collections
*
* @see summary.sameCountAsync
*/
AsyncStream.prototype.sameCountWith = function () {
var collections = [];
for (var _i = 0; _i < arguments.length; _i++) {
collections[_i] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, summary_1.sameCountAsync.apply(void 0, __spreadArray([this.data], __read(collections), false))];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Return several independent async streams from current stream.
*
* Once a tee() has been created, the original iterable should not be used anywhere else;
* otherwise, the iterable could get advanced without the tee objects being informed.
*
* This tool may require significant auxiliary storage (depending on how much temporary data needs to be stored).
* In general, if one iterator uses most or all of the data before another iterator starts,
* it is faster to use toArray() instead of tee().
*
* @param count
*
* @see transform.teeAsync
*/
AsyncStream.prototype.tee = function (count) {
return (0, transform_1.teeAsync)(this.data, count).map(function (iterable) { return new AsyncStream(iterable); });
};
/**
* Converts stream to Array.
*
* @see transform.toArrayAsync
*/
AsyncStream.prototype.toArray = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, transform_1.toArrayAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Converts stream to Map.
*
* Stream collection must contain only key-value pairs as elements.
*
* @see transform.toMapAsync
*/
AsyncStream.prototype.toMap = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, transform_1.toMapAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Converts stream to Set.
*
* @see transform.toSetAsync
*/
AsyncStream.prototype.toSet = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, transform_1.toSetAsync)(this)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Aggregated iterator.
*/
AsyncStream.prototype[Symbol.asyncIterator] = function () {
return __asyncGenerator(this, arguments, function _a() {
var _b, _c, _d, datum, e_2_1;
var _e, e_2, _f, _g;
return __generator(this, function (_h) {
switch (_h.label) {
case 0:
_h.trys.push([0, 7, 8, 13]);
_b = true, _c = __asyncValues(this.data);
_h.label = 1;
case 1: return [4 /*yield*/, __await(_c.next())];
case 2:
if (!(_d = _h.sent(), _e = _d.done, !_e)) return [3 /*break*/, 6];
_g = _d.value;
_b = false;
datum = _g;
return [4 /*yield*/, __await(datum)];
case 3: return [4 /*yield*/, _h.sent()];
case 4:
_h.sent();
_h.label = 5;
case 5:
_b = true;
return [3 /*break*/, 1];
case 6: return [3 /*break*/, 13];
case 7:
e_2_1 = _h.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 13];
case 8:
_h.trys.push([8, , 11, 12]);
if (!(!_b && !_e && (_f = _c.return))) return [3 /*break*/, 10];
return [4 /*yield*/, __await(_f.call(_c))];
case 9:
_h.sent();
_h.label = 10;
case 10: return [3 /*break*/, 12];
case 11:
if (e_2) throw e_2.error;
return [7 /*endfinally*/];
case 12: return [7 /*endfinally*/];
case 13: return [2 /*return*/];
}
});
});
};
return AsyncStream;
}());
exports.AsyncStream = AsyncStream;
//# sourceMappingURL=async-stream.js.map