node-line-reader
Advanced tools
Comparing version 0.0.1 to 0.0.2
26
index.js
@@ -0,21 +1,7 @@ | ||
(function () { | ||
"use strict"; | ||
module.exports.LineTransform = require('./lib/transformer'); | ||
module.exports.LineReader = require('./lib/reader'); | ||
// var LineTransform = require('./lib/transformer'); | ||
// var fs = require('fs'); | ||
// | ||
// var stream = fs.createReadStream('.\\test\\input.txt'); | ||
// var transform = new LineTransform(); | ||
// | ||
// stream.pipe(transform); | ||
// | ||
// transform.on('data', function(chunk) { | ||
// console.log('got %d bytes of data', chunk.length, chunk); | ||
// transform.pause(); | ||
// console.log('there will be no more data for 1 second'); | ||
// setTimeout(function() { | ||
// console.log('now data will start flowing again'); | ||
// transform.resume(); | ||
// }, 100); | ||
// }) | ||
module.exports.LineTransform = require('./lib/transformer'); | ||
module.exports.LineReader = require('./lib/reader'); | ||
module.exports.LineFilter = require('./lib/filter'); | ||
}()); |
@@ -1,59 +0,61 @@ | ||
"use strict"; | ||
(function () { | ||
"use strict"; | ||
var fs = require('fs'); | ||
var LineTransformer = require('./transformer'); | ||
var StringDecoder = require('string_decoder').StringDecoder; | ||
var fs = require('fs'); | ||
var LineTransformer = require('./transformer'); | ||
var StringDecoder = require('string_decoder').StringDecoder; | ||
var LineReader = module.exports = function LineReader (file, options) { | ||
this.file = (typeof file === 'object') ? this.file = options.input : file; | ||
var LineReader = module.exports = function LineReader (file, options) { | ||
this.file = (typeof file === 'object') ? this.file = options.input : file; | ||
if(!fs.existsSync(this.file)) { | ||
throw new Error('File: ' + file + ' does not exist'); | ||
} | ||
if(!fs.existsSync(this.file)) { | ||
throw new Error('File: ' + file + ' does not exist'); | ||
} | ||
options = options || {}; | ||
this._encoding = options.encoding || 'utf8'; | ||
this._decoder = new StringDecoder(this._encoding); | ||
this._reader = new LineTransformer(); | ||
options = options || {}; | ||
this._encoding = options.encoding || 'utf8'; | ||
this._decoder = new StringDecoder(this._encoding); | ||
this._reader = new LineTransformer(); | ||
this._source = fs.createReadStream(this.file); | ||
this._source.setEncoding(this._encoding); | ||
this._source = fs.createReadStream(this.file); | ||
this._source.setEncoding(this._encoding); | ||
this._init = false; | ||
this._end = false; | ||
this._cbs = []; | ||
}; | ||
this._init = false; | ||
this._end = false; | ||
this._cbs = []; | ||
}; | ||
LineReader.prototype.nextLine = function (cb) { | ||
if(!this._init) { | ||
this._init = true; | ||
this._source.pipe(this._reader); | ||
this._reader.on('data', onData.bind(this)); | ||
this._reader.on('end', onEnd.bind(this)); | ||
} | ||
LineReader.prototype.nextLine = function (cb) { | ||
if(!this._init) { | ||
this._init = true; | ||
this._source.pipe(this._reader); | ||
this._reader.on('data', onData.bind(this)); | ||
this._reader.on('end', onEnd.bind(this)); | ||
} | ||
if(this._end) { | ||
return cb(null, null); | ||
} | ||
if(this._end) { | ||
return cb(null, null); | ||
} | ||
this._cbs.push(cb); | ||
this._reader.resume(); | ||
this._cbs.push(cb); | ||
this._reader.resume(); | ||
/*jshint validthis:true */ | ||
function onData(line) { | ||
this._cbs.shift()(null, this._decoder.write(line)); | ||
/*jshint validthis:true */ | ||
function onData(line) { | ||
this._cbs.shift()(null, this._decoder.write(line)); | ||
if(this._cbs.length === 0) { | ||
this._reader.pause(); | ||
} | ||
} | ||
if(this._cbs.length === 0) { | ||
this._reader.pause(); | ||
} | ||
} | ||
/*jshint validthis:true */ | ||
function onEnd() { | ||
this._end = true; | ||
this._cbs.forEach(function (cb) { | ||
cb(null, null); | ||
}); | ||
this._cbs = []; | ||
} | ||
}; | ||
/*jshint validthis:true */ | ||
function onEnd() { | ||
this._end = true; | ||
this._cbs.forEach(function (cb) { | ||
cb(null, null); | ||
}); | ||
this._cbs = []; | ||
} | ||
}; | ||
}()); |
@@ -1,43 +0,47 @@ | ||
var util = require('util'); | ||
var Transform = require('stream').Transform; | ||
var StringDecoder = require('string_decoder').StringDecoder; | ||
(function () { | ||
"use strict"; | ||
util.inherits(LineTransformer, Transform); | ||
var util = require('util'); | ||
var Transform = require('stream').Transform; | ||
var StringDecoder = require('string_decoder').StringDecoder; | ||
function LineTransformer(options) { | ||
if (!(this instanceof LineTransformer)) { | ||
return new LineTransformer(options); | ||
} | ||
util.inherits(LineTransformer, Transform); | ||
Transform.call(this, options); | ||
this._writableState.objectMode = false; | ||
this._readableState.objectMode = true; | ||
this._buffer = ''; | ||
this._decoder = new StringDecoder('utf8'); | ||
} | ||
function LineTransformer(options) { | ||
if (!(this instanceof LineTransformer)) { | ||
return new LineTransformer(options); | ||
} | ||
LineTransformer.prototype._transform = function (chunk, encoding, done) { | ||
this._buffer += this._decoder.write(chunk); | ||
Transform.call(this, options); | ||
this._writableState.objectMode = false; | ||
this._readableState.objectMode = true; | ||
this._buffer = ''; | ||
this._decoder = new StringDecoder('utf8'); | ||
} | ||
var lines = this._buffer.split(/\r?\n/); | ||
LineTransformer.prototype._transform = function (chunk, encoding, done) { | ||
this._buffer += this._decoder.write(chunk); | ||
// Keep the last (potentially) partial line buffered | ||
this._buffer = lines.pop(); | ||
var lines = this._buffer.split(/\r?\n/); | ||
// Process all lines | ||
lines.forEach(this.push.bind(this)); | ||
// Keep the last (potentially) partial line buffered | ||
this._buffer = lines.pop(); | ||
done(); | ||
}; | ||
// Process all lines | ||
lines.forEach(this.push.bind(this)); | ||
LineTransformer.prototype._flush = function (done) { | ||
var rem = this._buffer.trim(); | ||
done(); | ||
}; | ||
if(rem) { | ||
this.push(rem); | ||
} | ||
LineTransformer.prototype._flush = function (done) { | ||
var rem = this._buffer.trim(); | ||
done(); | ||
}; | ||
if(rem) { | ||
this.push(rem); | ||
} | ||
module.exports = LineTransformer; | ||
done(); | ||
}; | ||
module.exports = LineTransformer; | ||
}()); |
{ | ||
"name": "node-line-reader", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Reads lines of text from input stream", | ||
@@ -18,3 +18,3 @@ "main": "index.js", | ||
], | ||
"author": "Tom Pawlak <tompwk@gmail.com>", | ||
"author": "Tom Pawlak <tompwk@gmail.com> (http://blog.tompawlak.org)", | ||
"license": "MIT", | ||
@@ -27,3 +27,6 @@ "bugs": { | ||
"async": "^0.9.0" | ||
}, | ||
"engines": { | ||
"node": ">=0.10" | ||
} | ||
} |
112
README.md
@@ -1,12 +0,13 @@ | ||
[![Build Status](https://travis-ci.org/T-PWK/node-line-reader.svg)](https://travis-ci.org/T-PWK/node-line-reader) [![Code Climate](https://codeclimate.com/github/T-PWK/node-line-reader/badges/gpa.svg)](https://codeclimate.com/github/T-PWK/node-line-reader) [![Test Coverage](https://codeclimate.com/github/T-PWK/node-line-reader/badges/coverage.svg)](https://codeclimate.com/github/T-PWK/node-line-reader) | ||
[![Build Status](https://travis-ci.org/T-PWK/node-line-reader.svg)](https://travis-ci.org/T-PWK/node-line-reader) [![npm version](https://badge.fury.io/js/node-line-reader.svg)](http://badge.fury.io/js/node-line-reader) [![Code Climate](https://codeclimate.com/github/T-PWK/node-line-reader/badges/gpa.svg)](https://codeclimate.com/github/T-PWK/node-line-reader) [![Test Coverage](https://codeclimate.com/github/T-PWK/node-line-reader/badges/coverage.svg)](https://codeclimate.com/github/T-PWK/node-line-reader) | ||
## Node Line Reader | ||
Node Line Reader | ||
======================== | ||
Node Line Reader is a [Node.js](http://nodejs.org/) module that helps you reading lines of text from files. | ||
Node Line Reader is a [Node.js](http://nodejs.org/) module that helps you reading lines of text from a file. | ||
Features: | ||
- Reads lines of text from Readable streams e.g. files | ||
- Reads lines of text from Readable streams e.g. a file | ||
- Reads lines that match one or more patterns | ||
- Skips lines that match one or more patterns e.g. empty lines | ||
- Reads part of files | ||
- Reads part of a file | ||
@@ -18,8 +19,13 @@ Installation: | ||
### Usage: | ||
##### Using `LineReader` | ||
## Usage: | ||
```javascript | ||
### Using `LineReader` | ||
```js | ||
var path = require('path'); | ||
var LineReader = require('node-line-reader').LineReader; | ||
var reader = new LineReader(path.join('/home/user', 'some-file.txt')); | ||
// Each execution of nextLine will get a following line of text from the input file | ||
reader.nextLine(function (err, line) { | ||
@@ -32,5 +38,5 @@ if (!err) { | ||
##### Using `LineTransform` | ||
### Using `LineTransform` | ||
```javascript | ||
```js | ||
var stream = getSomeReadableStream(); // Create read stream | ||
@@ -46,30 +52,96 @@ var LineTransform = require('node-line-reader').LineTransform; // LineTransform constructor | ||
transform.on('end', function(line) { | ||
transform.on('end', function() { | ||
// no more text lines | ||
}); | ||
``` | ||
### API: | ||
### Using `LineFilter` | ||
#### Class: LineTransform | ||
```js | ||
var stream = getSomeReadableStream(); // Create read stream | ||
var LineTransform = require('node-line-reader').LineTransform; // LineTransform constructor | ||
var LineFilter = require('node-line-reader').LineFilter; // LineFilter constructor | ||
var transform = new LineTransform(); | ||
// Skip empty lines and lines with "et" (with leading and trailing space) in them | ||
var filter = new LineFilter({ skipEmpty: true, exclude: [/\bet\b/ }); | ||
// Pipe input from a file stream over to line transform and to the filter | ||
stream.pipe(transform).pipe(filter); | ||
filter.on('data', function(line) { | ||
// line - single line of text from input file | ||
}); | ||
filter.on('end', function() { | ||
// no more text lines | ||
}); | ||
``` | ||
## API: | ||
### Class: `LineReader` | ||
`LineReader` is a text lines reader from a specified file. | ||
#### 'LineReader.nextLine(cb)' | ||
This function reads another line of text from a specified file and passes it over to the callback method. | ||
The callback has arguments `(err, line)`. The `err` argument is an error that occurred while reading a line of text (`null` if no error occurred). The `line` is a string with line of text from a specified input file. | ||
### Class: `LineFilter` | ||
`LineFilter` is a duplex stream passing through lines of text matching include and exclude rules. `LineFilter` instance can be piped into antoher [Writable](http://nodejs.org/api/stream.html#stream_class_stream_writable) instance. | ||
The `LineFilter` accepts options objects with following parameters: | ||
- `skipEmpty` - boolean value blocking empty lines | ||
- `skipBlank` - boolean value blocking blank lines (lines composed of whitespace characters) | ||
- `include` - a single instance or an array of regular expressions (see below for text filtering rules) | ||
- `exclude` - a single instance or an array of regular expressions (see below for text filtering rules) | ||
**Text filtering rules**: | ||
- If only `include` pattern(s) are provided, filter will pass through lines of text which are matched by the include pattern(s) only | ||
- If only `exclude` pattern(s) are provided, fillter will pass through all lines except the ones which are matched by the exclude pattern(s) only | ||
- If `include` and `exclude` pattern(s) are provided, filter filter will pass through lines of text which are matched by the include pattern(s) and not matched by the exclude pattern(s) | ||
#### Event: 'readable' | ||
When a line of text can be read from the transform, it will emit a 'readable' event. | ||
#### Event: 'data' | ||
- `line` Buffer | String The line of text. | ||
#### Event: 'end' | ||
This event fires when there will be no more test to read. | ||
#### Event: 'close' | ||
As [Stream 'close' event](http://nodejs.org/api/stream.html#stream_event_close): Emitted when the underlying resource (for example, the backing file descriptor) has been closed. Not all streams will emit this. | ||
#### Event: 'error' | ||
As [Stream 'close' event](http://nodejs.org/api/stream.html#stream_event_error): Emitted if there was an error receiving data. | ||
### Class: `LineTransform` | ||
`LineTransform` is a duplex stream converting input text into lines of text. `LineTransform` instance can be piped into antoher [Writable](http://nodejs.org/api/stream.html#stream_class_stream_writable) instance. | ||
##### Event: 'readable' | ||
#### Event: 'readable' | ||
When a line of text can be read from the transform, it will emit a 'readable' event. | ||
##### Event: 'data' | ||
#### Event: 'data' | ||
- `line` Buffer | String The line of text. | ||
##### Event: 'end' | ||
#### Event: 'end' | ||
This event fires when there will be no more test to read. | ||
##### Event: 'close' | ||
#### Event: 'close' | ||
As [Stream 'close' event](http://nodejs.org/api/stream.html#stream_event_close): Emitted when the underlying resource (for example, the backing file descriptor) has been closed. Not all streams will emit this. | ||
##### Event: 'error' | ||
#### Event: 'error' | ||
As [Stream 'close' event](http://nodejs.org/api/stream.html#stream_event_error): Emitted if there was an error receiving data. | ||
### License: | ||
## Author | ||
Writen by Tom Pawlak - [Blog](http://blog.tompawlak.org) | ||
## License: | ||
The MIT License (MIT) | ||
Copyright © 2014 Tom Pawlak | ||
Copyright © 2014 Tom Pawlak |
@@ -0,0 +0,0 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
@@ -0,0 +0,0 @@ var assert = require('assert'); |
@@ -0,0 +0,0 @@ var fs = require('fs'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30357
13
456
144
3