readl-async
Read a file line by line asynchronously
Install
You can install the latest version of the package using npm:
$ npm install --save readl-async
Usage
//Import dependenciesvar readl = ; //Initialize the reader objectvar reader = 'file.txt' encoding: 'utf8' ; //Emit this function when one line is read:reader; //Emit this function when the file is full readreader; //Emit this function when an error occursreader; //Start reading the filereader;
API
var reader = new readl(file, options)
Initialize the reader object. This method accepts the following arguments:
file
A string
with the path to the file.
options
An object
with the following options:
encoding
: set the encoding. Default:utf8
.emptyLines
: set it tofalse
if you want to omit the empty lines. Default:true
.start
: start position. Default is 0.chunk
: set the chunk size. Default is10240
.endl
: set the end-line character. Default is0x0a
.
reader.read()
Starts the file read process.
reader.on('line', handler)
Emit the provided function when one line of the file is read. The handler function will be called with the following arguments:
line
: astring
with the read line.index
: aninteger
with the line number. The line counter starts in 1.start
: aninteger
with the start position of the line in the file.end
: aninteger
with the end position of the line in the file.length
: aninteger
with the number of bytes read.
You can stop the file reading at a particular line by making the callback function return false
.
reader.on('end', handler)
Emit the provided function when the end of file is reached.
reader.on('error', handler)
Emit the provided function if there was an error reading the file.
Related
- readl: synchronous version of this module.
License
MIT © Josemi Juanes.