iterator

May 14, 2020 · View on GitHub

version Maintenance MIT dep size Known Vulnerabilities Build Status

SlimIO Iterator utilities designed to manage and help developer to interact with iterators to release lazy code.

Requirements

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @slimio/iterator
# or
$ yarn add @slimio/iterator

Usage example

const { fromStream, tryOn, whileOn, compose } = require("@slimio/iterator");
const { createReadStream } = require("fs");

function* splitLines(target) {
    let previous = "";
    try {
        while (true) {
            previous += yield;
            let eolIndex;
            while ((eolIndex = previous.indexOf("\n")) >= 0) {
                target.next(previous.slice(0, eolIndex));
                previous = previous.slice(eolIndex + 1);
            }
        }
    }
    finally {
        if (previous.length > 0) {
            target.next(previous);
        }
        target.return();
    }
}

function* numberLines(next) {
    for (let lineCount = 0; ;lineCount++) {
        next(`${lineCount}: ${yield}`);
    }
}

const fileToRead = process.argv[2];
fromStream(
    createReadStream(fileToRead), compose(splitLines, tryOn(numberLines), whileOn(console.log))
).then(() => console.log("done")).catch(console.error);

API

compose(...generatorFuncs: GeneratorFunction[]): Generator
tryOn(generatorFunc: GeneratorFunction): GeneratorFunction
whileOn(fn: Function): GeneratorFunction
fromStream(stream: Stream.Readable, target: Generator): Promise< void >
mergeAsyncIterator< T >(iterator: AsyncIterator< T >): T[];

Dependencies

NameRefactoringSecurity RiskUsage
@slimio/isMinorLowType checker
is-stream⚠️MajorMediumStream type checker

License

MIT