0.0.4 • Published 9 years ago
readl-async v0.0.4
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-asyncUsage
//Import dependencies
var readl = require('readl-async');
//Initialize the reader object
var reader = new readl('file.txt', { encoding: 'utf8' });
//Emit this function when one line is read:
reader.on('line', function(line, index, start, end)
{
//Do your magic with the line
// ....
});
//Emit this function when the file is full read
reader.on('end', function()
{
//Do more magic
// ....
});
//Emit this function when an error occurs
reader.on('error', function(error)
{
//Do some stuff with the error
// ....
});
//Start reading the file
reader.read();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 tofalseif 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: astringwith the read line.index: anintegerwith the line number. The line counter starts in 1.start: anintegerwith the start position of the line in the file.end: anintegerwith the end position of the line in the file.length: anintegerwith 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.