push-stream-std

@push-stream-std/push-merge (0.0.5)

Published 2026-07-20 18:59: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-merge@0.0.5
"@push-stream-std/push-merge": "0.0.5"

About this package

push-merge

Merge multiple pre-sorted streams.

Published as @push-stream-std/push-merge.

Synopsis

import { merge } from '@push-stream-std/push-merge';
import { values } from '@push-stream-std/push-values';
import { collect } from '@push-stream-std/push-collect';

const a = values([1, 4, 7]);
const b = values([2, 5, 8]);
const c = values([3, 6, 9]);

merge([a, b, c])
  .pipe(collect((err, results) => {
    console.log(results); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
  }));

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-merge

Description

Performs a k-way merge of multiple pre-sorted push-stream sources into a single sorted output stream. At each emission step, the item with the smallest value (according to the comparator) across all sources is emitted. This is the standard algorithm for merging sorted sequences and is commonly used in external sorting, log merging, and time-series aggregation. Sources are buffered using O(1) amortized head pointers. Passing a single source or an empty array returns immediately (the source itself or an empty stream).

API

merge(sources, comparator)

Creates a push-stream source that merges multiple sorted sources.

Parameters:

  • sources (Array | object) — An array of push-stream source objects (each pre-sorted) or a single source. An empty array produces an empty stream.
  • comparator (function, optional) — A comparison function (a, b) returning <0, 0, or >0. Defaults to JavaScript's default comparison (lexicographic for strings, numeric for numbers).

Returns: A push-stream source.

Tests

npm test

Tests cover merging of equal-length sorted arrays, unequal-length sources, single source passthrough, empty array, custom comparators, and proper end propagation.

Requirements

Node.js 18 or later. ESM only.

License

MIT

Keywords

push-stream stream merge sorted k-way-merge
Details
npm
2026-07-20 18:59:52 +00:00
0
SEE LICENSE IN LICENSE
4.6 KiB
Assets (1)
Versions (5) View all
0.0.7 2026-07-21
0.0.6 2026-07-20
0.0.5 2026-07-20
0.0.4 2026-07-19
0.0.3 2026-07-19