fs-line
A Node.js module for reading files line by line.
Features
- Read file line by line, synchronously or asynchronously.
- Using
Stream
, can handler big file without put it in memory. - Transform line string while reading.
Installation
npm install fs-line
Usage
Read and console each line:
const FsLine = ; const fsLine = ;fsLine;let lineCount = 0;fsLine;
Or asynchronously:
const FsLine = ; const fsLine = ;fsLine;let lineCount = 0;fsLine;
Remember to call next
to read next line.
The next
function can pass a input string as a replacement for current line:
const FsLine = ; const fsLine = ;fsLine;let lineCount = 0;fsLine;
API
Class: FsLine([options])
Create a fsline
instance.
const FsLine = ;const fsline = ;
options
: object, with following defaults:
encoding: 'utf8' separator: '\n'
You can change the separator to such as '\r\n'
.
Event: 'line'
Emit when a line is read, first argument is the line string, and the second argument is next
function call to read next line.
fsline
If a argument is passed to next
function, then this line will be changed to the input argument:
fsline
Event: 'end'
All data is read.
Method: fsline.open(file)
Open a file, and begin to read.
file
<sting>: the file path
Method: fsline.close()
Close the file, end the read stream.