Issues (103)

app/js/common/linehelper.js (1 issue)

Severity
1
// jshint esversion: 8
2
3
/*
4
  lineCount
5
    Count lines within a string
6
  parameters
7
    text (string) - string to count lines from
8
    newLineChar (string) - new line character
9
 */
10
function lineCount(text, newLineChar = '\n') { // '\n' unix; '\r' macos; '\r\n' windows
11
  var lines = 0;
12
  for (var char in text) lines += (lines[char] == newLineChar) ? 1 : 0;
0 ignored issues
show
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
13
14
  return lines;
15
}
16
17
/*
18
  fileReadLines
19
    Read a determined quantity of lines from a specific file
20
  parameters
21
    filePath (string) - file path to read lines from
22
    lines (integer) - line quantity to read from file
23
    startLine (integer) - line to start reading from
24
 */
25
function fileReadLines(filePath, lines = 2, startLine = 0) {
26
  var lineCounter = startLine,
27
    //endLine = startLine + lines,
28
    linesRead = [],
29
    lineReader = require('readline').createInterface({
30
      input: require('fs').createReadStream(filePath),
31
    });
32
33
  lineReader.on('line', function(line) {
34
    lineCounter++;
35
    linesRead.push(line);
36
    if (lineCounter == lines) lineReader.close();
37
  });
38
39
  lineReader.on('close', function() {
40
    return linesRead;
41
  });
42
43
  return 0;
44
}
45
46
module.exports = {
47
  lineCount: lineCount,
48
  fileReadLines: fileReadLines
49
};
50