From 16b00f7d52ad036b4a1a4e725f3082a2df6bac7d Mon Sep 17 00:00:00 2001 From: Nick Ewing Date: Sun, 27 Mar 2011 04:27:44 -0700 Subject: [PATCH] Update README --- README.md | 62 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 1907b21..eb2188b 100644 --- a/README.md +++ b/README.md @@ -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.