@push-stream-std/push-group-by (0.0.7)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-group-by@0.0.7"@push-stream-std/push-group-by": "0.0.7"About this package
@push-stream-std/push-group-by
Split stream into multiple named sub-streams by key.
Published as @push-stream-std/push-group-by.
Synopsis
import { groupBy } from '@push-stream-std/push-group-by';
import { values } from '@push-stream-std/push-pushable';
import { collect } from '@push-stream-std/push-concat';
const byType = groupBy(event => event.type);
values([
{ type: 'log', msg: 'started' },
{ type: 'log', msg: 'running' },
{ type: 'error', msg: 'failed' }
]).pipe(byType);
collect({ stream: byType, result: false }, (err, groups) => {
for (const { key, stream } of groups) {
collect({ stream, result: false }, (err, items) => {
console.log(key, items); // log [{type:'log',msg:'started'},{type:'log',msg:'running'}]
// error [{type:'error',msg:'failed'}]
});
}
});
Install
npm config set @push-stream-std:registry https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/
npm config set -- //hub.kl1.tenere.ai/api/packages/push-stream-std/npm/:_authToken "$PACKAGE_TOKEN"
npm install @push-stream-std/push-group-by
Why
Routes incoming values into named sub-streams based on a key function. For each unique key, emits a { key, stream } object to the downstream sink. The stream is a push-stream source that receives all values matching that key. Values for the same key arriving non-consecutively are correctly routed to the same sub-stream. Uses O(1) amortized buffer operations via head pointer pattern.
API
groupBy(keyFn)
keyFn-function(value)- Returns a key (string or value) used to group incoming values.
Returns a push-stream through stream. Pipe a source into it. Downstream receives { key, stream } objects — one per unique key. Each stream is a push-stream source that can be piped to process values for that group. When the upstream source ends, all group sub-streams are ended as well.
Tests
npm test
Tests cover grouping by key, non-consecutive interleaved grouping (same key values arriving at different times), single group, and empty stream.
Requirements
Node.js 22 or later. ESM only.
Dependencies
Dependencies
| ID | Version |
|---|---|
| @push-stream-std/push-stream-base | * |