Skip to content

Commit acea6b3

Browse files
author
Simon Renoult
committed
feat: support carriage returns when computing complexity
1 parent 9ff3e31 commit acea6b3

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/lib/complexity.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,20 @@ async function getComplexity(path: Path, options: Options): Promise<number> {
4545
}
4646

4747
async function countLineNumber(absolutePath: string): Promise<number> {
48+
const ASCII_FOR_NEW_LINE = "10";
49+
const ASCII_FOR_CARRIAGE_RETURN = "13";
50+
4851
let count = 0;
4952
return new Promise((resolve) => {
5053
createReadStream(absolutePath)
5154
.on("data", function (chunk) {
52-
for (let i = 0; i < chunk.length; ++i) if (chunk[i] == 10) count++;
55+
for (let i = 0; i < chunk.length; ++i) {
56+
const char = chunk[i];
57+
// Use "==" instead of "===" to use type coercion
58+
if (char == ASCII_FOR_NEW_LINE || char == ASCII_FOR_CARRIAGE_RETURN) {
59+
count += 1;
60+
}
61+
}
5362
})
5463
.on("end", function () {
5564
resolve(count);

0 commit comments

Comments
 (0)