Have ideas to improve npm?Join in the discussion! »

n-readline
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

n-readline

Build Status Code Coverage npm version

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, parameter err.
  • line: emitted when a next string is read, parameters line and lineNumber.
  • end: emitted when the whole file is read or stop was called.

Author

Alexander Mac

Licence

Licensed under the MIT license.

Keywords

Install

npm i n-readline

DownloadsWeekly Downloads

75

Version

0.1.2

License

MIT

Unpacked Size

7.63 kB

Total Files

6

Last publish

Collaborators

  • avatar