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)

554 lines 16.4 kB
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 __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); } }; import { zipEqual, zipEqualAsync } from "./multi"; import { mapAsync, pairwise, pairwiseAsync } from "./single"; import { toCount, toCountAsync } from "./reduce"; import { toArrayAsync, toAsyncIterable, toIterable } from "./transform"; /** * Returns true if all elements match the predicate function. * * Empty collections return true. * * @param data * @param predicate */ export function allMatch(data, predicate) { for (const datum of toIterable(data)) { if (!predicate(datum)) { return false; } } return true; } /** * Returns true if all elements of async collection match the predicate function. * * Empty collections return true. * * @param data * @param predicate */ export function allMatchAsync(data, predicate) { return __awaiter(this, void 0, void 0, function* () { var _a, e_1, _b, _c; try { for (var _d = true, _e = __asyncValues(toAsyncIterable(data)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const datum = _c; if (!(yield predicate(datum))) { return false; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_1) throw e_1.error; } } 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 */ export function allUnique(data) { const usages = new Set(); for (const datum of toIterable(data)) { if (usages.has(datum)) { return false; } usages.add(datum); } 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 */ export function allUniqueAsync(data) { return __awaiter(this, void 0, void 0, function* () { var _a, e_2, _b, _c; const usages = new Set(); try { for (var _d = true, _e = __asyncValues(toAsyncIterable(data)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const datum = _c; if (usages.has(datum)) { return false; } usages.add(datum); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_2) throw e_2.error; } } return true; }); } /** * Returns true if any element matches the predicate function. * * Empty collections return false. * * @param data * @param predicate */ export function anyMatch(data, predicate) { for (const datum of toIterable(data)) { if (predicate(datum)) { return true; } } return false; } /** * Returns true if any element of async collection matches the predicate function. * * Empty collections return false. * * @param data * @param predicate */ export function anyMatchAsync(data, predicate) { return __awaiter(this, void 0, void 0, function* () { var _a, e_3, _b, _c; try { for (var _d = true, _e = __asyncValues(toAsyncIterable(data)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const datum = _c; if (yield predicate(datum)) { return true; } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_3) throw e_3.error; } } 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 */ export function exactlyN(data, n, predicate) { if (n < 0) { return false; } if (predicate === undefined) { predicate = (datum) => Boolean(datum); } let count = 0; for (const datum of toIterable(data)) { if (predicate(datum)) { count++; if (count > n) { return false; } } } 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 */ export function exactlyNAsync(data, n, predicate) { return __awaiter(this, void 0, void 0, function* () { var _a, e_4, _b, _c; if (n < 0) { return false; } if (predicate === undefined) { predicate = (datum) => Boolean(datum); } let count = 0; try { for (var _d = true, _e = __asyncValues(toAsyncIterable(data)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const datum = _c; if (yield predicate(datum)) { count++; if (count > n) { return false; } } } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_4) throw e_4.error; } } return count === n; }); } /** * Returns true if given collection is empty. * * @param data */ export function isEmpty(data) { /* eslint-disable @typescript-eslint/no-unused-vars */ for (const _ of toIterable(data)) { return false; } return true; } /** * Returns true if given async collection is empty. * * @param data */ export function isEmptyAsync(data) { return __awaiter(this, void 0, void 0, function* () { var _a, e_5, _b, _c; try { /* eslint-disable @typescript-eslint/no-unused-vars */ for (var _d = true, _e = __asyncValues(toAsyncIterable(data)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const _ = _c; return false; } } catch (e_5_1) { e_5 = { error: e_5_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_5) throw e_5.error; } } return true; }); } /** * Return true if given input is an Iterable instance. * * @param input */ export 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 */ export 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 */ export 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 */ export function isReversed(data) { for (const [lhs, rhs] of pairwise(toIterable(data))) { if (lhs < rhs) { return false; } } 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 */ export function isReversedAsync(data) { return __awaiter(this, void 0, void 0, function* () { var _a, e_6, _b, _c; try { for (var _d = true, _e = __asyncValues(pairwiseAsync(toAsyncIterable(data))), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const [lhs, rhs] = _c; if (lhs < rhs) { return false; } } } catch (e_6_1) { e_6 = { error: e_6_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_6) throw e_6.error; } } 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 */ export function isSorted(data) { for (const [lhs, rhs] of pairwise(toIterable(data))) { if (lhs > rhs) { return false; } } 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 */ export function isSortedAsync(data) { return __awaiter(this, void 0, void 0, function* () { var _a, e_7, _b, _c; try { for (var _d = true, _e = __asyncValues(pairwiseAsync(toAsyncIterable(data))), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const [lhs, rhs] = _c; if (lhs > rhs) { return false; } } } catch (e_7_1) { e_7 = { error: e_7_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_7) throw e_7.error; } } return true; }); } /** * Return true if given input is string. * * @param input */ export 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 */ export function noneMatch(data, predicate) { for (const datum of toIterable(data)) { if (predicate(datum)) { return false; } } return true; } /** * Returns true if no element in async collection matches the predicate function. * * Empty collections return true. * * @param data * @param predicate */ export function noneMatchAsync(data, predicate) { return __awaiter(this, void 0, void 0, function* () { var _a, e_8, _b, _c; try { for (var _d = true, _e = __asyncValues(toAsyncIterable(data)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const datum = _c; if (yield predicate(datum)) { return false; } } } catch (e_8_1) { e_8 = { error: e_8_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_8) throw e_8.error; } } return true; }); } /** * Returns true if all given collections are the same. * * For single collection or empty collections list returns true. * * @param collections */ export function same(...collections) { try { for (const values of zipEqual(...collections)) { for (const [lhs, rhs] of pairwise(values)) { if (lhs !== rhs) { return false; } } } } 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 */ export function sameAsync(...collections) { return __awaiter(this, void 0, void 0, function* () { var _a, e_9, _b, _c; try { try { for (var _d = true, _e = __asyncValues(zipEqualAsync(...collections)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const values = _c; for (const [lhs, rhs] of pairwise(values)) { if (lhs !== rhs) { return false; } } } } catch (e_9_1) { e_9 = { error: e_9_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_9) throw e_9.error; } } } catch (e) { return false; } return true; }); } /** * Returns true if all given collections have the same lengths. * * For single collection or empty collections list returns true. * * @param collections */ export function sameCount(...collections) { if (collections.length <= 1) { return true; } const counts = collections.map((collection) => 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 */ export function sameCountAsync(...collections) { return __awaiter(this, void 0, void 0, function* () { if (collections.length <= 1) { return true; } const counts = yield mapAsync(collections, (collection) => __awaiter(this, void 0, void 0, function* () { return yield toCountAsync(collection); })); return new Set(yield toArrayAsync(counts)).size === 1; }); } //# sourceMappingURL=summary.js.map