Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including stream-eachline with all npm packages installed. Try it out:

var streamEachline = require("stream-eachline")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

stream-eachline v0.1.0

Like readline or split, but behaves more like ruby's .each_with_index method when streams lack trailing newlines.

stream-eachline Build Status

Like readline or split, but behaves more like ruby's .each_with_index method when streams lack trailing newlines.

Getting Started

Install the module with: npm install stream-eachline

var eachline = require('stream-eachline').eachline;

// Modify each line in process.stdin input stream (while streaming)
// and when done log an array of modified lines.
eachline(process.stdin, function(line, index) {
  return '[' + index + '] ' + line.toUpperCase();
}, function(lines) {
  console.log('Array of modified lines:', lines);
});

// The following examples all do the same thing:
eachline(process.stdin, function(line, index) {
  return line;
}, function(lines) {
  doSomethingWithLines(lines);
});

// You may omit lineFunction if you don't need to modify the streamed lines.
eachline(process.stdin, doSomethingWithLines);

// You may omit instream if you want to process process.stdin's lines.
eachline(doSomethingWithLines);

Documentation

eachline([instream], [lineFunction], doneFunction)

Why this library?

# I couldn't get readline to give me the last line of a stream if there
# was no trailing newline.
$ echo -en 'foo\nbar\nbaz\n' | node examples/readline-broken.js
[ '[0] <foo>', '[1] <bar>', '[2] <baz>' ]

$ echo -en 'foo\nbar\nbaz' | node examples/readline-broken.js
[ '[0] <foo>', '[1] <bar>' ]


# This is the behavior I expected, based on my experience with ruby's
# STDIN.each_with_index method.
$ echo -en 'foo\nbar\nbaz\n' | ruby examples/each-with-index.rb
["[0] <foo>", "[1] <bar>", "[2] <baz>"]

$ echo -en 'foo\nbar\nbaz' | ruby examples/each-with-index.rb
["[0] <foo>", "[1] <bar>", "[2] <baz>"]


# This lib behaves more like ruby's STDIN.each_with_index method.
$ echo -en 'foo\nbar\nbaz\n' | node examples/eachline-yay.js
[ '[0] <foo>', '[1] <bar>', '[2] <baz>' ]

$ echo -en 'foo\nbar\nbaz' | node examples/eachline-yay.js
[ '[0] <foo>', '[1] <bar>', '[2] <baz>' ]

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

(Nothing yet)

License

Copyright (c) 2014 "Cowboy" Ben Alman
Licensed under the MIT license.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free