push-stream-std

@push-stream-std/push-unfold (0.0.9)

Published 2026-07-21 15:53:41 +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-unfold@0.0.9
"@push-stream-std/push-unfold": "0.0.9"

About this package

@push-stream-std/push-unfold

Generate a push-stream source by repeated function application from a seed value.

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

Synopsis

import unfold from '@push-stream-std/push-unfold';

// Synchronous: unfolder receives current seed, returns { value, seed } or null to end
const naturals = unfold((n) => {
  if (n > 5) return null;
  return { value: n, seed: n + 1 };
}, 1);

const sink = {
  paused: false,
  write(v) { console.log(v); },
  end() { console.log('done'); }
};

naturals.pipe(sink);
// Output: 1, 2, 3, 4, 5, done

// Asynchronous: unfolder receives (seed, callback)
const delayed = unfold((n, cb) => {
  if (n > 3) return cb(null, null);
  setTimeout(() => cb(null, { value: n, seed: n + 1 }), 100);
}, 1);

delayed.pipe(sink);
// Output: 1, 2, 3, done

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

Why

A push-stream source that lazily generates a sequence by applying an "unfolder" function to a seed value. Each call returns either a { value, seed } object (to emit a value and update the seed) or null/undefined to end the stream. The async form (detected when unfolder.length === 2) accepts a callback (err, result) for callback-based asynchronous generation. Sync unfolder errors and async errors are propagated to the sink via end(err).

API

unfold(unfolder, seed)

Creates an unfold source stream.

  • unfolder Function – A function that receives the current seed and either returns a result synchronously or (if unfolder.length === 2) calls a callback (err, result). The result must be either:
    • { value: any, seed: any } – Emit value and continue with the new seed.
    • null or undefined – End the stream.
  • seed any – The initial seed value.
  • Returns A source stream with pipe(), resume(), end(), abort(), paused, and ended.

Tests

npm test

Tests cover sync generation from seed, sync termination via null return, sync error propagation, async generation via callback, async termination, and async error propagation.

Requirements

Node.js 22 or later. ESM only.

Keywords

push-stream stream unfold generate
Details
npm
2026-07-21 15:53:41 +00:00
0
SEE LICENSE IN LICENSE
latest
3.6 KiB
Assets (1)
Versions (6) View all
0.0.9 2026-07-21
0.0.8 2026-07-21
0.0.7 2026-07-20
0.0.6 2026-07-20
0.0.5 2026-07-19