Over a million developers have joined DZone.
Platinum Partner

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

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix.  Discover how IT departments looking for ways to keep up with demand for business apps has caused a new breed of developers to surface - the Rapid Application Developer.

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



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

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

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

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


    npm install lazylines

Related post


The Web Dev Zone is brought to you in partnership with Mendix.  Learn more about The Essentials of Digital Innovation and how it needs to be at the heart of every organization.


Published at DZone with permission of Axel Rauschmayer , DZone MVB .

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}