Node Line Reader
Node Line Reader is a Node.js module that helps you reading lines of text from a file.
Features:
- 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 a file
Installation:
npm install node-line-reader
Usage:
LineReader
Using var path = ;var LineReader = LineReader; var reader = path; // Each execution of nextLine will get a following line of text from the input filereader;
LineTransform
Using var stream = ; // Create read streamvar LineTransform = LineTransform; // LineTransform constructorvar transform = ; stream; // Pipe input from a file stream over to line transform transform; transform;
LineFilter
Using var stream = ; // Create read streamvar LineTransform = LineTransform; // LineTransform constructorvar LineFilter = LineFilter; // LineFilter constructor var transform = ; // Skip empty lines and lines with "et" (with leading and trailing space) in themvar filter = skipEmpty: true exclude: /\bet\b/ ; // Pipe input from a file stream over to line transform and to the filterstream; filter; filter;
API:
LineReader
Class: 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.
LineFilter
Class: LineFilter
is a duplex stream passing through lines of text matching include and exclude rules. LineFilter
instance can be piped into antoher Writable instance.
The LineFilter
accepts options objects with following parameters:
skipEmpty
- boolean value blocking empty linesskipBlank
- 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
andexclude
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: 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: Emitted if there was an error receiving data.
LineTransform
Class: LineTransform
is a duplex stream converting input text into lines of text. LineTransform
instance can be piped into antoher Writable instance.
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: 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: Emitted if there was an error receiving data.
Author
Writen by Tom Pawlak - Blog
License:
The MIT License (MIT)
Copyright © 2014 Tom Pawlak