n-readline
Features
- Read text files line by line.
- Support for pause/resume operations.
- Skip and limit strings.
Commands
npm i n-readline
Usage
const NReadline = require('n-readline')
let rl = new NReadline({
filepath: './example.txt',
start: 100,
limit: 50,
skipEmptyLines: true
})
rl.start()
rl.on('error', err => console.log('Error', err))
rl.on('line', (line, lineNumber) => {
// on line 110, pause reader for 100ms
if (lineNumber === 109) {
rl.pause()
setTimeout(() => rl.resume(), 100)
// on line 120, stop reader
} else if (lineNumber === 119) {
rl.stop()
}
console.log(line, lineNumber)
})
rl.on('end', () => console.log('Done'))
API
constructor(params)
Creates instance of NReadline
.
-
filepath
: the path to the source file. -
encoding
: text encoding used by a read stream,utf8
by default. -
start
: the first string number to read, zero based. -
limit
: the count of strings to read. -
skipEmptyLines
: if true, skips empty lines, otherwise not,false
by default.
start(): void
Starts reader.
pause: void
Pauses reader.
resume: void
Resumes reader.
stop: void
Stops reader.
on(event: string; handler: () => {})
Event handler.
-
error
: emitted in the case of any error, parametererr
. -
line
: emitted when a next string is read, parametersline
andlineNumber
. -
end
: emitted when the whole file is read orstop
was called.
Author
Alexander Mac
Licence
Licensed under the MIT license.