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.

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