UNPKG

batchjs

Version:

Batch processing framework for NodeJS

77 lines (76 loc) 2.43 kB
import { InternalBufferDuplex } from "../interfaces/_index"; /** * @class * Class that allows you to group data in a stream. * @extends InternalBufferDuplex * @template T * @example * ```typescript * const stream:GroupByStream<string> = new GroupByStream({ * objectMode: true, * groupBy: (chunk: string) => chunk.split("").at(0) ?? "", * }); * * stream.write("DATA1"); //group : D * stream.write("DATA2"); //group : D * stream.write("data3"); //group : d * stream.end(); * * stream.on("data", (chunk: Array<string>) => { * console.log(``Pushed chunk: ${chunk}```); * }); * ``` * ```shell * >> Pushed chunk: ["DATA1", "DATA2"] * >> Pushed chunk: ["data3"] * ``` */ export class GroupByStream extends InternalBufferDuplex { groups = new Map(); groupBy; /** * @constructor * @param {GroupByStreamOptions<T>} options - The options for the GroupBy. * @param [options.groupBy] {Function} - The function used to get the grouping key from the chunk. */ constructor(options) { super(options); this.groupBy = options.groupBy; } /** * Writes a chunk of data to the stream, groups it according to a specified function, * and executes the callback. * * @param {T} chunk - The data chunk to write to the stream. * @param {BufferEncoding} encoding - The encoding of the data. * @param {TransformCallback} callback - The callback function to be executed after writing the data. * @return {void} */ _write(chunk, encoding, callback) { this._groupBy(chunk); callback(); } /** * Finalize the stream by draining the buffer and pushing any remaining chunks to the stream. * * @override * @param {TransformCallback} callback - The callback to be called when the stream is finalized. * @return {void} */ _final(callback) { this.buffer = Array.from(this.groups.values()); super._final(callback); } /** * Groups a chunk of data based on the provided groupBy function and stores it in the buffer. * * @param {T} chunk - The data chunk to be grouped. * @return {void} This function does not return anything. */ _groupBy(chunk) { const groupKey = this.groupBy(chunk); const group = this.groups.get(groupKey) ?? []; group.push(chunk); this.groups.set(groupKey, group); } }