Skip to content

Commit 0acf2ca

Browse files
committed
refactor: improve readability by Deleting comments and replacing forEach with for...of loops
1 parent 7a43ee8 commit 0acf2ca

File tree

1 file changed

+15
-17
lines changed
  • implement-shell-tools/cat

1 file changed

+15
-17
lines changed

implement-shell-tools/cat/cat.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,32 @@ program
1111

1212
program.parse(process.argv);
1313

14-
const args = program.args; //Array all file paths
14+
const filepaths = program.args;
1515

16-
//read flags user typed and return them as object.
17-
const opts = program.opts();
16+
const options = program.opts();
1817

1918
let lineNumber = 1;
2019

21-
// Loop over every filepath in args
22-
args.forEach(async (filepath) => {
23-
const content = await fs.readFile(filepath, "utf8");
24-
const lines = content.split("\n");
20+
for (const filepath of filepaths) {
21+
const fileContent = await fs.readFile(filepath, "utf8");
22+
const lines = fileContent.split("\n");
2523

26-
lines.forEach((line) => {
27-
if (opts.numberAllLines) {
28-
// -n: number every line
24+
for (const line of lines) {
25+
if (options.numberAllLines) {
2926
console.log(`${lineNumber} ${line}`);
3027
lineNumber++;
31-
} else if (opts.numberNonEmptyLines) {
32-
// -b: number non-empty lines only
28+
continue;
29+
}
30+
31+
if (options.numberNonEmptyLines) {
3332
if (line.trim() === "") {
3433
console.log(line);
3534
} else {
3635
console.log(`${lineNumber} ${line}`);
3736
lineNumber++;
3837
}
39-
} else {
40-
// No flag: just print
41-
console.log(line);
38+
continue;
4239
}
43-
});
44-
});
40+
console.log(line);
41+
}
42+
}

0 commit comments

Comments
 (0)