JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1412
  • Score
    100M100P100Q113475F
  • License MIT

Generator based line reader

Package Exports

  • gen-readlines

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (gen-readlines) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

gen-readlines Build Status

A generator based line reader. This node package will return the lines of a file as a generator when given file descriptor and the size of the file.

Install

npm install gen-readlines

Usage

Provide readlines with a file descriptor and the size of the file and it will create a generator which will iterate through all the lines in that file.

var fs = require('fs');
var readlines = require('gen-readlines');

var fd = fs.openSync('./test_data/hipster.txt', 'r');
var stats = fs.fstatSync(fd);

for (let line of readlines(fd, stats.size)) {
  console.log(line.toString());
}

fs.closeSync(fd);
fs.open('./test_data/hipster.txt', 'r', function(err, fd) {
  if (err) throw err;
  fs.fstat(fd, function(err, stats) {
    if (err) throw err;

    for (let line of readlines(fd, stats.size)) {
      console.log(line.toString());
    }

  });
});

readlines returns a generator object and calling next will get the next line as a buffer object:

var file = readlines(fd, stats.size);
var line = file.next();
console.log(line);
> { value: <Buffer 42 65 73 70 ... >, done: false }

Convert the buffer to a string:

line.toString();
> This is the first line of the file

Compatibility

  • node 0.11.x or greater, with --harmony-generators flag or --harmony to get access to generators.
  • io.js is supported without any flags.

Documentation

readlines (fd, filesize, bufferSize=64*1024, position=0)

  • fd {Number} The file descriptor
  • filesize {Number} The size of the file in bytes
  • bufferSize {Number} The size of the buffer in bytes, default: 64*1024
  • position {Number} The position where to start reading the file in bytes, default: 0

Testing

We are using mocha for unit testing

npm test