@kogs/utils

@kogs/util is a Node.js package that provides a collection of utility functions.
Installation
npm install @kogs/utils
Usage
import { someFunction } from '@kogs/utils';
import utils from '@kogs/utils';
API
collectFiles - Collect all files in a directory recursively.
arrayToStream - Convert an array of values to a readable stream.
streamToArray - Convert a readable stream to an array of values.
streamToBuffer - Convert a readable stream to a Buffer.
filterStream - Create a transform stream that filters stream data.
mergeStreams - Merge multiple readable streams into a single stream.
collectFiles
collectFiles(dir: string, filter?: FileFilter): Promise<string[]>
type FileFilter = (entryPath: string) => boolean;
This method accepts a directory path and returns a promise that resolves with an array of file paths recursively contained within the directory.
const files = await collectFiles('/path/to/dir');
If filter is defined, it will be used to filter the files returned by the method. The filter function will be passed the combined path of the directory and file name, and should return true if the file should be included in the result.
const files = await collectFiles('/path/to/dir', e => e.endsWith('.txt'));
arrayToStream
arrayToStream(input: Array<ReadableChunk>, objectMode: boolean = true): stream.Readable
type ReadableChunk = string | Buffer | Uint8Array | null;
This method accepts an array of values and returns a readable stream that emits each value in the array.
If objectMode is not defined, it will default to true if the first element in the input array is a non-null object, otherwise it will default to false. See the Node.js documentation for more information on object mode.
import { arrayToStream } from '@kogs/utils';
const stream = arrayToStream(['foo', 'bar', 'baz']);
streamToArray
streamToArray(input: stream.Readable): Promise<Array<ReadableChunk>>
type ReadableChunk = string | Buffer | Uint8Array | null;
This method accepts a readable stream and returns a promise that resolves with an array of values emitted by the stream.
import { streamToArray } from '@kogs/utils';
const stream = ...;
const result = await streamToArray(stream);
streamToBuffer
streamToBuffer(input: stream.Readable): Promise<Buffer>
This method accepts a readable stream and returns a promise that resolves with a Buffer containing the data emitted by the stream.
import { streamToBuffer } from '@kogs/utils';
const stream = ...;
const result = await streamToBuffer(stream);
filterStream
filterStream(fn: StreamFilter, objectMode: boolean = true): stream.Transform
type StreamFilter = (chunk: ReadableChunk) => Promise<boolean>;
This method accepts a filter function and returns a transform stream that filters the data emitted by a stream.
import { filterStream } from '@kogs/utils';
const stream = ...;
const filtered = stream.pipe(filterStream(chunk => chunk !== 'bar'));
mergeStreams
mergeStreams(...streams: Array<stream.Readable>): Promise<stream.PassThrough>
This method accepts a variable number of readable streams and returns a promise that resolves with a PassThrough stream that merges the data emitted by the input streams.
See the Node.js documentation for more information on PassThrough streams.
import { mergeStreams } from '@kogs/utils';
const stream1 = ...;
const stream2 = ...;
const merged = await mergeStreams(stream1, stream2);
What is @kogs?
@kogs is a collection of packages that I've written to consolidate the code I often reuse across my projects with the following goals in mind:
- Consistent API.
- Minimal dependencies.
- Full TypeScript definitions.
- Avoid feature creep.
- ES6+ syntax.
All of the packages in the @kogs collection can be found on npm under the @kogs scope.
Contributing / Feedback / Issues
Feedback, bug reports and contributions are welcome. Please use the GitHub issue tracker and follow the guidelines found in the CONTRIBUTING file.
License
The code in this repository is licensed under the ISC license. See the LICENSE file for more information.