declare global { export interface SymbolConstructor { readonly asyncIterator: symbol; } } export interface AsyncIterator { next(value?: any): Promise>; return?(value?: any): Promise>; throw?(e?: any): Promise>; } export interface AsyncIterable { [Symbol.asyncIterator](): AsyncIterator; } export interface AsyncIterableIterator extends AsyncIterator { [Symbol.asyncIterator](): AsyncIterableIterator; } export declare function isAsyncIterable(o: any): o is AsyncIterable; export declare function isIterable(o: any): o is Iterable; export declare type Operator = (source: AsyncIterable) => AsyncIterable; export declare type Evaluator = (source: AsyncIterable) => Promise; export declare type AsyncQuerySource = Iterable | AsyncIterable | (() => Iterable | AsyncIterable); export declare type OrderKey = string | number; export interface AsyncQueryable extends AsyncIterable { /** * Filters out all elements in a sequence for which `predicate` does not return true. * @param predicate * @example * await range(0, 3).filter(x => x < 2).toArray(); // == [0, 1] */ filter(predicate: (t: TSource, i: number) => boolean | Promise): AsyncQueryable; /** * Transforms each element in a sequence of `TSource` to some number of `TResult`, flattening * the result into a single sequence of `TResult`. * * `transform` performs this transformation. An optional transformation, * `intermediateTransform`, pairs each element of the flattened sequence with the input that was * passed to `transform` to generate it, and returning some arbitrary transformation. See * examples for where this is useful. * @param transform Function performing the transformation of one `TSource` element into many * `TResult`. * @param intermediateTransform Optionally allows transformation of each element of the * flattened list resulting from `transform`. * @example * await from([[1], [2], [3]]).flatMap(x => x).toArray(); // == [1, 2, 3] * @example * // Take a sequence of customers, then for each order, produce a tuple `[customer, order]`. * await customers.flatMap(customer => customer.orders, (customer, order) => [customer, order]); */ flatMap(transform: (t: TSource, index: number) => AsyncQuerySource | Promise>, // TODO: Make this iterable. resultTransform?: (t: TSource, ti: TInner) => TResult | Promise): AsyncQueryable; /** * Transforms a sequence of `TSource` into a sequence of `TResult` by calling `transform` on * every element in the sequence. * @param transform Function performing the transformation of `TSource` to `TResult`. * @example * await range(0, 2).map(x => x * x).toArray(); // == [0, 1, 4] */ map(transform: (t: TSource, i: number) => TResult | Promise): AsyncQueryable; /** * Skips `n` elements of a sequence, then yields the remainder of the sequence. * @param n Number of elements to skip * @example * await range(0, 3).skip(2).toArray(); // == [2] */ skip(n: number): AsyncQueryable; /** * Skips elements in a sequence while `predicate` returns `true` and then yields the rest of the * sequence without testing `predicate` again. * @param predicate Tests whether we should keep skipping elements * @example * await from([1, 2, 3, 4]).skipWhile(x => x < 2).toArray() // == [2, 3, 4] */ skipWhile(predicate: (t: TSource, i: number) => boolean | Promise): AsyncQueryable; /** * Takes `n` elements of a sequence, then skips the remainder of the sequence. * @param n Number of elements to take from the sequence * @example * await range(0, 3).take(2).toArray(); // == [0, 1] */ take(n: number): AsyncQueryable; /** * Takes elements in a sequence while `predicate` returns `true` and then skips the rest of * the sequence without testing `predicate` again. * @param predicate Tests whether we should keep taking elements * @example * await from([1, 2, 3, 4]).takeWhile(x => x < 2).toArray() // == [1] */ takeWhile(predicate: (t: TSource, i: number) => boolean | Promise): AsyncQueryable; join(inner: AsyncQuerySource, outerKeySelector: (to: TSource) => TKey | Promise, innerKeySelector: (ti: TInner) => TKey | Promise, resultSelector: (to: TSource, ti: TInner) => TResult | Promise): AsyncQueryable; groupJoin(inner: AsyncQuerySource, outerKeySelector: (to: TSource) => TKey | Promise, innerKeySelector: (ti: TInner) => TKey | Promise, resultSelector: (to: TSource, ti: AsyncQueryable) => TResult | Promise): AsyncQueryable; /** * Concatenates two sequences. * @param second Sequence to concatenate to `this` sequence. */ concat(second: AsyncQuerySource): AsyncQueryable; /** * Enumerates the elements of a sequence in reverse order. */ reverse(): AsyncQueryable; /** * Sorts the elements of a sequence in ascending order. * * Unlike JavaScript's `Array#sort`, which coerces all objects to string and sorts them * lexically, this method requires `keySelector` to perform any conversions explicitly. Among * other things, this means that numbers are sorted numerically rather than lexically. * @param keySelector Maps an element of the sequence to the key used for sorting. */ orderBy(keySelector: (t: TSource) => OrderKey | Promise): AsyncQueryable; /** * Sorts the elements of a sequence in descending order. * * Unlike JavaScript's `Array#sort`, which coerces all objects to string and sorts them * lexically, this method requires `keySelector` to perform any conversions explicitly. Among * other things, this means that numbers are sorted numerically rather than lexically. * @param keySelector Maps an element of the sequence to the key used for sorting. */ orderByDescending(keySelector: (t: TSource) => OrderKey | Promise): AsyncQueryable; /** * Collect elements in a sequence into groups whose keys match. * @param keySelector Maps an element of the sequence into the key used for grouping. * @param elementSelector Optionally transforms each element of the sequence from `TSource` to * `TResult`. * @example * await from([1, 2, 1]).groupBy(x => x).map(g => g.toArray()).toArray(); // [[1, 1], [2]] */ groupBy(keySelector: (t: TSource) => TKey | Promise, elementSelector?: (t: TSource) => TResult | Promise): AsyncQueryable>; /** * Suppresses duplicate elements in a sequence. */ distinct(): AsyncQueryable; /** * Produces the set union of two sequences. * @param second Sequence to union with `this` sequence. * @example * await from([1, 2, 3]).union([1, 1, 1, 1, 1]).toArray(); // == [1, 2, 3] */ union(second: AsyncQuerySource): AsyncQueryable; /** * Produces the set intersection of two sequences. * @param second Sequence to intersect with `this` sequence. * @example * await from([1, 2, 3]).intersection([1, 1, 1, 1, 1]).toArray(); // == [1] */ intersect(second: AsyncQuerySource): AsyncQueryable; /** * Produces the set difference of two sequences. * @param second Sequence to diff with `this` sequence. * @example * await from([1, 2]).except([1, 1, 1, 2, 3, 1, 1]).toArray(); // == [3] */ except(second: AsyncQuerySource): AsyncQueryable; /** * Find first element in sequence, or first element in sequence that satisfies `predicate`. If * sequence is empty or no elements satisfy this condition, throw an exception. * @param predicate Optional test for elements in the sequence. */ first(predicate?: (t: TSource) => boolean | Promise): Promise; /** * Find first element in sequence, or first element in sequence that satisfies `predicate`, or * return provided default value. * @param defaultValue Default value to return if element cannot be found. * @param predicate Optional test for elements in the sequence. */ firstOrDefault(defaultValue: TSource, predicate?: (t: TSource) => boolean | Promise): Promise; /** * Find last element in sequence, or last element in sequence that satisfies `predicate`. If * sequence is empty or no elements satisfy this condition, throw an exception. * @param predicate Optional test for elements in the sequence. */ last(predicate?: (t: TSource) => boolean | Promise): Promise; /** * Find last element in sequence, or last element in sequence that satisfies `predicate`, or * return provided default value. * @param defaultValue Default value to return if element cannot be found. * @param predicate Optional test for elements in the sequence. */ lastOrDefault(defaultValue: TSource, predicate?: (t: TSource) => boolean | Promise): Promise; /** * Return single element in sequence, or single element in sequence that satisfies `predicate`. * If sequence is empty or no elements satisfy this condition, throw an exception. * @param predicate Optional test for elements in the sequence. */ single(predicate?: (t: TSource) => boolean | Promise): Promise; /** * Find single element in sequence, or single element in sequence that satisfies `predicate`, or * return provided default value. * @param defaultValue Default value to return if element cannot be found. * @param predicate Optional test for elements in the sequence. */ singleOrDefault(defaultValue: TSource, predicate?: (t: TSource) => boolean | Promise): Promise; /** * Return element at `index` in sequence. If `index` is out of range, throw an exception. * @param index Zero-based index of the element to return. */ elementAt(index: number): Promise; /** * Return element at `index` in sequence, or the provided default value if the index does not exist. * @param defaultValue Default value to return if element cannot be found. * @param index Zero-based index of the element to return. */ elementAtOrDefault(defaultValue: TSource, index: number | Promise): Promise; /** * Return `this` sequence, or a sequence containing only `defaultValue` if the sequence is * empty. * @param defaultValue Default value to return if sequence is empty. */ defaultIfEmpty(defaultValue: TSource): AsyncQueryable; /** * Retruns `true` if any element of a sequence exists or satisfies `predicate`. * @param predicate Boolean function to check against elements of the sequence. */ any(predicate?: (t: TSource) => boolean | Promise): Promise; /** * Returns `true` if all elements of a sequence satisfy `predicate`. * @param predicate Boolean function to check against elements of the sequence. */ all(predicate: (t: TSource) => boolean | Promise): Promise; /** * Returns `true` of sequence contains element equal to `value`. * @param value Element to check the sequence contains. */ contains(value: TSource): Promise; /** * Counts the number of elements in a sequence, or the number of elements that satisfy * `predicate`. * @param predicate Function to check against elements of the sequence. */ count(predicate?: (t: TSource) => boolean | Promise): Promise; /** * Sums all the numbers in a sequence. */ sum(): TSource extends number ? Promise : never; /** * Applies `selector` to each element in a sequence, and then sums the resulting numbers. * @param selector Function mapping elements of a sequence to numbers. */ sum(selector?: (t: TSource) => number | Promise): Promise; /** * Finds the minimum number in a sequence. */ min(): TSource extends number ? Promise : never; /** * Applies `selector` to each element in a sequence, and then finds the minimum of the resulting * numbers. * @param selector Function mapping elements of a sequence to numbers. */ min(selector?: (t: TSource) => number | Promise): Promise; /** * Finds the maximum number in a sequence. */ max(): TSource extends number ? Promise : never; /** * Applies `selector` to each element in a sequence, and then finds the maximum of the resulting * numbers. * @param selector Function mapping elements of a sequence to numbers. */ max(selector?: (t: TSource) => number | Promise): Promise; /** * Averages the numbers of a sequence. */ average(): TSource extends number ? Promise : never; /** * Applies `selector` to each element in a sequence, and then averages the resulting numbers. * @param selector Function mapping elements of a sequence to numbers. */ average(selector?: (t: TSource) => number | Promise): Promise; /** * Accumulates a value over a sequence. `func` is applied to each element in the sequence, an * the result of `func` becomes the `acc` argument in the next application. The first `acc` * takes the value `seed`. * @param seed Value of `acc` in the first call to `func`. * @param func Accumulates a result. */ aggregate(seed: TAccumulate, func: (acc: TAccumulate, t: TSource) => TAccumulate | Promise): Promise; /** * Transforms a sequence of `TSource` to a `Promise`. */ toArray(): Promise; /** * Transforms a sequence into a `Map`. * @param keySelector Maps elements in the sequence to keys that will be used in the resulting * `Map`. * @param elementSelector Optionally maps elements in the sequence into the values used in teh * resulting `Map`. */ toMap(keySelector: (t: TSource) => TKey | Promise, elementSelector?: (t: TSource) => TResult | Promise): Promise>; /** * Filter out everything in the sequence that is not of type `TResult`, returning a sequence of * type `TResult`. * @param typeGuard Checks whether element is of type `TResult`. */ ofType(typeGuard: (o: any) => o is TResult): AsyncQueryable; /** * Evaluate a function `f` on each element of a sequence. * @param f Function to run on each element of the sequence. */ forEach(f: (t: TSource) => void | Promise): void; pipe(): AsyncQueryable; pipe(op: Operator): AsyncQueryable; pipe(op1: Operator, op2: Operator): AsyncQueryable; pipe(op1: Operator, op2: Operator, op3: Operator): AsyncQueryable; pipe(op1: Operator, op2: Operator, op3: Operator, op4: Operator): AsyncQueryable; pipe(op1: Operator, op2: Operator, op3: Operator, op4: Operator, op5: Operator): AsyncQueryable; pipe(op1: Operator, op2: Operator, op3: Operator, op4: Operator, op5: Operator, op6: Operator): AsyncQueryable; pipe(op1: Operator, op2: Operator, op3: Operator, op4: Operator, op5: Operator, op6: Operator, op7: Operator): AsyncQueryable; pipe(op1: Operator, op2: Operator, op3: Operator, op4: Operator, op5: Operator, op6: Operator, op7: Operator, op8: Operator): AsyncQueryable; pipe(op1: Operator, op2: Operator, op3: Operator, op4: Operator, op5: Operator, op6: Operator, op7: Operator, op8: Operator, op9: Operator, ...ops: Operator[]): AsyncQueryable; } export interface GroupedAsyncIterable extends AsyncIterable { key: TKey; } export interface AsyncQueryableGrouping extends AsyncQueryable { key: TKey; }