Giter Site home page Giter Site logo

gkbps / line-by-line Goto Github PK

View Code? Open in Web Editor NEW

This project forked from osterjour/line-by-line

0.0 1.0 0.0 18 KB

A NodeJS module that helps you reading large text files, line by line, without buffering the files into memory.

License: MIT License

JavaScript 100.00%

line-by-line's Introduction

Build Status npm version

Line By Line

is a NodeJS module that helps you reading large text files, line by line, without buffering the files into memory.

Installation:

npm install line-by-line

Usage:

Synchronous processing of lines:

var LineByLineReader = require('line-by-line'),
    lr = new LineByLineReader('big_file.txt');

lr.on('error', function (err) {
	// 'err' contains error object
});

lr.on('line', function (line) {
	// 'line' contains the current line without the trailing newline character.
});

lr.on('end', function () {
	// All lines are read, file is closed now.
});

Asynchronous processing of lines:

var LineByLineReader = require('line-by-line'),
    lr = new LineByLineReader('big_file.txt');

lr.on('error', function (err) {
	// 'err' contains error object
});

lr.on('line', function (line) {
	// pause emitting of lines...
	lr.pause();

	// ...do your asynchronous line processing..
	setTimeout(function () {

		// ...and continue emitting lines.
		lr.resume();
	}, 100);
});

lr.on('end', function () {
	// All lines are read, file is closed now.
});

Initialize with Stream:

var LineByLineReader = require('line-by-line'),
    lr = new LineByLineReader(S3.getObject({ Bucket, Key }).createReadStream());

API:

Class: LineReader(path [, options])

path specifies the file to read or Stream

options is an object with the following defaults:

{ encoding: 'utf8',
  skipEmptyLines: false }

encoding can be 'utf8', 'ascii', or 'base64'.

If skipEmptyLines set to true, empty lines don't trigger a 'line' event.

You can also pass start and end position to read from file region:

{ encoding: 'utf8',
  skipEmptyLines: true,
  start: 1000 }

Event: 'line'

function (line) { }

Emitted on every line read.

line contains the line without the line ending character.

Event: 'error'

function (error) { }

Emitted if an error occurred.

error contains the error object.

Event: 'end'

function () { }

Emitted if all lines are read.

pause()

Call this method to stop emitting 'line' events.

resume()

After calling this method, 'line' events gets emitted again.

close()

Stops emitting 'line' events, closes the file and emits the 'end' event.

License:

The MIT License (MIT)

Copyright © 2012 Markus von der Wehd

line-by-line's People

Contributors

abelozerov avatar alexloi avatar gogson avatar netoneko avatar osterjour avatar rustymarvin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.