UNPKG

iterator-helper

Version:

Provide helpers that polyfill all methods defined in [iterator helpers proposal](https://github.com/tc39/proposal-iterator-helpers), both for `Iterator` and `AsyncIterator`, and even more.

96 lines (95 loc) 8.28 kB
import { AsyncIteratorOrIterable, IteratorSlot } from '../types'; export declare class HAsyncIterator<T, TReturn = any, TNext = undefined> implements AsyncIterator<T, TReturn, TNext> { [IteratorSlot]: AsyncIterator<T, TReturn, TNext>; constructor(iterator?: AsyncIterator<T, TReturn, TNext>); static from<T, TReturn, TNext>(iterator: AsyncIterator<T, TReturn, TNext>): HAsyncIterator<T, TReturn, TNext>; static from<T>(iterator: AsyncIterable<T>): HAsyncIterator<T>; static from<T, TReturn = any, TNext = undefined>(iterator: AsyncIterator<T, TReturn, TNext> | AsyncIterable<T>): HAsyncIterator<T, TReturn, TNext>; next(val?: TNext): Promise<IteratorResult<T, TReturn>>; throw(val?: any): Promise<IteratorResult<T, TReturn>>; return(val?: TReturn): Promise<IteratorResult<T, TReturn>>; /** Map each value of iterator to another value via {callback}. */ map<R>(callback: (value: T) => R | PromiseLike<R>): HAsyncIterator<R, TReturn, TNext>; static map<T, R>(this: AsyncIterator<T>, callback: (value: T) => R | PromiseLike<R>): AsyncGenerator<R, any, unknown>; /** Each value is given through {callback}, return `true` if value is needed into returned iterator. */ filter(callback: (value: T) => boolean | PromiseLike<boolean>): HAsyncIterator<T, TReturn, TNext>; static filter<T>(this: AsyncIterator<T>, callback: (value: T) => boolean | PromiseLike<boolean>): AsyncGenerator<T, any, unknown>; /** Find a specific value that returns `true` in {callback}, and return it. Returns `undefined` otherwise. */ find(callback: (value: T) => boolean | PromiseLike<boolean>): Promise<T | undefined>; /** Return `true` if each value of iterator validate {callback}. */ every(callback: (value: T) => boolean | PromiseLike<boolean>): Promise<boolean>; /** Return `true` if one value of iterator validate {callback}. */ some(callback: (value: T) => boolean | PromiseLike<boolean>): Promise<boolean>; /** Consume iterator and collapse values inside an array. */ toArray(max_count?: number): Promise<T[]>; /** Create a new iterator that consume {limit} items, then stops. */ take(limit: number): HAsyncIterator<T, TReturn, TNext>; static take<T>(this: AsyncIterator<T>, limit: number): AsyncGenerator<T, any, unknown>; /** Create a new iterator that skip {limit} items from source iterator, then yield all values. */ drop(limit: number): HAsyncIterator<T, TReturn, TNext>; static drop<T>(this: AsyncIterator<T>, limit: number): AsyncGenerator<T, any, unknown>; /** Get a pair [index, value] for each remaining value of iterable. */ asIndexedPairs(): HAsyncIterator<[number, T], TReturn, TNext>; static asIndexedPairs<T>(this: AsyncIterator<T>): AsyncGenerator<(number | T)[], any, unknown>; /** Like map, but you can return a new iterator that will be flattened. */ flatMap<R>(mapper: (value: T) => AsyncIterator<R> | R | PromiseLike<AsyncIterator<R>> | PromiseLike<R>): HAsyncIterator<R, TReturn, TNext>; static flatMap<T, R>(this: AsyncIterator<T>, mapper: (value: T) => AsyncIterator<R> | R | PromiseLike<AsyncIterator<R>> | PromiseLike<R>): AsyncGenerator<any, any, any>; /** Accumulate each item inside **acc** for each value **value**. */ reduce<V>(reducer: (acc: V, value: T) => V | PromiseLike<V>, initial_value?: V): Promise<V>; /** Iterate over each value of iterator by calling **callback** for each value. */ forEach(callback: (value: T) => any | PromiseLike<any>): Promise<void>; /** Join all the remaining elements of the iterator in a single string with glue {glue}. */ join(string: string): Promise<string>; /** End the iterator and return the number of remaining items. */ count(): Promise<number>; /** Iterate through current iterator, then through the given iterators in the correct order. */ chain<I>(...iterables: AsyncIteratorOrIterable<I>[]): HAsyncIterator<T | I, TReturn, TNext>; static chain<T, I>(this: AsyncIterableIterator<T>, ...iterables: AsyncIteratorOrIterable<I>[]): AsyncGenerator<T | I, any, any>; /** Iterate through multiple iterators together. */ zip<O>(other: AsyncIteratorOrIterable<O>): HAsyncIterator<[T, O][], TReturn, TNext>; zip<O>(...others: AsyncIteratorOrIterable<O>[]): HAsyncIterator<(T | O)[], TReturn, TNext>; static zip<T, O>(this: AsyncIteratorOrIterable<T>, ...others: AsyncIteratorOrIterable<O>[]): AsyncIterator<(T | O)[]>; /** Continue iterator until {callback} return a falsy value. */ takeWhile(callback: (value: T) => boolean | PromiseLike<boolean>): HAsyncIterator<T, TReturn, TNext>; static takeWhile<T>(this: AsyncIterator<T>, callback: (value: T) => boolean | PromiseLike<boolean>): AsyncGenerator<T, any, unknown>; /** Skip elements until {callback} return a truthy value. */ dropWhile(callback: (value: T) => boolean | PromiseLike<boolean>): HAsyncIterator<T, TReturn, TNext>; static dropWhile<T>(this: AsyncIterator<T>, callback: (value: T) => boolean | PromiseLike<boolean>): AsyncGenerator<T, any, unknown>; /** Continue iterator until `null` or `undefined` is encountered. */ fuse(): HAsyncIterator<T, TReturn, TNext>; static fuse<T>(this: AsyncIterator<T>): AsyncGenerator<T, any, unknown>; /** Partition {true} elements to first array, {false} elements to second one. */ partition(callback: (value: T) => boolean | PromiseLike<boolean>): Promise<T[][]>; /** Group by objects by key according to returned key for each object. */ groupBy<K extends string | number | symbol>(callback: (value: T) => K | PromiseLike<K>): Promise<{ [Key in K]: T[]; }>; /** Index this iterator objects in a {Map} with key obtained through {keyGetter}. */ toIndexedItems<K>(keyGetter: (value: T) => K | PromiseLike<K>): Promise<Map<K, T>>; /** * Iterate over items present in both current collection and {otherItems} iterable. * **Warning**: This is a O(n*m) operation and this will consume {otherItems} iterator/iterable! */ intersection<O>(otherItems: AsyncIteratorOrIterable<O>, isSameItemCallback?: (value: T, other: O) => boolean | PromiseLike<boolean>): HAsyncIterator<T, TReturn, TNext>; static intersection<T, O>(this: AsyncIterator<T>, otherItems: AsyncIteratorOrIterable<O>, isSameItemCallback?: (value: T, other: O) => boolean | PromiseLike<boolean>): AsyncGenerator<T, any, unknown>; /** * Iterate over items present only in current collection, not in {otherItems} iterable. * **Warning**: This is a O(n*m) operation and this will consume {otherItems} iterator/iterable! */ difference<O>(otherItems: AsyncIteratorOrIterable<O>, isSameItemCallback?: (value: T, other: O) => boolean | PromiseLike<boolean>): HAsyncIterator<T, TReturn, TNext>; static difference<T, O>(this: AsyncIterator<T>, otherItems: AsyncIteratorOrIterable<O>, isSameItemCallback?: (value: T, other: O) => boolean | PromiseLike<boolean>): AsyncGenerator<T, any, unknown>; /** * Iterate over items present only in current collection or only in {otherItems} iterable, but not in both. * **Warning**: This is a O(n*m) operation and this will consume {otherItems} iterator/iterable! */ symmetricDifference<O>(otherItems: AsyncIteratorOrIterable<O>, isSameItemCallback?: (value: T, other: O) => boolean | PromiseLike<boolean>): HAsyncIterator<T | O, TReturn, TNext>; static symmetricDifference<T, O>(this: AsyncIterator<T>, otherItems: AsyncIteratorOrIterable<O>, isSameItemCallback?: (value: T, other: O) => boolean | PromiseLike<boolean>): AsyncGenerator<T | O, any, unknown>; /** Find the iterator index of the first element that returns a truthy value, -1 otherwise. */ findIndex(callback: (value: T) => boolean | PromiseLike<boolean>): Promise<number>; /** Only works if it is a number iterator. Returns the maximum of iterator. */ max(): Promise<number>; /** Only works if it is a number iterator. Returns the minimum of iterator. */ min(): Promise<number>; /** When iterator ends, go back to the first item then loop. Indefinitively. */ cycle(): HAsyncIterator<T, TReturn, TNext>; static cycle<T>(this: AsyncIterator<T>): AsyncGenerator<T, void, unknown>; [Symbol.asyncIterator](): this; }