0.2.0 • Published 10 years ago

fileinput v0.2.0

Weekly downloads
19
License
-
Repository
github
Last release
10 years ago

fileinput

A class and functions to quickly write a loop over standard input or a list of files. Heavilly inspired by the fileinput module of Python.

Build Status

Installation

$ npm install fileinput

Typical Usage

var fileinput = require('fileinput');

fileinput.input()
  .on('line', function(line) {
  	console.log( fileinput.lineno(), line.toString('utf8') );
  });

This will read files specified in process.argv and emit each line to the event line. When no filenames are found in argv it will default to reading from stdin (this can also be achieved by using - as a filename).

Files can also be passed to the .input() function directly:

var fileinput = require('fileinput');

var lines = 0;
fileinput.input(['index.js', 'lib/fileinput.js')
  .on('line', function(line) {
    if(line.toString().trim().length > 0) {
      lines += 1;
    }
  })
  .on('end', function() {
    console.log('lines:', lines);
  });

See examples/ from more uses.

API documentation

Read the examples, tests and source for now, sorry...

Todo

  • Support stdin reading for Windows (it now uses /dev/stdin)