Total Complexity | 7 |
Complexity/F | 1.75 |
Lines of Code | 40 |
Function Count | 4 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | // jshint esversion: 8 |
||
10 | function lineCount(text, newLineChar = '\n') { // '\n' unix; '\r' macos; '\r\n' windows |
||
11 | var lines = 0; |
||
12 | for (var char in text) { |
||
|
|||
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, |
||
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 |
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: