Giter Site home page Giter Site logo

node-dogh's Introduction

dogh

NPM

Build Status

Overview

Read text streams line by line with a promisified async/await friendly interface.

Install

$ npm install dogh --save

Usage

import LineReader from 'dogh';

async function main() {
    // Create a LineReader on a readable stream:
    const lr = new LineReader(process.stdin);

    // Read a line of text:
    let line = await lr.readLine();

    // Read all the lines of text:
    while ((line = await lr.readLine()) != null) {
        // Do stuff with line
    }
}

Examples

See examples.

API

readLine(): Promise<string>

Read a line of text from the stream. Returns a Promise that resolves to the line of text with the newline stripped. Returns null once the end of the stream is reached. Calling this function again prior to a previous invocation completing will reject with a concurrency error.

Internals

An event listener is registered for the data event of the stream. Chunks of data, that could be partial lines or multiple lines, are added to an internal list of lines and buffer of remaining partial line text. The stream is then paused to prevent the internal buffer from growing.

When readLine() is called, it first checks to see if the internal list of lines has a buffered line. If so, a line is removed from that list. If not, the stream is resumed so that it can continue to buffer lines. Once at least one line is buffered the stream is again paused to prevent the buffer from growing.

In practice this means you can read a file one line at a time without worrying about blowing up your memory usage.

Dependencies

None!

Building and Testing

To build the module run:

$ make

License

ISC. See the file LICENSE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.