push-stream-std

@push-stream-std/push-group-by (0.0.8)

Published 2026-07-21 03:58:52 +00:00 by Dvorak

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.8
"@push-stream-std/push-group-by": "0.0.8"

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 *

Keywords

push-stream stream group key route
Details
npm
2026-07-21 03:58:52 +00:00
0
SEE LICENSE IN LICENSE
latest
4.2 KiB
Assets (1)
Versions (5) View all
0.0.8 2026-07-21
0.0.7 2026-07-20
0.0.6 2026-07-20
0.0.5 2026-07-19
0.0.3 2026-07-19