0.0.4 • Published 7 years ago

readl-async v0.0.4

Weekly downloads
30
License
MIT
Repository
github
Last release
7 years ago

readl-async

Read a file line by line asynchronously

npm npm npm

Install

You can install the latest version of the package using npm:

$ npm install --save readl-async

Usage

//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 to false if you want to omit the empty lines. Default: true.
  • start: start position. Default is 0.
  • chunk: set the chunk size. Default is 10240.
  • endl: set the end-line character. Default is 0x0a.

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: a string with the read line.
  • index: an integer with the line number. The line counter starts in 1.
  • start: an integer with the start position of the line in the file.
  • end: an integer with the end position of the line in the file.
  • length: an integer 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.