@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
24 lines (22 loc) • 472 B
text/typescript
import { map } from '@johngw/stream/transformers/map'
/**
* Adds a counter representing the amount of chunks
* received thus far.
*
* @group Transformers
* @example
* ```
* --a----------------------b-------|
*
* withCounter()
*
* --{chunk:'a',counter:0}--{chunk:'b',counter:1}-|
* ```
*/
export function withCounter<T>(start = 0) {
let counter = start
return map<T, { chunk: T; counter: number }>((chunk) => ({
chunk,
counter: counter++,
}))
}