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)

814 lines 31.3 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.toValue = toValue; exports.toValueAsync = toValueAsync; exports.toAverage = toAverage; exports.toAverageAsync = toAverageAsync; exports.toMax = toMax; exports.toMaxAsync = toMaxAsync; exports.toMin = toMin; exports.toMinAsync = toMinAsync; exports.toMinMax = toMinMax; exports.toMinMaxAsync = toMinMaxAsync; exports.toRange = toRange; exports.toRangeAsync = toRangeAsync; exports.toSum = toSum; exports.toSumAsync = toSumAsync; exports.toProduct = toProduct; exports.toProductAsync = toProductAsync; exports.toCount = toCount; exports.toCountAsync = toCountAsync; exports.toFirst = toFirst; exports.toFirstAsync = toFirstAsync; exports.toLast = toLast; exports.toLastAsync = toLastAsync; exports.toFirstAndLast = toFirstAndLast; exports.toFirstAndLastAsync = toFirstAndLastAsync; var transform_1 = require("./transform"); var exceptions_1 = require("./exceptions"); var summary_1 = require("./summary"); var tools_1 = require("./tools"); var single_1 = require("./single"); /** * Reduces iterable source like `array.reduce()` function. * * @param data * @param reducer * @param initialValue */ function toValue(data, reducer, initialValue) { var e_1, _a; var carry = initialValue; try { for (var _b = __values((0, transform_1.toIterable)(data)), _c = _b.next(); !_c.done; _c = _b.next()) { var datum = _c.value; carry = reducer(carry, datum); } } 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 carry; } /** * Reduces async iterable source like `array.reduce()` function. * * @param data * @param reducer * @param initialValue */ function toValueAsync(data, reducer, initialValue) { return __awaiter(this, void 0, void 0, function () { var carry, _a, _b, _c, datum, e_2_1; var _d, e_2, _e, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: carry = initialValue; _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*/, reducer(carry, datum)]; case 4: carry = _g.sent(); _g.label = 5; case 5: _a = true; return [3 /*break*/, 2]; case 6: return [3 /*break*/, 13]; case 7: e_2_1 = _g.sent(); e_2 = { error: e_2_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_2) throw e_2.error; return [7 /*endfinally*/]; case 12: return [7 /*endfinally*/]; case 13: return [2 /*return*/, carry]; } }); }); } /** * Reduces given collection to the mean average of its items. * * Returns `undefined` if given collection is empty. * * @param data */ function toAverage(data) { var _a = __read(toValue(data, function (carry, datum) { var _a = __read(carry, 2), count = _a[0], sum = _a[1]; return [count + 1, sum + Number(datum)]; }, [0, 0]), 2), count = _a[0], sum = _a[1]; return count ? sum / count : undefined; } /** * Reduces given async collection to the mean average of its items. * * Returns `undefined` if given collection is empty. * * @param data */ function toAverageAsync(data) { return __awaiter(this, void 0, void 0, function () { var _a, count, sum; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, toValueAsync(data, function (carry, datum) { var _a = __read(carry, 2), count = _a[0], sum = _a[1]; return [count + 1, sum + Number(datum)]; }, [0, 0])]; case 1: _a = __read.apply(void 0, [(_b.sent()), 2]), count = _a[0], sum = _a[1]; return [2 /*return*/, count ? sum / count : undefined]; } }); }); } /** * Reduces given iterable to its max value. * * Optional callable param `compareBy` must return comparable value. * If `compareBy` is not provided then items of given collection must be comparable. * * Returns `undefined` if given collection is empty. * * @param data * @param compareBy */ function toMax(data, compareBy) { if (compareBy !== undefined) { return toValue(data, function (carry, datum) { return compareBy(datum) > compareBy(carry !== null && carry !== void 0 ? carry : datum) ? datum : carry !== null && carry !== void 0 ? carry : datum; }); } return toValue(data, function (carry, datum) { var lhs = carry !== null && carry !== void 0 ? carry : datum; var rhs = datum; return lhs >= rhs ? lhs : rhs; }); } /** * Reduces given async iterable to its max value. * * Optional callable param `compareBy` must return comparable value. * If `compareBy` is not provided then items of given collection must be comparable. * * Returns `undefined` if given collection is empty. * * @param data * @param compareBy */ function toMaxAsync(data, compareBy) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(compareBy !== undefined)) return [3 /*break*/, 2]; return [4 /*yield*/, toValueAsync(data, function (carry, datum) { return __awaiter(_this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, compareBy(datum)]; case 1: _a = (_b.sent()); return [4 /*yield*/, compareBy(carry !== null && carry !== void 0 ? carry : datum)]; case 2: return [2 /*return*/, _a > (_b.sent()) ? datum : carry !== null && carry !== void 0 ? carry : datum]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; case 2: return [4 /*yield*/, toValueAsync(data, function (carry, datum) { var lhs = carry !== null && carry !== void 0 ? carry : datum; var rhs = datum; return lhs >= rhs ? lhs : rhs; })]; case 3: return [2 /*return*/, _a.sent()]; } }); }); } /** * Reduces given iterable to its min value. * * Optional callable param `compareBy` must return comparable value. * If `compareBy` is not provided then items of given collection must be comparable. * * Returns `undefined` if given collection is empty. * * @param data * @param compareBy */ function toMin(data, compareBy) { if (compareBy !== undefined) { return toValue(data, function (carry, datum) { return compareBy(datum) < compareBy(carry !== null && carry !== void 0 ? carry : datum) ? datum : carry !== null && carry !== void 0 ? carry : datum; }); } return toValue(data, function (carry, datum) { var lhs = carry !== null && carry !== void 0 ? carry : datum; var rhs = datum; return lhs <= rhs ? lhs : rhs; }); } /** * Reduces given async iterable to its min value. * * Optional callable param `compareBy` must return comparable value. * If `compareBy` is not provided then items of given collection must be comparable. * * Returns `undefined` if given collection is empty. * * @param data * @param compareBy */ function toMinAsync(data, compareBy) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(compareBy !== undefined)) return [3 /*break*/, 2]; return [4 /*yield*/, toValueAsync(data, function (carry, datum) { return __awaiter(_this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, compareBy(datum)]; case 1: _a = (_b.sent()); return [4 /*yield*/, compareBy(carry !== null && carry !== void 0 ? carry : datum)]; case 2: return [2 /*return*/, _a < (_b.sent()) ? datum : carry !== null && carry !== void 0 ? carry : datum]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; case 2: return [4 /*yield*/, toValueAsync(data, function (carry, datum) { var lhs = carry !== null && carry !== void 0 ? carry : datum; var rhs = datum; return lhs <= rhs ? lhs : rhs; })]; case 3: 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 data * @param compareBy */ function toMinMax(data, compareBy) { var comparableGetter = compareBy !== undefined ? compareBy : function (item) { return item; }; return toValue(data, function (carry, datum) { var _a, _b, _c, _d; carry = carry; return [ comparableGetter(datum) <= comparableGetter((_a = carry[0]) !== null && _a !== void 0 ? _a : datum) ? datum : (_b = carry[0]) !== null && _b !== void 0 ? _b : datum, comparableGetter(datum) >= comparableGetter((_c = carry[1]) !== null && _c !== void 0 ? _c : datum) ? datum : (_d = carry[1]) !== null && _d !== void 0 ? _d : datum, ]; }, [undefined, undefined]); } /** * Reduces given async 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 data * @param compareBy */ function toMinMaxAsync(data, compareBy) { return __awaiter(this, void 0, void 0, function () { var comparableGetter; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: comparableGetter = compareBy !== undefined ? compareBy : function (item) { return item; }; return [4 /*yield*/, toValueAsync(data, function (carry, datum) { return __awaiter(_this, void 0, void 0, function () { var _a, _b, _c; var _d, _e, _f, _g; return __generator(this, function (_h) { switch (_h.label) { case 0: carry = carry; return [4 /*yield*/, comparableGetter(datum)]; case 1: _a = (_h.sent()); return [4 /*yield*/, comparableGetter((_d = carry[0]) !== null && _d !== void 0 ? _d : datum)]; case 2: _b = [ _a <= (_h.sent()) ? datum : (_e = carry[0]) !== null && _e !== void 0 ? _e : datum ]; return [4 /*yield*/, comparableGetter(datum)]; case 3: _c = (_h.sent()); return [4 /*yield*/, comparableGetter((_f = carry[1]) !== null && _f !== void 0 ? _f : datum)]; case 4: return [2 /*return*/, _b.concat([ _c >= (_h.sent()) ? datum : (_g = carry[1]) !== null && _g !== void 0 ? _g : datum ])]; } }); }); }, [undefined, undefined])]; case 1: return [2 /*return*/, (_a.sent())]; } }); }); } /** * Reduces given collection to its range. * * Returns 0 if given collection is empty. * * @param numbers */ function toRange(numbers) { var _a = __read(toMinMax((0, single_1.map)(numbers, function (n) { return Number(n); })), 2), min = _a[0], max = _a[1]; return (max !== null && max !== void 0 ? max : 0) - (min !== null && min !== void 0 ? min : 0); } /** * Reduces given async collection to its range. * * Returns 0 if given async collection is empty. * * @param numbers */ function toRangeAsync(numbers) { return __awaiter(this, void 0, void 0, function () { var _a, min, max; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, toMinMaxAsync((0, single_1.mapAsync)(numbers, function (n) { return Number(n); }))]; case 1: _a = __read.apply(void 0, [_b.sent(), 2]), min = _a[0], max = _a[1]; return [2 /*return*/, (max !== null && max !== void 0 ? max : 0) - (min !== null && min !== void 0 ? min : 0)]; } }); }); } /** * Reduces given collection to the sum of its items. * * @param data */ function toSum(data) { return toValue(data, function (carry, datum) { return carry + Number(datum); }, 0); } /** * Reduces given async collection to the sum of its items. * * @param data */ function toSumAsync(data) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, toValueAsync(data, function (carry, datum) { return carry + Number(datum); }, 0)]; case 1: return [2 /*return*/, (_a.sent())]; } }); }); } /** * Reduces given collection to the product of its items. * * Returns `undefined` if given collection is empty. * * @param data */ function toProduct(data) { return toValue(data, function (carry, datum) { return (carry !== null && carry !== void 0 ? carry : 1) * datum; }); } /** * Reduces given async collection to the product of its items. * * Returns `undefined` if given collection is empty. * * @param data */ function toProductAsync(data) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, toValueAsync(data, function (carry, datum) { return (carry !== null && carry !== void 0 ? carry : 1) * datum; })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); } /** * Reduces given iterable to its length. * * @param data */ function toCount(data) { switch (true) { case data instanceof Array: return data.length; case (0, summary_1.isString)(data): return data.length; case data instanceof Set: return data.size; case data instanceof Map: return data.size; } return toValue(data, function (carry) { return carry + 1; }, 0); } /** * Reduces given async iterable to its length. * * @param data */ function toCountAsync(data) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: switch (true) { case data instanceof Array: case (0, summary_1.isString)(data): case data instanceof Set: case data instanceof Map: return [2 /*return*/, toCount(data)]; } return [4 /*yield*/, toValueAsync(data, function (carry) { return carry + 1; }, 0)]; case 1: return [2 /*return*/, (_a.sent())]; } }); }); } /** * Reduces given collection to its first value. * * @param data * * @throws LengthError if given collection is empty. */ function toFirst(data) { var e_3, _a; try { for (var _b = __values((0, transform_1.toIterable)(data)), _c = _b.next(); !_c.done; _c = _b.next()) { var datum = _c.value; return 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; } } throw new exceptions_1.LengthError("Collection is empty"); } /** * Reduces given async collection to its first value. * * @param data * * @throws LengthError if given collection is empty. */ function toFirstAsync(data) { return __awaiter(this, void 0, void 0, function () { var _a, _b, _c, datum, e_4_1; var _d, e_4, _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; datum = _f; return [2 /*return*/, datum]; case 3: _a = true; return [3 /*break*/, 1]; case 4: return [3 /*break*/, 11]; case 5: e_4_1 = _g.sent(); e_4 = { error: e_4_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_4) throw e_4.error; return [7 /*endfinally*/]; case 10: return [7 /*endfinally*/]; case 11: throw new exceptions_1.LengthError("Collection is empty"); } }); }); } /** * Reduces given collection to its last value. * * @param data * * @throws LengthError if given collection is empty. */ function toLast(data) { var e_5, _a; var isEmpty = true; var result; try { for (var _b = __values((0, transform_1.toIterable)(data)), _c = _b.next(); !_c.done; _c = _b.next()) { var datum = _c.value; result = datum; isEmpty = false; } } 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; } } if (isEmpty) { throw new exceptions_1.LengthError("Collection is empty"); } return result; } /** * Reduces given async collection to its last value. * * @param data * * @throws LengthError if given collection is empty. */ function toLastAsync(data) { return __awaiter(this, void 0, void 0, function () { var isEmpty, result, _a, _b, _c, datum, e_6_1; var _d, e_6, _e, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: isEmpty = true; _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; result = datum; isEmpty = false; _g.label = 4; case 4: _a = true; return [3 /*break*/, 2]; 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: if (isEmpty) { throw new exceptions_1.LengthError("Collection is empty"); } return [2 /*return*/, result]; } }); }); } /** * Reduces given collection to its first and last values. * * @param data * * @throws LengthError if given collection is empty. */ function toFirstAndLast(data) { var e_7, _a; var first = tools_1.NoValueMonad; var last = tools_1.NoValueMonad; try { for (var _b = __values((0, transform_1.toIterable)(data)), _c = _b.next(); !_c.done; _c = _b.next()) { var value = _c.value; if (first === tools_1.NoValueMonad) { first = value; } last = value; } } 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; } } if (first === tools_1.NoValueMonad) { throw new exceptions_1.LengthError("Collection is empty"); } return [first, last]; } /** * Reduces given async collection to its first and last values. * * @param data * * @throws LengthError if given collection is empty. */ function toFirstAndLastAsync(data) { return __awaiter(this, void 0, void 0, function () { var first, last, _a, _b, _c, value, e_8_1; var _d, e_8, _e, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: first = tools_1.NoValueMonad; last = tools_1.NoValueMonad; _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; value = _f; if (first === tools_1.NoValueMonad) { first = value; } last = value; _g.label = 4; case 4: _a = true; return [3 /*break*/, 2]; case 5: return [3 /*break*/, 12]; case 6: e_8_1 = _g.sent(); e_8 = { error: e_8_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_8) throw e_8.error; return [7 /*endfinally*/]; case 11: return [7 /*endfinally*/]; case 12: if (first === tools_1.NoValueMonad) { throw new exceptions_1.LengthError("Collection is empty"); } return [2 /*return*/, [first, last]]; } }); }); } //# sourceMappingURL=reduce.js.map