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,005 lines • 36 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 __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;
};
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.allMatch = allMatch;
exports.allMatchAsync = allMatchAsync;
exports.allUnique = allUnique;
exports.allUniqueAsync = allUniqueAsync;
exports.anyMatch = anyMatch;
exports.anyMatchAsync = anyMatchAsync;
exports.exactlyN = exactlyN;
exports.exactlyNAsync = exactlyNAsync;
exports.isEmpty = isEmpty;
exports.isEmptyAsync = isEmptyAsync;
exports.isIterable = isIterable;
exports.isAsyncIterable = isAsyncIterable;
exports.isIterator = isIterator;
exports.isReversed = isReversed;
exports.isReversedAsync = isReversedAsync;
exports.isSorted = isSorted;
exports.isSortedAsync = isSortedAsync;
exports.isString = isString;
exports.noneMatch = noneMatch;
exports.noneMatchAsync = noneMatchAsync;
exports.same = same;
exports.sameAsync = sameAsync;
exports.sameCount = sameCount;
exports.sameCountAsync = sameCountAsync;
var multi_1 = require("./multi");
var single_1 = require("./single");
var reduce_1 = require("./reduce");
var transform_1 = require("./transform");
/**
* Returns true if all elements match the predicate function.
*
* Empty collections return true.
*
* @param data
* @param predicate
*/
function allMatch(data, predicate) {
var e_1, _a;
try {
for (var _b = __values((0, transform_1.toIterable)(data)), _c = _b.next(); !_c.done; _c = _b.next()) {
var datum = _c.value;
if (!predicate(datum)) {
return false;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return true;
}
/**
* Returns true if all elements of async collection match the predicate function.
*
* Empty collections return true.
*
* @param data
* @param predicate
*/
function allMatchAsync(data, predicate) {
return __awaiter(this, void 0, void 0, function () {
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, 6, 7, 12]);
_a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data));
_g.label = 1;
case 1: return [4 /*yield*/, _b.next()];
case 2:
if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 5];
_f = _c.value;
_a = false;
datum = _f;
return [4 /*yield*/, predicate(datum)];
case 3:
if (!(_g.sent())) {
return [2 /*return*/, false];
}
_g.label = 4;
case 4:
_a = true;
return [3 /*break*/, 1];
case 5: return [3 /*break*/, 12];
case 6:
e_2_1 = _g.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 12];
case 7:
_g.trys.push([7, , 10, 11]);
if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 9];
return [4 /*yield*/, _e.call(_b)];
case 8:
_g.sent();
_g.label = 9;
case 9: return [3 /*break*/, 11];
case 10:
if (e_2) throw e_2.error;
return [7 /*endfinally*/];
case 11: return [7 /*endfinally*/];
case 12: return [2 /*return*/, true];
}
});
});
}
/**
* Return true if all elements in given collection are unique.
*
* Empty collections return true.
*
* Considers different instances of data containers to be different, even if they have the same content.
*
* @param data
*/
function allUnique(data) {
var e_3, _a;
var usages = new Set();
try {
for (var _b = __values((0, transform_1.toIterable)(data)), _c = _b.next(); !_c.done; _c = _b.next()) {
var datum = _c.value;
if (usages.has(datum)) {
return false;
}
usages.add(datum);
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_3) throw e_3.error; }
}
return true;
}
/**
* Return true if all elements in given async collection are unique.
*
* Empty collections return true.
*
* Considers different instances of data containers to be different, even if they have the same content.
*
* @param data
*/
function allUniqueAsync(data) {
return __awaiter(this, void 0, void 0, function () {
var usages, _a, _b, _c, datum, e_4_1;
var _d, e_4, _e, _f;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
usages = new Set();
_g.label = 1;
case 1:
_g.trys.push([1, 6, 7, 12]);
_a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data));
_g.label = 2;
case 2: return [4 /*yield*/, _b.next()];
case 3:
if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 5];
_f = _c.value;
_a = false;
datum = _f;
if (usages.has(datum)) {
return [2 /*return*/, false];
}
usages.add(datum);
_g.label = 4;
case 4:
_a = true;
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 12];
case 6:
e_4_1 = _g.sent();
e_4 = { error: e_4_1 };
return [3 /*break*/, 12];
case 7:
_g.trys.push([7, , 10, 11]);
if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 9];
return [4 /*yield*/, _e.call(_b)];
case 8:
_g.sent();
_g.label = 9;
case 9: return [3 /*break*/, 11];
case 10:
if (e_4) throw e_4.error;
return [7 /*endfinally*/];
case 11: return [7 /*endfinally*/];
case 12: return [2 /*return*/, true];
}
});
});
}
/**
* Returns true if any element matches the predicate function.
*
* Empty collections return false.
*
* @param data
* @param predicate
*/
function anyMatch(data, predicate) {
var e_5, _a;
try {
for (var _b = __values((0, transform_1.toIterable)(data)), _c = _b.next(); !_c.done; _c = _b.next()) {
var datum = _c.value;
if (predicate(datum)) {
return true;
}
}
}
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_5) throw e_5.error; }
}
return false;
}
/**
* Returns true if any element of async collection matches the predicate function.
*
* Empty collections return false.
*
* @param data
* @param predicate
*/
function anyMatchAsync(data, predicate) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b, _c, datum, e_6_1;
var _d, e_6, _e, _f;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
_g.trys.push([0, 6, 7, 12]);
_a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data));
_g.label = 1;
case 1: return [4 /*yield*/, _b.next()];
case 2:
if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 5];
_f = _c.value;
_a = false;
datum = _f;
return [4 /*yield*/, predicate(datum)];
case 3:
if (_g.sent()) {
return [2 /*return*/, true];
}
_g.label = 4;
case 4:
_a = true;
return [3 /*break*/, 1];
case 5: return [3 /*break*/, 12];
case 6:
e_6_1 = _g.sent();
e_6 = { error: e_6_1 };
return [3 /*break*/, 12];
case 7:
_g.trys.push([7, , 10, 11]);
if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 9];
return [4 /*yield*/, _e.call(_b)];
case 8:
_g.sent();
_g.label = 9;
case 9: return [3 /*break*/, 11];
case 10:
if (e_6) throw e_6.error;
return [7 /*endfinally*/];
case 11: return [7 /*endfinally*/];
case 12: return [2 /*return*/, false];
}
});
});
}
/**
* Returns true if exactly n items in the iterable are true where the predicate function is true.
*
* Default predicate if not provided is the boolean value of each data item.
*
* @param data
* @param n
* @param predicate
*/
function exactlyN(data, n, predicate) {
var e_7, _a;
if (n < 0) {
return false;
}
if (predicate === undefined) {
predicate = function (datum) { return Boolean(datum); };
}
var count = 0;
try {
for (var _b = __values((0, transform_1.toIterable)(data)), _c = _b.next(); !_c.done; _c = _b.next()) {
var datum = _c.value;
if (predicate(datum)) {
count++;
if (count > n) {
return false;
}
}
}
}
catch (e_7_1) { e_7 = { error: e_7_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_7) throw e_7.error; }
}
return count === n;
}
/**
* 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 data
* @param n
* @param predicate
*/
function exactlyNAsync(data, n, predicate) {
return __awaiter(this, void 0, void 0, function () {
var count, _a, _b, _c, datum, e_8_1;
var _d, e_8, _e, _f;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
if (n < 0) {
return [2 /*return*/, false];
}
if (predicate === undefined) {
predicate = function (datum) { return Boolean(datum); };
}
count = 0;
_g.label = 1;
case 1:
_g.trys.push([1, 7, 8, 13]);
_a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data));
_g.label = 2;
case 2: return [4 /*yield*/, _b.next()];
case 3:
if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 6];
_f = _c.value;
_a = false;
datum = _f;
return [4 /*yield*/, predicate(datum)];
case 4:
if (_g.sent()) {
count++;
if (count > n) {
return [2 /*return*/, false];
}
}
_g.label = 5;
case 5:
_a = true;
return [3 /*break*/, 2];
case 6: return [3 /*break*/, 13];
case 7:
e_8_1 = _g.sent();
e_8 = { error: e_8_1 };
return [3 /*break*/, 13];
case 8:
_g.trys.push([8, , 11, 12]);
if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 10];
return [4 /*yield*/, _e.call(_b)];
case 9:
_g.sent();
_g.label = 10;
case 10: return [3 /*break*/, 12];
case 11:
if (e_8) throw e_8.error;
return [7 /*endfinally*/];
case 12: return [7 /*endfinally*/];
case 13: return [2 /*return*/, count === n];
}
});
});
}
/**
* Returns true if given collection is empty.
*
* @param data
*/
function isEmpty(data) {
var e_9, _a;
try {
/* eslint-disable @typescript-eslint/no-unused-vars */
for (var _b = __values((0, transform_1.toIterable)(data)), _c = _b.next(); !_c.done; _c = _b.next()) {
var _ = _c.value;
return false;
}
}
catch (e_9_1) { e_9 = { error: e_9_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_9) throw e_9.error; }
}
return true;
}
/**
* Returns true if given async collection is empty.
*
* @param data
*/
function isEmptyAsync(data) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b, _c, _, e_10_1;
var _d, e_10, _e, _f;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
_g.trys.push([0, 5, 6, 11]);
_a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data));
_g.label = 1;
case 1: return [4 /*yield*/, _b.next()];
case 2:
if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 4];
_f = _c.value;
_a = false;
_ = _f;
return [2 /*return*/, false];
case 3:
_a = true;
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 11];
case 5:
e_10_1 = _g.sent();
e_10 = { error: e_10_1 };
return [3 /*break*/, 11];
case 6:
_g.trys.push([6, , 9, 10]);
if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 8];
return [4 /*yield*/, _e.call(_b)];
case 7:
_g.sent();
_g.label = 8;
case 8: return [3 /*break*/, 10];
case 9:
if (e_10) throw e_10.error;
return [7 /*endfinally*/];
case 10: return [7 /*endfinally*/];
case 11: return [2 /*return*/, true];
}
});
});
}
/**
* Return true if given input is an Iterable instance.
*
* @param input
*/
function isIterable(input) {
if (input === null || input === undefined) {
return false;
}
return (typeof input[Symbol.iterator] ===
"function");
}
/**
* Return true if given input is an AsyncIterable instance.
*
* @param input
*/
function isAsyncIterable(input) {
if (input === null || input === undefined) {
return false;
}
return (typeof input[Symbol.asyncIterator] ===
"function");
}
/**
* Return true if given input is an Iterator instance.
*
* @param input
*/
function isIterator(input) {
if (input === null || input === undefined) {
return false;
}
return (input.next !== undefined &&
typeof input.next === "function");
}
/**
* Returns true if given collection is sorted in descending order; otherwise false.
*
* Items of given collection must be comparable.
*
* Returns true if given collection is empty or has only one element.
*
* @param data
*/
function isReversed(data) {
var e_11, _a;
try {
for (var _b = __values((0, single_1.pairwise)((0, transform_1.toIterable)(data))), _c = _b.next(); !_c.done; _c = _b.next()) {
var _d = __read(_c.value, 2), lhs = _d[0], rhs = _d[1];
if (lhs < rhs) {
return false;
}
}
}
catch (e_11_1) { e_11 = { error: e_11_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_11) throw e_11.error; }
}
return true;
}
/**
* Returns true if given async collection is sorted in descending order; otherwise false.
*
* Items of given collection must be comparable.
*
* Returns true if given collection is empty or has only one element.
*
* @param data
*/
function isReversedAsync(data) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b, _c, _d, lhs, rhs, e_12_1;
var _e, e_12, _f, _g;
return __generator(this, function (_h) {
switch (_h.label) {
case 0:
_h.trys.push([0, 5, 6, 11]);
_a = true, _b = __asyncValues((0, single_1.pairwiseAsync)((0, transform_1.toAsyncIterable)(data)));
_h.label = 1;
case 1: return [4 /*yield*/, _b.next()];
case 2:
if (!(_c = _h.sent(), _e = _c.done, !_e)) return [3 /*break*/, 4];
_g = _c.value;
_a = false;
_d = __read(_g, 2), lhs = _d[0], rhs = _d[1];
if (lhs < rhs) {
return [2 /*return*/, false];
}
_h.label = 3;
case 3:
_a = true;
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 11];
case 5:
e_12_1 = _h.sent();
e_12 = { error: e_12_1 };
return [3 /*break*/, 11];
case 6:
_h.trys.push([6, , 9, 10]);
if (!(!_a && !_e && (_f = _b.return))) return [3 /*break*/, 8];
return [4 /*yield*/, _f.call(_b)];
case 7:
_h.sent();
_h.label = 8;
case 8: return [3 /*break*/, 10];
case 9:
if (e_12) throw e_12.error;
return [7 /*endfinally*/];
case 10: return [7 /*endfinally*/];
case 11: return [2 /*return*/, true];
}
});
});
}
/**
* Returns true if given collection is sorted in ascending order; otherwise false.
*
* Items of given collection must be comparable.
*
* Returns true if given collection is empty or has only one element.
*
* @param data
*/
function isSorted(data) {
var e_13, _a;
try {
for (var _b = __values((0, single_1.pairwise)((0, transform_1.toIterable)(data))), _c = _b.next(); !_c.done; _c = _b.next()) {
var _d = __read(_c.value, 2), lhs = _d[0], rhs = _d[1];
if (lhs > rhs) {
return false;
}
}
}
catch (e_13_1) { e_13 = { error: e_13_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_13) throw e_13.error; }
}
return true;
}
/**
* Returns true if given async collection is sorted in ascending order; otherwise false.
*
* Items of given collection must be comparable.
*
* Returns true if given collection is empty or has only one element.
*
* @param data
*/
function isSortedAsync(data) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b, _c, _d, lhs, rhs, e_14_1;
var _e, e_14, _f, _g;
return __generator(this, function (_h) {
switch (_h.label) {
case 0:
_h.trys.push([0, 5, 6, 11]);
_a = true, _b = __asyncValues((0, single_1.pairwiseAsync)((0, transform_1.toAsyncIterable)(data)));
_h.label = 1;
case 1: return [4 /*yield*/, _b.next()];
case 2:
if (!(_c = _h.sent(), _e = _c.done, !_e)) return [3 /*break*/, 4];
_g = _c.value;
_a = false;
_d = __read(_g, 2), lhs = _d[0], rhs = _d[1];
if (lhs > rhs) {
return [2 /*return*/, false];
}
_h.label = 3;
case 3:
_a = true;
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 11];
case 5:
e_14_1 = _h.sent();
e_14 = { error: e_14_1 };
return [3 /*break*/, 11];
case 6:
_h.trys.push([6, , 9, 10]);
if (!(!_a && !_e && (_f = _b.return))) return [3 /*break*/, 8];
return [4 /*yield*/, _f.call(_b)];
case 7:
_h.sent();
_h.label = 8;
case 8: return [3 /*break*/, 10];
case 9:
if (e_14) throw e_14.error;
return [7 /*endfinally*/];
case 10: return [7 /*endfinally*/];
case 11: return [2 /*return*/, true];
}
});
});
}
/**
* Return true if given input is string.
*
* @param input
*/
function isString(input) {
return typeof input === "string" || input instanceof String;
}
/**
* Returns true if no element matches the predicate function.
*
* Empty collections return true.
*
* @param data
* @param predicate
*/
function noneMatch(data, predicate) {
var e_15, _a;
try {
for (var _b = __values((0, transform_1.toIterable)(data)), _c = _b.next(); !_c.done; _c = _b.next()) {
var datum = _c.value;
if (predicate(datum)) {
return false;
}
}
}
catch (e_15_1) { e_15 = { error: e_15_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_15) throw e_15.error; }
}
return true;
}
/**
* Returns true if no element in async collection matches the predicate function.
*
* Empty collections return true.
*
* @param data
* @param predicate
*/
function noneMatchAsync(data, predicate) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b, _c, datum, e_16_1;
var _d, e_16, _e, _f;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
_g.trys.push([0, 6, 7, 12]);
_a = true, _b = __asyncValues((0, transform_1.toAsyncIterable)(data));
_g.label = 1;
case 1: return [4 /*yield*/, _b.next()];
case 2:
if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 5];
_f = _c.value;
_a = false;
datum = _f;
return [4 /*yield*/, predicate(datum)];
case 3:
if (_g.sent()) {
return [2 /*return*/, false];
}
_g.label = 4;
case 4:
_a = true;
return [3 /*break*/, 1];
case 5: return [3 /*break*/, 12];
case 6:
e_16_1 = _g.sent();
e_16 = { error: e_16_1 };
return [3 /*break*/, 12];
case 7:
_g.trys.push([7, , 10, 11]);
if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 9];
return [4 /*yield*/, _e.call(_b)];
case 8:
_g.sent();
_g.label = 9;
case 9: return [3 /*break*/, 11];
case 10:
if (e_16) throw e_16.error;
return [7 /*endfinally*/];
case 11: return [7 /*endfinally*/];
case 12: return [2 /*return*/, true];
}
});
});
}
/**
* Returns true if all given collections are the same.
*
* For single collection or empty collections list returns true.
*
* @param collections
*/
function same() {
var e_17, _a, e_18, _b;
var collections = [];
for (var _i = 0; _i < arguments.length; _i++) {
collections[_i] = arguments[_i];
}
try {
try {
for (var _c = __values(multi_1.zipEqual.apply(void 0, __spreadArray([], __read(collections), false))), _d = _c.next(); !_d.done; _d = _c.next()) {
var values = _d.value;
try {
for (var _e = (e_18 = void 0, __values((0, single_1.pairwise)(values))), _f = _e.next(); !_f.done; _f = _e.next()) {
var _g = __read(_f.value, 2), lhs = _g[0], rhs = _g[1];
if (lhs !== rhs) {
return false;
}
}
}
catch (e_18_1) { e_18 = { error: e_18_1 }; }
finally {
try {
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
}
finally { if (e_18) throw e_18.error; }
}
}
}
catch (e_17_1) { e_17 = { error: e_17_1 }; }
finally {
try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
}
finally { if (e_17) throw e_17.error; }
}
}
catch (e) {
return false;
}
return true;
}
/**
* Returns true if all given async collections are the same.
*
* For single collection or empty collections list returns true.
*
* @param collections
*/
function sameAsync() {
var collections = [];
for (var _i = 0; _i < arguments.length; _i++) {
collections[_i] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var _a, _b, _c, values, _d, _e, _f, lhs, rhs, e_19_1, e_20;
var e_21, _g;
var _h, e_19, _j, _k;
return __generator(this, function (_l) {
switch (_l.label) {
case 0:
_l.trys.push([0, 13, , 14]);
_l.label = 1;
case 1:
_l.trys.push([1, 6, 7, 12]);
_a = true, _b = __asyncValues(multi_1.zipEqualAsync.apply(void 0, __spreadArray([], __read(collections), false)));
_l.label = 2;
case 2: return [4 /*yield*/, _b.next()];
case 3:
if (!(_c = _l.sent(), _h = _c.done, !_h)) return [3 /*break*/, 5];
_k = _c.value;
_a = false;
values = _k;
try {
for (_d = (e_21 = void 0, __values((0, single_1.pairwise)(values))), _e = _d.next(); !_e.done; _e = _d.next()) {
_f = __read(_e.value, 2), lhs = _f[0], rhs = _f[1];
if (lhs !== rhs) {
return [2 /*return*/, false];
}
}
}
catch (e_21_1) { e_21 = { error: e_21_1 }; }
finally {
try {
if (_e && !_e.done && (_g = _d.return)) _g.call(_d);
}
finally { if (e_21) throw e_21.error; }
}
_l.label = 4;
case 4:
_a = true;
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 12];
case 6:
e_19_1 = _l.sent();
e_19 = { error: e_19_1 };
return [3 /*break*/, 12];
case 7:
_l.trys.push([7, , 10, 11]);
if (!(!_a && !_h && (_j = _b.return))) return [3 /*break*/, 9];
return [4 /*yield*/, _j.call(_b)];
case 8:
_l.sent();
_l.label = 9;
case 9: return [3 /*break*/, 11];
case 10:
if (e_19) throw e_19.error;
return [7 /*endfinally*/];
case 11: return [7 /*endfinally*/];
case 12: return [3 /*break*/, 14];
case 13:
e_20 = _l.sent();
return [2 /*return*/, false];
case 14: return [2 /*return*/, true];
}
});
});
}
/**
* Returns true if all given collections have the same lengths.
*
* For single collection or empty collections list returns true.
*
* @param collections
*/
function sameCount() {
var collections = [];
for (var _i = 0; _i < arguments.length; _i++) {
collections[_i] = arguments[_i];
}
if (collections.length <= 1) {
return true;
}
var counts = collections.map(function (collection) { return (0, reduce_1.toCount)(collection); });
return new Set(counts).size === 1;
}
/**
* Returns true if all given async collections have the same lengths.
*
* For single collection or empty collections list returns true.
*
* @param collections
*/
function sameCountAsync() {
var collections = [];
for (var _i = 0; _i < arguments.length; _i++) {
collections[_i] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var counts, _a;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (collections.length <= 1) {
return [2 /*return*/, true];
}
return [4 /*yield*/, (0, single_1.mapAsync)(collections, function (collection) { 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)(collection)];
case 1: return [2 /*return*/, _a.sent()];
}
}); }); })];
case 1:
counts = _b.sent();
_a = Set.bind;
return [4 /*yield*/, (0, transform_1.toArrayAsync)(counts)];
case 2: return [2 /*return*/, new (_a.apply(Set, [void 0, _b.sent()]))().size === 1];
}
});
});
}
//# sourceMappingURL=summary.js.map