-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rename
transformBuffer
to transformChunk
- Loading branch information
1 parent
e8fe43a
commit 69e6445
Showing
5 changed files
with
55 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Transform } from 'node:stream' | ||
import { TransformOptions, TransformTyped } from '../stream.model' | ||
|
||
export interface TransformChunkOptions extends TransformOptions { | ||
/** | ||
* How many items to include in each chunk. | ||
* Last chunk will contain the remaining items, possibly less than chunkSize. | ||
*/ | ||
chunkSize: number | ||
} | ||
|
||
/** | ||
* Similar to RxJS bufferCount(), | ||
* allows to "chunk" the input stream into chunks of `opt.chunkSize` size. | ||
* Last chunk will contain the remaining items, possibly less than chunkSize. | ||
*/ | ||
export function transformChunk<IN = any>(opt: TransformChunkOptions): TransformTyped<IN, IN[]> { | ||
const { chunkSize } = opt | ||
|
||
let buf: IN[] = [] | ||
|
||
return new Transform({ | ||
objectMode: true, | ||
...opt, | ||
transform(chunk, _, cb) { | ||
buf.push(chunk) | ||
|
||
if (buf.length >= chunkSize) { | ||
cb(null, buf) | ||
buf = [] | ||
} else { | ||
cb() | ||
} | ||
}, | ||
final(this: Transform, cb) { | ||
if (buf.length) { | ||
this.push(buf) | ||
buf = [] | ||
} | ||
|
||
cb() | ||
}, | ||
}) | ||
} |