meteor/node-eachline

Name: node-eachline

Owner: Meteor

Description: Streams2 line reader

Created: 2014-02-14 01:30:30.0

Updated: 2014-02-14 01:37:09.0

Pushed: 2017-07-13 19:35:18.0

Homepage: null

Size: 14

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

eachline

Streams2 line reader / stream transformer.

First let me say: I did NOT want to write yet another line reader! I couldn't find one that is working with node's new Streams2 objects.

The byline module has taken a first stab at it, but it isn't working. (as of 32bf791e38)

So here we are…

fs = require('fs');
eachline = require('eachline');
stream = fs.createReadStream(__dirname+'/.gitignore');

line(stream, function(line){
nsole.log(line);

However, if you're just looking to open a file:

eachline = require('eachline');
line.in(__dirname+'/.gitignore', function(line){
nsole.log(line);

…or if you need a simple http GET:

eachline = require('eachline');
url = 'https://raw.github.com/williamwicks/node-eachline/master/.gitignore';
line.in(url, function(line){
nsole.log(line);

Transforming Streams

eachline uses Streams2's transformation feature allowing you to throw eachline between .pipe()s to modify output as needed.

fs = require('fs');
eachline = require('eachline');
file = fs.createReadStream(__dirname+'/.gitignore');

transformer = eachline(function(data){
turn data.substr(0, 2)+'\n';


.pipe(transformer).pipe(process.stdout);
API
eachline()

Use with a pipe() to re-chunk the stream into lines.

eachline([encoding,] callback)

Use with pipe() optionally specifying the encoding.

eachline(ReadableStream,[ encoding,] callback)

Got that stream ready? Pass it in, get them lines. Easy-peasy.

eachline.in(url, callback)

eachline.in(filepath, callback) Just a helper function to make these simple tasks cleaner.

It returns the Transform stream so you can listen to the events.

linecount = 0;
eachline = require('eachline');
line.in(__dirname+'/.gitignore', function(data){
linecount++;

'finish', function(){
console.log(linecount + " lines found");

callback(data, lineno[ ,next])
The callback arguments above will be called for every line found in the ReadableStream.

It will be passed the data and lineno arguments. You can optionally defined a 3rd argument to get asynchronous flow. eachline examines callback.length to detirmine if asynchronous flow should be used. If found, you must call next() to continue reading.

For Stream transformations, any value return'd will be written to the next pipe() in the chain.

License

The MIT License (MIT)

Copyright (c) 2013 William Wicks

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.