Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Read a Text Stream Line by Line with lazylines for Node.js

DZone's Guide to

Read a Text Stream Line by Line with lazylines for Node.js

· Web Dev Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

The new Node.js module “lazylines” allows you to read a text stream, one line at a time, lazily.

 

Examples

The following code reads input from stdin and prepends a number to each line.

    var ll = require("lazylines.js");

    process.stdin.resume();
    var inp = new ll.LineReadStream(process.stdin);
    var count = 1;
    inp.on("line", function (line) {
        console.log(count+": "+ll.chomp(line));
        count++;
    });

There are more examples in the demo/ directory. lazylines is very handy for shell programming. For example, you can implement a script that lets you do the following. 

    # List jpg files, oldest first (-t -r), then wrap
    ls -1 -t -r *.jpg | addlinecounts.js 'mv "{line}" {padi}.jpg' | bash

The above executes commands such as 

    mv "new.jpg" 001.jpg
    mv "newer.jpg" 002.jpg
    mv "newest.jpg" 003.jpg

Installation

    npm install lazylines

Related post

 

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:

Published at DZone with permission of Axel Rauschmayer, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}