简体   繁体   中英

Node.js Readline, get the current line number

I have the following implementation where everything works but this line:

lineNumber: line.lineNumber

this line returns undefined, I am adding a full fragment of code below, my question is: Does Readline provide a standard way to get the line number somehow? Or I have to implement my counter to keep track of the line number, which would be simple but I don't want if there's a standard way of doing it?

/**
* Search for occurrences of the specified pattern in the received list of files.
* @param filesToSearch - the list of files to search for the pattern
* @returns {Promise} - resolves with the information about the encountered matches for the pattern specified.
*/
const findPattern = (filesToSearch) => {
console.log(filesToSearch);
return new Promise((resolve, reject) => {
 var results = [ ];
 // iterate over the files
 for(let theFile of filesToSearch){
  let dataStream = fs.createReadStream(theFile);
  let lineReader = readLine.createInterface({
    input: dataStream
  });

  let count = 0; // this would do the trick but I'd rather use standard approach if there's one
  // iterates over each line of the current file
  lineReader.on('line',(line) => {
    count++;
    if(line.indexOf(searchPattern) > 0) {
      let currLine = line.toString();
      currLine = currLine.replace(/{/g, '');//cleanup { from string if present
      results.push({
        fileName: theFile,
        value: currLine,
        lineNumber: line.lineNumber //HERE: this results undefined
        //lineNumber: count // this does the trick but I'd rather use standard approach.
      });
    }
  });

   // resolve the promise once the file scan is finished.
   lineReader.on('close', () => resolve(results));
  }
 });
};

Unfortunately there isn't a way to find the line number using the readline node module, however, using ES6 it isn't difficult to roll your own counter in one line of code.

const line_counter = ((i = 0) => () => ++i)();

When we create the callback function we simply default the second parameter to the line_counter function, and we can act as though the line and the line number are both being passed when a line event occurs.

rl.on("line", (line, lineno = line_counter()) => {
  console.log(lineno); //1...2...3...10...100...etc
});

Simply, using a variable increment together with foo(data, ++i) it will always pass the number of the new line to the function.

let i = 0
const stream = fs.createReadStream(yourFileName)
stream.pipe().on("data", (data) => foo(data, ++i))

const foo = (data, line) => {
  consle.log("Data: ", data)
  consle.log("Line number:", line)
}

You need to include the lineno param if you are using node linereader

lineReader.on('line', function (lineno, line) {
    if(line.indexOf(searchPattern) > 0) {
          let currLine = line.toString();
          currLine = currLine.replace(/{/g, '');//cleanup { from string if present
          results.push({
            fileName: theFile,
            value: currLine,
            lineNumber: lineno 
          });
     }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM