|
| 1 | +# @simple-libs/stream-utils |
| 2 | + |
| 3 | +[![ESM-only package][package]][package-url] |
| 4 | +[![NPM version][npm]][npm-url] |
| 5 | +[![Node version][node]][node-url] |
| 6 | +[![Dependencies status][deps]][deps-url] |
| 7 | +[![Install size][size]][size-url] |
| 8 | +[![Build status][build]][build-url] |
| 9 | +[![Coverage status][coverage]][coverage-url] |
| 10 | + |
| 11 | +[package]: https://img.shields.io/badge/package-ESM--only-ffe536.svg |
| 12 | +[package-url]: https://nodejs.org/api/esm.html |
| 13 | + |
| 14 | +[npm]: https://img.shields.io/npm/v/@simple-libs/stream-utils.svg |
| 15 | +[npm-url]: https://www.npmjs.com/package/@simple-libs/stream-utils |
| 16 | + |
| 17 | +[node]: https://img.shields.io/node/v/@simple-libs/stream-utils.svg |
| 18 | +[node-url]: https://nodejs.org |
| 19 | + |
| 20 | +[deps]: https://img.shields.io/librariesio/release/npm/@simple-libs/stream-utils |
| 21 | +[deps-url]: https://libraries.io/npm/@simple-libs%2Fstream-utils/tree |
| 22 | + |
| 23 | +[size]: https://packagephobia.com/badge?p=@simple-libs/stream-utils |
| 24 | +[size-url]: https://packagephobia.com/result?p=@simple-libs/stream-utils |
| 25 | + |
| 26 | +[build]: https://img.shields.io/github/actions/workflow/status/TrigenSoftware/simple-libs/tests.yml?branch=main |
| 27 | +[build-url]: https://github.com/TrigenSoftware/simple-libs/actions |
| 28 | + |
| 29 | +[coverage]: https://img.shields.io/codecov/c/github/TrigenSoftware/simple-libs.svg?flag=@simple-libs/stream-utils |
| 30 | +[coverage-url]: https://app.codecov.io/gh/TrigenSoftware/simple-libs/tree/main/packages%2Fstream-utils |
| 31 | + |
| 32 | +A small set of utilities for streams. |
| 33 | + |
| 34 | +## Install |
| 35 | + |
| 36 | +```bash |
| 37 | +# pnpm |
| 38 | +pnpm add @simple-libs/stream-utils |
| 39 | +# yarn |
| 40 | +yarn add @simple-libs/stream-utils |
| 41 | +# npm |
| 42 | +npm i @simple-libs/stream-utils |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +```ts |
| 48 | +import { |
| 49 | + toArray, |
| 50 | + concatBufferStream, |
| 51 | + concatStringStream, |
| 52 | + firstFromStream, |
| 53 | + mergeReadables |
| 54 | +} from '@simple-libs/stream-utils' |
| 55 | + |
| 56 | +// Convert a readable stream to an array |
| 57 | +await toArray(Readable.from(['foo', 'bar', 'baz'])) |
| 58 | +// Returns ['foo', 'bar', 'baz'] |
| 59 | + |
| 60 | +// Concatenate a stream of buffers into a single buffer |
| 61 | +await concatBufferStream(Readable.from([Buffer.from('foo'), Buffer.from('bar')])) |
| 62 | +// Returns <Buffer 66 6f 6f 62 61 72> |
| 63 | + |
| 64 | +// Concatenate a stream of strings into a single string |
| 65 | +await concatStringStream(Readable.from(['foo', 'bar'])) |
| 66 | +// Returns 'foobar' |
| 67 | + |
| 68 | +// Get the first value from a stream |
| 69 | +await firstFromStream(Readable.from(['foo', 'bar'])) |
| 70 | +// Returns 'foo' |
| 71 | + |
| 72 | +// Merges multiple Readable streams into a single Readable stream. |
| 73 | +// Each chunk will be an object containing the source stream name and the chunk data. |
| 74 | +await mergeReadables({ |
| 75 | + foo: Readable.from(['foo1', 'foo2']), |
| 76 | + bar: Readable.from(['bar1', 'bar2']) |
| 77 | +}) |
| 78 | +// Returns [{ source: 'foo', chunk: 'foo1' }, { source: 'foo', chunk: 'foo2' }, { source: 'bar', chunk: 'bar1' }, { source: 'bar', chunk: 'bar2' }] |
| 79 | +``` |
0 commit comments