Passed
Push — master ( 65f9b0...e165e5 )
by Eduardo
04:01
created

app/js/common/linehelper.js   A

Complexity

Total Complexity 7
Complexity/F 1.75

Size

Lines of Code 40
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 21
mnd 3
bc 3
fnc 4
dl 0
loc 40
rs 10
bpm 0.75
cpm 1.75
noi 2
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A linehelper.js ➔ fileReadLines 0 19 4
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) {
0 ignored issues
show
Complexity introduced by
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
    lines += (lines[char] == newLineChar) ? 1 : 0;
14
  }
15
  return lines;
16
}
17
18
/*
19
  fileReadLines
20
    Read a determined quantity of lines from a specific file
21
  parameters
22
    filePath (string) - file path to read lines from
23
    lines (integer) - line quantity to read from file
24
    startLine (integer) - line to start reading from
25
 */
26
function fileReadLines(filePath, lines = 2, startLine = 0) {
27
  var lineCounter = startLine,
28
    endLine = startLine + lines,
0 ignored issues
show
Unused Code introduced by
The variable endLine seems to be never used. Consider removing it.
Loading history...
29
    linesRead = [],
30
    lineReader = require('readline').createInterface({
31
      input: require('fs').createReadStream(filePath),
32
    });
33
34
  lineReader.on('line', function(line) {
35
    lineCounter++;
36
    linesRead.push(line);
37
    if (lineCounter == lines) {
38
      lineReader.close();
39
    }
40
  });
41
  lineReader.on('close', function() {
42
    return linesRead;
43
  });
44
}
45
46
module.exports = {
47
  lineCount: lineCount,
48
  fileReadLines: fileReadLines
49
};
50