@push-stream-std/push-merge (0.0.4)
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.4"@push-stream-std/push-merge": "0.0.4"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