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)

212 lines (211 loc) 7.47 kB
import { Comparable, Numeric } from "./types"; /** * Reduces iterable source like `array.reduce()` function. * * @param data * @param reducer * @param initialValue */ export declare function toValue<TInput, TOutput>(data: Iterable<TInput> | Iterator<TInput>, reducer: (carry: TOutput, datum: TInput) => TOutput, initialValue?: TOutput): TOutput; /** * Reduces async iterable source like `array.reduce()` function. * * @param data * @param reducer * @param initialValue */ export declare function toValueAsync<TInput, TOutput>(data: AsyncIterable<TInput> | AsyncIterator<TInput> | Iterable<TInput> | Iterator<TInput>, reducer: (carry: TOutput, datum: TInput) => TOutput | Promise<TOutput>, initialValue?: TOutput): Promise<TOutput>; /** * Reduces given collection to the mean average of its items. * * Returns `undefined` if given collection is empty. * * @param data */ export declare function toAverage(data: Iterable<number> | Iterator<number>): number | undefined; /** * Reduces given async collection to the mean average of its items. * * Returns `undefined` if given collection is empty. * * @param data */ export declare function toAverageAsync(data: AsyncIterable<number> | AsyncIterator<number> | Iterable<number> | Iterator<number>): Promise<number | 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 */ export declare function toMax<TValue>(data: Iterable<TValue> | Iterator<TValue>, compareBy?: (datum: TValue) => Comparable): TValue | undefined; /** * 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 */ export declare function toMaxAsync<TValue>(data: AsyncIterable<TValue> | AsyncIterator<TValue> | Iterable<TValue> | Iterator<TValue>, compareBy?: (datum: TValue) => Promise<Comparable> | Comparable): Promise<TValue | undefined>; /** * 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 */ export declare function toMin<TValue>(data: Iterable<TValue> | Iterator<TValue>, compareBy?: (datum: TValue) => Comparable): TValue | undefined; /** * 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 */ export declare function toMinAsync<TValue>(data: AsyncIterable<TValue> | AsyncIterator<TValue> | Iterable<TValue> | Iterator<TValue>, compareBy?: (datum: TValue) => Promise<Comparable> | Comparable): Promise<TValue | undefined>; /** * 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 */ export declare function toMinMax<T>(data: Iterable<T> | Iterator<T>, compareBy?: (item: T) => Comparable): [T?, T?]; /** * 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 */ export declare function toMinMaxAsync<T>(data: AsyncIterable<T> | AsyncIterator<T> | Iterable<T> | Iterator<T>, compareBy?: (item: T) => Promise<Comparable> | Comparable): Promise<[T?, T?]>; /** * Reduces given collection to its range. * * Returns 0 if given collection is empty. * * @param numbers */ export declare function toRange(numbers: Iterable<Numeric> | Iterator<Numeric>): number; /** * Reduces given async collection to its range. * * Returns 0 if given async collection is empty. * * @param numbers */ export declare function toRangeAsync(numbers: AsyncIterable<Numeric> | AsyncIterator<Numeric> | Iterable<Numeric> | Iterator<Numeric>): Promise<number>; /** * Reduces given collection to the sum of its items. * * @param data */ export declare function toSum(data: Iterable<number> | Iterator<number>): number; /** * Reduces given async collection to the sum of its items. * * @param data */ export declare function toSumAsync(data: AsyncIterable<number> | AsyncIterator<number> | Iterable<number> | Iterator<number>): Promise<number>; /** * Reduces given collection to the product of its items. * * Returns `undefined` if given collection is empty. * * @param data */ export declare function toProduct(data: Iterable<number> | Iterator<number>): number | undefined; /** * Reduces given async collection to the product of its items. * * Returns `undefined` if given collection is empty. * * @param data */ export declare function toProductAsync(data: AsyncIterable<number> | AsyncIterator<number> | Iterable<number> | Iterator<number>): Promise<number | undefined>; /** * Reduces given iterable to its length. * * @param data */ export declare function toCount(data: Iterable<unknown> | Iterator<unknown>): number; /** * Reduces given async iterable to its length. * * @param data */ export declare function toCountAsync(data: AsyncIterable<unknown> | AsyncIterator<unknown> | Iterable<unknown> | Iterator<unknown>): Promise<number>; /** * Reduces given collection to its first value. * * @param data * * @throws LengthError if given collection is empty. */ export declare function toFirst<T>(data: Iterable<T> | Iterator<T>): T; /** * Reduces given async collection to its first value. * * @param data * * @throws LengthError if given collection is empty. */ export declare function toFirstAsync<T>(data: AsyncIterable<T> | AsyncIterator<T> | Iterable<T> | Iterator<T>): Promise<T>; /** * Reduces given collection to its last value. * * @param data * * @throws LengthError if given collection is empty. */ export declare function toLast<T>(data: Iterable<T> | Iterator<T>): T; /** * Reduces given async collection to its last value. * * @param data * * @throws LengthError if given collection is empty. */ export declare function toLastAsync<T>(data: AsyncIterable<T> | AsyncIterator<T> | Iterable<T> | Iterator<T>): Promise<T>; /** * Reduces given collection to its first and last values. * * @param data * * @throws LengthError if given collection is empty. */ export declare function toFirstAndLast<T>(data: Iterable<T> | Iterator<T>): [T, T]; /** * Reduces given async collection to its first and last values. * * @param data * * @throws LengthError if given collection is empty. */ export declare function toFirstAndLastAsync<T>(data: AsyncIterable<T> | AsyncIterator<T> | Iterable<T> | Iterator<T>): Promise<[T, T]>;