maleck13 / readline

read line module for node js
93 stars 27 forks source link

Get maximun lines of the file #23

Open masalinas opened 6 years ago

masalinas commented 6 years ago

How can I get the number of lines of the file with your library?

adadgio commented 5 years ago

Up ! Agree that would be super useful, because there is currently no way to known when the end of file has been reached

adadgio commented 5 years ago

I found a way by wrapping into a promise and declare a global variable that you can increment. Code is bellow (ts).

let maxLines: number = 0

return new Promise((resolve, reject) => {
    rl.on('line', (line, lineCount, byteCount) => {
        maxLines = lineCount
        // here i am looking for a string, and... 
       // ... if blahbla => resolve promise
    })
    .on('end', () => {
        console.log('End of file reached', maxLines)
    })
    .on('error', (err) => {
        reject(err)
    })
})