Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
nickewing committed Mar 27, 2011
1 parent 37ee14b commit 16b00f7
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions README.md
Expand Up @@ -11,28 +11,46 @@ Install
Usage
-----

The eachLine function returns an object with one property, then. If a callback is passed in, then it will be called once all lines have been read.
The `eachLine` method reads each line of the given file. Upon each new line,
the given callback function is called with two parameters: the line read and a
boolean value specifying whether the line read was the last line of the file.
If the callback returns `false`, reading will stop and the file will be closed.

var lineReader = require('line-reader');

lineReader.eachLine('file.txt', function(line, last) {
console.log(line);

if (/* done */) {
return false; // stop reading
}
});


The `eachLine` function also returns an object with one property, `then`. If a
callback is provided to `then`, it will be called once all lines have been read.

var lineReader = require('line-reader');

// read all lines:
lineReader.eachLine('file.txt', function(line) {
console.log(line);
}).then(function () {
console.log("I'm done!!");
});


For more granular control, `open`, `hasNextLine`, and `nextLine` maybe be used
to iterate a file:

// or read line by line:
lineReader.open('file.txt', function(reader) {
if (reader.hasNextLine()) {
reader.nextLine(function(line) {
console.log(line);
});
}
});

For example, if you need to parse a file and return a JSON array, the `then` function can be used to append a `]` at the end.

var lineReader = require('line-reader');

// read all lines:
lineReader.eachLine('test.txt', function(line) {
console.log(line);
}).then(function () {
console.log("I'm done!!");
});

Another way to interface with it is by manually looping through each line. The parameters are the same, but you get to control when each line is read.

// or read line by line:
lineReader.open('test.txt', function(reader) {
if (reader.nextLine()) {
reader.nextLine(function(line) {
console.log(line);
});
}
});

Copyright 2011 Nick Ewing.

0 comments on commit 16b00f7

Please sign in to comment.