UNPKG

26.6 kBTypeScriptView Raw
1/**
2 * Literally any `Iterable` (async or regular).
3 */
4export declare type AnyIterable<T> = Iterable<T> | AsyncIterable<T>;
5
6/**
7 * Batch objects from `iterable` into arrays of `size` length. The final array may be shorter than size if there is not enough items. Returns a sync iterator if the `iterable` is sync, otherwise an async iterator. Errors from the source `iterable` are immediately raised.
8
9 `size` can be between 1 and `Infinity`.
10
11 ```ts
12 import { batch } from 'streaming-iterables'
13 import { getPokemon } from 'iterable-pokedex'
14
15 // batch 10 pokemon while we process them
16 for await (const pokemons of batch(10, getPokemon())) {
17 console.log(pokemons) // 10 pokemon at a time!
18 }
19 ```
20 */
21export declare function batch(size: number): CurriedBatchResult;
22
23export declare function batch<T, M extends AnyIterable<T>>(size: number, iterable: M): UnwrapAnyIterableArray<M>;
24
25/**
26 * Like `batch` but flushes early if the `timeout` is reached. The batches may be shorter than size if there are not enough items. Returns a sync iterator if the `iterable` is sync, otherwise an async iterator. Errors from the source `iterable` are immediately raised.
27
28 `size` can be between 1 and `Infinity`.
29 `timeout` can be between 0 and `Infinity`.
30
31 ```ts
32 import { batchWithTimeout } from 'streaming-iterables'
33 import { getPokemon } from 'iterable-pokedex'
34
35 // batch 10 pokemon while we process them
36 for await (const pokemons of batchWithTimeout(10, 100, getPokemon())) {
37 console.log(pokemons) // Up to 10 pokemon at a time!
38 }
39 ```
40 */
41export declare function batchWithTimeout(size: number, timeout: number): CurriedBatchWithTimeoutResult;
42
43export declare function batchWithTimeout<T, M extends AnyIterable<T>>(size: number, timeout: number, iterable: M): UnwrapAnyIterableArray<M>;
44
45/**
46 * Buffer keeps a number of objects in reserve available for immediate reading. This is helpful with async iterators as it will pre-fetch results so you don't have to wait for them to load. For sync iterables it will pre-compute up to `size` values and keep them in reserve. The internal buffer will start to be filled once `.next()` is called for the first time and will continue to fill until the source `iterable` is exhausted or the buffer is full. Errors from the source `iterable` will be raised after all buffered values are yielded.
47
48 `size` can be between 0 and `Infinity`.
49
50 ```ts
51 import { buffer } from 'streaming-iterables'
52 import { getPokemon, trainMonster } from 'iterable-pokedex'
53
54 // load 10 monsters in the background while we process them one by one
55 for await (const monster of buffer(10, getPokemon())) {
56 await trainMonster(monster) // got to do some pokéwork
57 }
58 ```
59 */
60export declare function buffer(size: number): CurriedBufferResult;
61
62export declare function buffer<T, M extends AnyIterable<T>>(size: number, iterable: M): UnwrapAnyIterable<M>;
63
64/**
65 * Collect all the values from an iterable into an array. Returns an array if you pass it an iterable and a promise for an array if you pass it an async iterable. Errors from the source `iterable` are raised immediately.
66
67 ```ts
68 import { collect } from 'streaming-iterables'
69 import { getPokemon } from 'iterable-pokedex'
70
71 console.log(await collect(getPokemon()))
72 // [bulbasaur, ivysaur, venusaur, charmander, ...]
73 ```
74 */
75export declare function collect<T, M extends AnyIterable<T>>(iterable: M): UnwrapToPromiseOrAsyncIterable<M>;
76
77/**
78 * Combine multiple iterators into a single iterable. Reads each iterable completely one at a time. Returns a sync iterator if all `iterables` are sync, otherwise it returns an async iterable. Errors from the source `iterable` are raised immediately.
79
80 ```ts
81 import { concat } from 'streaming-iterables'
82 import { getPokemon } from 'iterable-pokedex'
83 import { getTransformers } from './util'
84
85 for await (const hero of concat(getPokemon(2), getTransformers(2))) {
86 console.log(hero)
87 }
88 // charmander
89 // bulbasaur <- end of pokemon
90 // megatron
91 // bumblebee <- end of transformers
92 ```
93 */
94export declare function concat<I extends Iterable<any>[]>(...iterables: I): Iterable<UnArrayAnyIterable<I>>;
95
96export declare function concat<I extends AnyIterable<any>[]>(...iterables: I): AsyncIterable<UnArrayAnyIterable<I>>;
97
98/**
99 * A promise that resolves after the function drains the iterable of all data. Useful for processing a pipeline of data. Errors from the source `iterable` are raised immediately.
100
101 ```ts
102 import { consume, map } from 'streaming-iterables'
103 import { getPokemon, trainMonster } from 'iterable-pokedex'
104
105 const train = map(trainMonster)
106 await consume(train(getPokemon())) // load all the pokemon and train them!
107 ```
108 */
109export declare function consume<T>(iterable: Iterable<T>): void;
110
111export declare function consume<T>(iterable: AsyncIterable<T>): Promise<void>;
112
113export declare type CurriedBatchResult = <T, M extends AnyIterable<T>>(curriedIterable: M) => UnwrapAnyIterableArray<M>;
114
115export declare type CurriedBatchWithTimeoutResult = <T, M extends AnyIterable<T>>(curriedIterable: M) => UnwrapAnyIterableArray<M>;
116
117export declare type CurriedBufferResult = <T, M extends AnyIterable<T>>(curriedIterable: M) => UnwrapAnyIterable<M>;
118
119declare type CurriedDropResult = <T, M extends AnyIterable<T>>(curriedIterable: M) => UnwrapAnyIterable<M>;
120
121export declare type CurriedTakeResult = <T, M extends AnyIterable<T>>(curriedIterable: M) => UnwrapAnyIterable<M>;
122
123export declare type CurriedTimeResult = <T, M extends AnyIterable<T>>(curriedIterable: M) => UnwrapAnyIterable<M>;
124
125/**
126 * Returns a new iterator that skips a specific number of items from `iterable`. When used with generators it advances the generator `count` items, when used with arrays it gets a new iterator and skips `count` items.
127
128 ```ts
129 import { pipeline, drop, collect } from 'streaming-iterables'
130 import { getPokemon } from 'iterable-pokedex'
131
132 const allButFirstFive = await collect(drop(5, getPokemon()))
133 // first five pokemon
134 ```
135 */
136export declare function drop(count: number): CurriedDropResult;
137
138export declare function drop<T, M extends AnyIterable<T>>(count: number, iterable: M): UnwrapAnyIterable<M>;
139
140/**
141 * Takes a `filterFunc` and a `iterable`, and returns a new async iterator of the same type containing the members of the given iterable which cause the `filterFunc` to return true.
142
143 ```ts
144 import { filter } from 'streaming-iterables'
145 import { getPokemon } from 'iterable-pokedex'
146
147 const filterWater = filter(pokemon => pokemon.types.include('Water'))
148
149 for await (const pokemon of filterWater(getPokemon())) {
150 console.log(pokemon)
151 }
152 // squirtle
153 // vaporeon
154 // magikarp
155 ```
156 */
157export declare function filter<T, S extends T>(filterFunc: (data: T) => data is S): <A extends T>(curriedIterable: AnyIterable<A>) => AsyncGenerator<S>;
158
159export declare function filter<T>(filterFunc: (data: T) => boolean | Promise<boolean>): <A>(curriedIterable: AnyIterable<A>) => AsyncGenerator<A>;
160
161export declare function filter<T, S extends T>(filterFunc: (data: T) => data is S, iterable: AnyIterable<T>): AsyncGenerator<S>;
162
163export declare function filter<T>(filterFunc: (data: T) => boolean | Promise<boolean>, iterable: AnyIterable<T>): AsyncGenerator<T>;
164
165/**
166 * Map `func` over the `iterable`, flatten the result and then ignore all null or undefined values. It's the transform function we've always needed. It's equivalent to;
167
168 ```ts
169 (func, iterable) => filter(i => i !== undefined && i !== null, flatten(map(func, iterable)))
170 ```
171
172 *note*: The return value for `func` is `FlatMapValue<B>`. Typescript doesn't have recursive types but you can nest iterables as deep as you like.
173
174 The ordering of the results is guaranteed. Errors from the source `iterable` are raised after all mapped values are yielded. Errors from `func` are raised after all previously mapped values are yielded.
175
176 ```ts
177 import { flatMap } from 'streaming-iterables'
178 import { getPokemon, lookupStats } from 'iterable-pokedex'
179
180 async function getDefeatedGyms(pokemon) {
181 if (pokemon.gymBattlesWon > 0) {
182 const stats = await lookupStats(pokemon)
183 return stats.gyms
184 }
185 }
186
187 for await (const gym of flatMap(getDefeatedGyms, getPokemon())) {
188 console.log(gym.name)
189 }
190 // "Pewter Gym"
191 // "Cerulean Gym"
192 // "Vermilion Gym"
193 ```
194 */
195export declare function flatMap<T, B>(func: (data: T) => FlatMapValue<B>): (iterable: AnyIterable<T>) => AsyncGenerator<B>;
196
197export declare function flatMap<T, B>(func: (data: T) => FlatMapValue<B>, iterable: AnyIterable<T>): AsyncGenerator<B>;
198
199/**
200 * A value, an array of that value, undefined, null or promises for any of them. Used in the `flatMap` and `flatTransform` functions as possible return values of the mapping function.
201 */
202export declare type FlatMapValue<B> = B | AnyIterable<B> | undefined | null | Promise<B | AnyIterable<B> | undefined | null>;
203
204/**
205 * Returns a new iterator by pulling every item out of `iterable` (and all its sub iterables) and yielding them depth-first. Checks for the iterable interfaces and iterates it if it exists. If the value is a string it is not iterated as that ends up in an infinite loop. Errors from the source `iterable` are raised immediately.
206
207 *note*: Typescript doesn't have recursive types but you can nest iterables as deep as you like.
208
209 ```ts
210 import { flatten } from 'streaming-iterables'
211
212 for await (const item of flatten([1, 2, [3, [4, 5], 6])) {
213 console.log(item)
214 }
215 // 1
216 // 2
217 // 3
218 // 4
219 // 5
220 // 6
221 ```
222 */
223export declare function flatten<B>(iterable: AnyIterable<B | AnyIterable<B>>): AsyncIterableIterator<B>;
224
225/**
226 * Map `func` over the `iterable`, flatten the result and then ignore all null or undefined values. Returned async iterables are flattened concurrently too. It's the transform function we've always wanted.
227
228 It's similar to;
229
230 ```ts
231 const filterEmpty = filter(i => i !== undefined && i !== null)
232 (concurrency, func, iterable) => filterEmpty(flatten(transform(concurrency, func, iterable)))
233 ```
234
235 *note*: The return value for `func` is `FlatMapValue<B>`. Typescript doesn't have recursive types but you can nest iterables as deep as you like. However only directly returned async iterables are processed concurrently. (Eg, if you use an async generator function as `func` it's output will be processed concurrently, but if it's nested inside other iterables it will be processed sequentially.)
236
237 Order is determined by when async operations resolve. And it will run up to `concurrency` async operations at once. This includes promises and async iterables returned from `func`. Errors from the source `iterable` are raised after all transformed values are yielded. Errors from `func` are raised after all previously transformed values are yielded.
238
239 `concurrency` can be between 1 and `Infinity`.
240
241 Promise Example;
242
243 ```ts
244 import { flatTransform } from 'streaming-iterables'
245 import { getPokemon, lookupStats } from 'iterable-pokedex'
246
247 async function getDefeatedGyms(pokemon) {
248 if (pokemon.gymBattlesWon > 0) {
249 const stats = await lookupStats(pokemon)
250 return stats.gyms
251 }
252 }
253
254 // lookup 10 stats at a time
255 for await (const gym of flatTransform(10, getDefeatedGyms, getPokemon())) {
256 console.log(gym.name)
257 }
258 // "Pewter Gym"
259 // "Cerulean Gym"
260 // "Vermilion Gym"
261 ```
262
263 Async Generator Example
264
265 ```ts
266 import { flatTransform } from 'streaming-iterables'
267 import { getPokemon } from 'iterable-pokedex'
268 import { findFriendsFB, findFriendsMySpace } from './util'
269
270
271 async function* findFriends (pokemon) {
272 yield await findFriendsFB(pokemon.name)
273 yield await findFriendsMySpace(pokemon.name)
274 }
275
276 for await (const pokemon of flatTransform(10, findFriends, getPokemon())) {
277 console.log(pokemon.name)
278 }
279 // Pikachu
280 // Meowth
281 // Ash - FB
282 // Jessie - FB
283 // Misty - MySpace
284 // James - MySpace
285 ```
286 */
287export declare function flatTransform<T, R>(concurrency: number): {
288 (func: (data: T) => FlatMapValue<R>, iterable: AnyIterable<T>): AsyncGenerator<R>;
289 (func: (data: T) => FlatMapValue<R>): (iterable: AnyIterable<T>) => AsyncGenerator<R>;
290};
291
292export declare function flatTransform<T, R>(concurrency: number, func: (data: T) => FlatMapValue<R>): (iterable: AnyIterable<T>) => AsyncGenerator<R>;
293
294export declare function flatTransform<T, R>(concurrency: number, func: (data: T) => FlatMapValue<R>, iterable: AnyIterable<T>): AsyncGenerator<R>;
295
296/**
297 * Wraps the stream in an async iterator or returns the stream if it already is an async iterator.
298
299 *note*: Since Node 10, streams already async iterators. This function may be used to ensure compatibility with older versions of Node.
300
301 ```ts
302 import { fromStream } from 'streaming-iterables'
303 import { createReadStream } from 'fs'
304
305 const pokeLog = fromStream(createReadStream('./pokedex-operating-system.log'))
306
307 for await (const pokeData of pokeLog) {
308 console.log(pokeData) // Buffer(...)
309 }
310 ```
311 * @deprecated This method is deprecated since, node 10 is out of LTS. It may be removed in an upcoming major release.
312 */
313export declare function fromStream<T>(stream: ReadableStreamish): AsyncIterable<T>;
314
315/**
316 * Get the iterator from any iterable or just return an iterator itself.
317 */
318export declare function getIterator<T>(iterable: Iterable<T> | Iterator<T>): Iterator<T>;
319
320export declare function getIterator<T>(iterable: AsyncIterable<T> | AsyncIterator<T>): AsyncIterator<T>;
321
322export declare function getIterator<T>(iterable: AsyncIterable<T> | Iterable<T>): AsyncIterator<T> | Iterator<T>;
323
324/**
325 * Any iterable or iterator.
326 */
327export declare type Iterableish<T> = Iterable<T> | Iterator<T> | AsyncIterable<T> | AsyncIterator<T>;
328
329/**
330 * Map a function or async function over all the values of an iterable. Errors from the source `iterable` and `func` are raised immediately.
331
332 ```ts
333 import { consume, map } from 'streaming-iterables'
334 import got from 'got'
335
336 const urls = ['https://http.cat/200', 'https://http.cat/201', 'https://http.cat/202']
337 const download = map(got)
338
339 // download one at a time
340 for await (page of download(urls)) {
341 console.log(page)
342 }
343 ```
344 */
345export declare function map<T, B>(func: (data: T) => B | Promise<B>): (iterable: AnyIterable<T>) => AsyncGenerator<B>;
346
347export declare function map<T, B>(func: (data: T) => B | Promise<B>, iterable: AnyIterable<T>): AsyncGenerator<B>;
348
349/**
350 * Combine multiple iterators into a single iterable. Reads one item off each iterable in order repeatedly until they are all exhausted. If you care less about order and want them faster see `parallelMerge()`.
351 */
352export declare function merge<I extends AnyIterable<any>[]>(...iterables: I): AsyncIterable<UnArrayAnyIterable<I>>;
353
354export declare function parallelFlatMap<T, R>(concurrency: number): {
355 (func: (data: T) => R | Promise<R>, iterable: AnyIterable<T>): AsyncGenerator<R>;
356 (func: (data: T) => R | Promise<R>): (iterable: AnyIterable<T>) => AsyncGenerator<R>;
357};
358
359export declare function parallelFlatMap<T, R>(concurrency: number, func: (data: T) => R | Promise<R>): (iterable: AnyIterable<T>) => AsyncGenerator<R>;
360
361export declare function parallelFlatMap<T, R>(concurrency: number, func: (data: T) => R | Promise<R>, iterable: AnyIterable<T>): AsyncGenerator<R>;
362
363export declare function parallelMap<T, R>(concurrency: number): {
364 (func: (data: T) => R | Promise<R>, iterable: AnyIterable<T>): AsyncIterableIterator<R>;
365 (func: (data: T) => R | Promise<R>): (iterable: AnyIterable<T>) => AsyncIterableIterator<R>;
366};
367
368export declare function parallelMap<T, R>(concurrency: number, func: (data: T) => R | Promise<R>): (iterable: AnyIterable<T>) => AsyncIterableIterator<R>;
369
370export declare function parallelMap<T, R>(concurrency: number, func: (data: T) => R | Promise<R>, iterable: AnyIterable<T>): AsyncIterableIterator<R>;
371
372/**
373 *Combine multiple iterators into a single iterable. Reads one item off of every iterable and yields them as they resolve. This is useful for pulling items out of a collection of iterables as soon as they're available. Errors `iterables` are raised immediately.
374
375 ```ts
376 import { parallelMerge } from 'streaming-iterables'
377 import { getPokemon, getTransformer } from 'iterable-pokedex'
378
379 // pokemon are much faster to load btw
380 const heros = parallelMerge(getPokemon(), getTransformer())
381 for await (const hero of heros) {
382 console.log(hero)
383 }
384 // charmander
385 // bulbasaur
386 // megatron
387 // pikachu
388 // eevee
389 // bumblebee
390 // jazz
391 ```
392 */
393export declare function parallelMerge<I extends AnyIterable<any>[]>(...iterables: I): AsyncIterable<UnArrayAnyIterable<I>>;
394
395/**
396 * Calls `firstFn` and then every function in `fns` with the result of the previous function. The final return is the result of the last function in `fns`.
397
398 ```ts
399 import { pipeline, map, collect } from 'streaming-iterables'
400 import { getPokemon } from 'iterable-pokedex'
401 const getName = map(pokemon => pokemon.name)
402
403 // equivalent to `await collect(getName(getPokemon()))`
404 await pipeline(getPokemon, getName, collect)
405 // charmander
406 // bulbasaur
407 // MissingNo.
408 ```
409 */
410export declare function pipeline<T0>(firstFn: () => T0): T0;
411
412export declare function pipeline<T0, T1>(a0: () => T0, a1: (a: T0) => T1): T1;
413
414export declare function pipeline<T0, T1, T2>(a0: () => T0, a1: (a: T0) => T1, a2: (a: T1) => T2): T2;
415
416export declare function pipeline<T0, T1, T2, T3>(a0: () => T0, a1: (a: T0) => T1, a2: (a: T1) => T2, a3: (a: T2) => T3): T3;
417
418export declare function pipeline<T0, T1, T2, T3, T4>(a0: () => T0, a1: (a: T0) => T1, a2: (a: T1) => T2, a3: (a: T2) => T3, a4: (a: T3) => T4): T4;
419
420export declare function pipeline<T0, T1, T2, T3, T4, T5>(a0: () => T0, a1: (a: T0) => T1, a2: (a: T1) => T2, a3: (a: T2) => T3, a4: (a: T3) => T4, a5: (a: T4) => T5): T5;
421
422export declare function pipeline<T0, T1, T2, T3, T4, T5, T6>(a0: () => T0, a1: (a: T0) => T1, a2: (a: T1) => T2, a3: (a: T2) => T3, a4: (a: T3) => T4, a5: (a: T4) => T5, a6: (a: T5) => T6): T6;
423
424export declare function pipeline<T0, T1, T2, T3, T4, T5, T6, T7>(a0: () => T0, a1: (a: T0) => T1, a2: (a: T1) => T2, a3: (a: T2) => T3, a4: (a: T3) => T4, a5: (a: T4) => T5, a6: (a: T5) => T6, a7: (a: T6) => T7): T7;
425
426export declare function pipeline<T0, T1, T2, T3, T4, T5, T6, T7, T8>(a0: () => T0, a1: (a: T0) => T1, a2: (a: T1) => T2, a3: (a: T2) => T3, a4: (a: T3) => T4, a5: (a: T4) => T5, a6: (a: T5) => T6, a7: (a: T6) => T7, a8: (a: T7) => T8): T8;
427
428export declare function pipeline<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(a0: () => T0, a1: (a: T0) => T1, a2: (a: T1) => T2, a3: (a: T2) => T3, a4: (a: T3) => T4, a5: (a: T4) => T5, a6: (a: T5) => T6, a7: (a: T6) => T7, a8: (a: T7) => T8, a9: (a: T8) => T9): T9;
429
430export declare interface ReadableStreamish {
431 once: any;
432 read: any;
433}
434
435/**
436 * An async function that takes a reducer function, an initial value and an iterable.
437
438 Reduces an iterable to a value which is the accumulated result of running each value from the iterable thru `func`, where each successive invocation is supplied the return value of the previous. Errors are immediate raised.
439 */
440export declare function reduce<T, B>(func: (acc: B, value: T) => B): {
441 (start: B): (iterable: AnyIterable<T>) => Promise<B>;
442 (start: B, iterable: AnyIterable<T>): Promise<B>;
443};
444
445export declare function reduce<T, B>(func: (acc: B, value: T) => B, start: B): (iterable: AnyIterable<T>) => Promise<B>;
446
447export declare function reduce<T, B>(func: (acc: B, value: T) => B, start: B, iterable: AnyIterable<T>): Promise<B>;
448
449/**
450 * Returns a new iterator that reads a specific number of items from `iterable`. When used with generators it advances the generator, when used with arrays it gets a new iterator and starts from the beginning.
451
452 ```ts
453 import { pipeline, take, collect } from 'streaming-iterables'
454 import { getPokemon } from 'iterable-pokedex'
455
456 const topFive = await collect(take(5, getPokemon()))
457 // first five pokemon
458 ```
459 */
460export declare function take(count: number): CurriedTakeResult;
461
462export declare function take<T, M extends AnyIterable<T>>(count: number, iterable: M): UnwrapAnyIterable<M>;
463
464/**
465 * Returns a new iterator that yields the data it consumes, passing the data through to a function. If you provide an async function, the iterator will wait for the promise to resolve before yielding the value. This is useful for logging, or processing information and passing it along.
466 */
467export declare function tap<T>(func: (data: T) => any): (iterable: AnyIterable<T>) => AsyncGenerator<T>;
468
469export declare function tap<T>(func: (data: T) => any, iterable: AnyIterable<T>): AsyncGenerator<T>;
470
471/**
472 * Throttles `iterable` at a rate of `limit` per `interval` without discarding data. Useful for throttling rate limited APIs.
473
474 `limit` can be greater than 0 but less than `Infinity`.
475 `interval` can be greater than or equal to 0 but less than `Infinity`.
476
477 ```ts
478 import { throttle } from 'streaming-iterables'
479 import { getPokemon, trainMonster } from 'iterable-pokedex'
480
481 // load monsters at a maximum rate of 1 per second
482 for await (const monster of throttle(1, 1000, getPokemon())) {
483 await trainMonster(monster)
484 }
485 ```
486 */
487export declare function throttle<T>(limit: number, interval: number): (iterable: AnyIterable<T>) => AsyncGenerator<T>;
488
489export declare function throttle<T>(limit: number, interval: number, iterable: AnyIterable<T>): AsyncGenerator<T>;
490
491/**
492 * Returns a new iterator that yields the data it consumes and calls the `progress` and `total` callbacks with the [`hrtime`](https://nodejs.org/api/process.html#process_process_hrtime_time) it took for `iterable` to provide a value when `.next()` was called on it. That is to say, the time returned is the time this iterator spent waiting for data, not the time it took to finish being read. The `hrtime` tuple looks like `[seconds, nanoseconds]`.
493
494 ```ts
495 import { consume, transform, time } from 'streaming-iterables'
496 import got from 'got'
497
498 const urls = ['https://http.cat/200', 'https://http.cat/201', 'https://http.cat/202']
499 const download = transform(1000, got)
500 const timer = time({
501 total: total => console.log(`Spent ${total[0]} seconds and ${total[1]}ns downloading cats`),
502 })
503 // download all of these at the same time
504 for await (page of timer(download(urls))) {
505 console.log(page)
506 }
507 ```
508 */
509export declare function time(config?: TimeConfig): CurriedTimeResult;
510
511export declare function time<T, M extends AnyIterable<T>>(config: TimeConfig, iterable: M): UnwrapAnyIterable<M>;
512
513export declare interface TimeConfig {
514 progress?: (delta: [number, number], total: [number, number]) => any;
515 total?: (time: [number, number]) => any;
516}
517
518/**
519 * Map a function or async function over all the values of an iterable. Order is determined by when `func` resolves. And it will run up to `concurrency` async `func` operations at once. If you care about order see [`parallelMap()`](#parallelmap). Errors from the source `iterable` are raised after all transformed values are yielded. Errors from `func` are raised after all previously transformed values are yielded.
520
521 `concurrency` can be between 1 and `Infinity`.
522
523 ```ts
524 import { consume, transform } from 'streaming-iterables'
525 import got from 'got'
526
527 const urls = ['https://http.cat/200', 'https://http.cat/201', 'https://http.cat/202']
528 const download = transform(1000, got)
529
530 // download all of these at the same time
531 for await (page of download(urls)) {
532 console.log(page)
533 }
534 ```
535 */
536export declare function transform<T, R>(concurrency: number): {
537 (func: (data: T) => R | Promise<R>, iterable: AnyIterable<T>): AsyncIterableIterator<R>;
538 (func: (data: T) => R | Promise<R>): (iterable: AnyIterable<T>) => AsyncIterableIterator<R>;
539};
540
541export declare function transform<T, R>(concurrency: number, func: (data: T) => R | Promise<R>): (iterable: AnyIterable<T>) => AsyncIterableIterator<R>;
542
543export declare function transform<T, R>(concurrency: number, func: (data: T) => R | Promise<R>, iterable: AnyIterable<T>): AsyncIterableIterator<R>;
544
545export declare type UnArrayAnyIterable<A extends AnyIterable<any>[]> = A extends AnyIterable<infer T>[] ? T : never;
546
547export declare type UnwrapAnyIterable<M extends AnyIterable<any>> = M extends Iterable<infer T> ? Iterable<T> : M extends AsyncIterable<infer B> ? AsyncIterable<B> : never;
548
549export declare type UnwrapAnyIterableArray<M extends AnyIterable<any>> = M extends Iterable<infer T> ? Generator<T[]> : M extends AsyncIterable<infer B> ? AsyncGenerator<B[]> : never;
550
551export declare type UnwrapToPromiseOrAsyncIterable<M extends AnyIterable<any>> = M extends Iterable<infer T> ? T[] : M extends AsyncIterable<infer B> ? Promise<B[]> : never;
552
553export declare interface WritableStreamish {
554 once: any;
555 write: any;
556 removeListener: any;
557}
558
559/**
560 * Writes the `iterable` to the stream respecting the stream back pressure. Resolves when the iterable is exhausted, rejects if the stream errors during calls to `write()` or if there are `error` events during the write.
561
562 As it is when working with streams there are a few caveats;
563
564 - It is possible for the stream to error after `writeToStream()` has finished writing due to internal buffering and other concerns, so always handle errors on the stream as well.
565 - `writeToStream()` doesn't close the stream like `stream.pipe()` might. This is done so you can write to the stream multiple times. You can call `stream.write(null)` or any stream specific end function if you are done with the stream.
566
567 ```ts
568 import { pipeline, map, writeToStream } from 'streaming-iterables'
569 import { getPokemon } from 'iterable-pokedex'
570 import { createWriteStream } from 'fs'
571
572 const file = createWriteStream('pokemon.ndjson')
573 const serialize = map(pokemon => `${JSON.stringify(pokemon)}\n`)
574 await pipeline(getPokemon, serialize, writeToStream(file))
575 file.end() // close the stream
576 // now all the pokemon are written to the file!
577 ```
578 */
579export declare function writeToStream(stream: WritableStreamish): (iterable: AnyIterable<any>) => Promise<void>;
580
581export declare function writeToStream(stream: WritableStreamish, iterable: AnyIterable<any>): Promise<void>;
582
583export { }