|
| 1 | +// https://github.com/tc39/proposal-async-iterator-helpers |
| 2 | + |
| 3 | +interface AsyncIteratorConstructor { |
| 4 | + /** |
| 5 | + * Creates an `AsyncIterator` from an iterable object |
| 6 | + * @param iterable An `AsyncIterable`, `Iterable`, or `AsyncIterator` to convert to an `AsyncIterator` |
| 7 | + * @returns A new `AsyncIterator` instance |
| 8 | + */ |
| 9 | + from<T>(iterable: AsyncIterable<T> | Iterable<T> | AsyncIteratorObject<T>): AsyncIteratorObject<T>; |
| 10 | +} |
| 11 | + |
| 12 | +declare var AsyncIterator: AsyncIteratorConstructor; |
| 13 | + |
| 14 | +interface AsyncIterator<T, TReturn = any, TNext = undefined> { |
| 15 | + /** |
| 16 | + * Drops elements from the iterator until the limit is reached |
| 17 | + * @param limit The number of elements to drop |
| 18 | + * @returns A new `AsyncIterator` |
| 19 | + */ |
| 20 | + drop(limit: number): AsyncIteratorObject<T>; |
| 21 | + |
| 22 | + /** |
| 23 | + * Check if every value generated by the iterator passes the `predicate` function. |
| 24 | + * @param predicate A function that tests each element of the iterator |
| 25 | + * @returns A promise that resolves to `true` if all elements pass the test, otherwise `false` |
| 26 | + */ |
| 27 | + every(predicate: (value: T, index: number) => boolean): Promise<boolean>; |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates a new `AsyncIterator` that contains only the elements that pass the `predicate` function. |
| 31 | + * @param predicate A function that tests each element of the iterator |
| 32 | + * @returns A new `AsyncIterator` |
| 33 | + */ |
| 34 | + filter(predicate: (value: T, index: number) => boolean): AsyncIteratorObject<T>; |
| 35 | + |
| 36 | + /** |
| 37 | + * Finds the first element in the iterator that satisfies the `predicate` function. |
| 38 | + * @param predicate A function that tests each element of the iterator |
| 39 | + * @returns A `Promise` that resolves to the found element, or `undefined` if no element satisfies the `predicate` |
| 40 | + */ |
| 41 | + find(predicate: (value: T, index: number) => boolean): Promise<T>; |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a new `AsyncIterator` by applying the `mapper` function to each element of the original iterator and flattening the result. |
| 45 | + * @param mapper A function that transforms each element of the iterator |
| 46 | + * @returns A new `AsyncIterator` |
| 47 | + */ |
| 48 | + flatMap(mapper: (value: T, index: number) => any): AsyncIteratorObject<any>; |
| 49 | + |
| 50 | + /** |
| 51 | + * Executes a provided function once for each element in the iterator. |
| 52 | + * @param callbackFn A function that is called for each element of the iterator |
| 53 | + * @returns A `Promise` that resolves when all elements have been processed |
| 54 | + */ |
| 55 | + forEach(callbackFn: (value: T, index: number) => void): Promise<void>; |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a new `AsyncIterator` by applying the `mapper` function to each element of the original iterator. |
| 59 | + * @param mapper A function that transforms each element of the iterator |
| 60 | + * @returns A new `AsyncIterator` |
| 61 | + */ |
| 62 | + map(mapper: (value: T, index: number) => any): AsyncIteratorObject<T>; |
| 63 | + |
| 64 | + /** |
| 65 | + * Reduces the elements of the iterator to a single value using the `reducer` function. |
| 66 | + * @param reducer A function that combines two elements of the iterator |
| 67 | + * @param initialValue An optional initial value to start the reduction |
| 68 | + * @returns A `Promise` that resolves to the reduced value |
| 69 | + */ |
| 70 | + reduce(reducer: (accumulator: any, value: T, index: number) => any, initialValue?: any): Promise<any>; |
| 71 | + |
| 72 | + /** |
| 73 | + * Checks if any value in the iterator matches a given `predicate` |
| 74 | + * @param predicate A function that tests each element of the iterator |
| 75 | + * @returns A `Promise` that resolves to `true` if any element passes the `predicate`, otherwise `false` |
| 76 | + */ |
| 77 | + some(predicate: (value: T, index: number) => boolean): Promise<boolean>; |
| 78 | + |
| 79 | + /** |
| 80 | + * Creates a new `AsyncIterator` that yields only the first `limit` elements from the original iterator. |
| 81 | + * @param limit The maximum number of elements to take |
| 82 | + * @returns A new `AsyncIterator` |
| 83 | + */ |
| 84 | + take(limit: number): AsyncIteratorObject<T>; |
| 85 | + |
| 86 | + /** |
| 87 | + * Collects all elements from the iterator into an array. |
| 88 | + * @returns A `Promise` that resolves to an array containing all elements from the iterator |
| 89 | + */ |
| 90 | + toArray(): Promise<T[]>; |
| 91 | +} |
| 92 | + |
| 93 | +interface Iterator<T> { |
| 94 | + /** |
| 95 | + * Creates an `AsyncIterator` from the current `Iterator` |
| 96 | + * @returns A new `AsyncIterator` instance |
| 97 | + */ |
| 98 | + toAsync(): AsyncIteratorObject<T>; |
| 99 | +} |
0 commit comments