Skip to content

Commit ba910d0

Browse files
code refactored and cleaned up
1 parent 0c384d7 commit ba910d0

1 file changed

Lines changed: 18 additions & 33 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.js

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,50 @@ program
1111

1212
program.parse();
1313

14-
const paths = program.args;
14+
const filePaths = program.args;
1515
const { lines, words, bytes } = program.opts();
1616

17+
// When no options are provided, show all counts
1718
const showAll = !lines && !words && !bytes;
1819

1920
// --- Read files and sizes ---
20-
let content = "";
21-
let output = [];
21+
let fileContent = "";
22+
let outputData = [];
2223

2324
let lineCountTotal = 0,
2425
wordCountTotal = 0,
2526
fileSizeTotal = 0;
2627

27-
if (Object.keys(program.opts()).length === 1) {
28-
}
29-
30-
for (const path of paths) {
28+
for (const path of filePaths) {
3129
let fileStats;
32-
let data = {};
30+
let fileData = {};
3331

34-
content = await fs.readFile(path, "utf-8");
35-
if (content.endsWith("\n")) {
36-
content = content.slice(0, -1);
37-
}
32+
fileContent = await fs.readFile(path, "utf-8");
3833

39-
data.lineCount = getLineCount(content);
40-
lineCountTotal += data.lineCount;
34+
fileData.lineCount = getLineCount(fileContent);
35+
lineCountTotal += fileData.lineCount;
4136

42-
data.wordCount = getWordCount(content);
43-
wordCountTotal += data.wordCount;
37+
fileData.wordCount = getWordCount(fileContent);
38+
wordCountTotal += fileData.wordCount;
4439

4540
fileStats = await fs.stat(path);
46-
data.fileSize = fileStats.size;
47-
fileSizeTotal += data.fileSize;
41+
fileData.fileSize = fileStats.size;
42+
fileSizeTotal += fileData.fileSize;
4843

49-
data.path = path;
50-
output.push(data);
44+
fileData.path = path;
45+
outputData.push(fileData);
5146
}
5247

53-
console.log(output);
48+
console.log(outputData.map(formatOutput).join("\n"));
5449

55-
if (paths.length > 1) {
50+
if (filePaths.length > 1) {
5651
console.log(
5752
`${String(lineCountTotal).padStart(3)}${String(wordCountTotal).padStart(
5853
4
5954
)}${String(fileSizeTotal).padStart(4)} total`
6055
);
6156
}
6257

63-
// output.push(String(fileSize.size).padStart(4));
64-
// console.log(`${output.join("")} ${path}`);
65-
6658
function formatOutput({ lineCount, wordCount, fileSize, path }) {
6759
let output = [];
6860
if (lines || showAll) output.push(String(lineCount).padStart(3));
@@ -76,18 +68,11 @@ function getWordCount(text) {
7668
let words, lines;
7769

7870
lines = text.split("\n");
79-
// console.log(lines);
8071
words = lines.flatMap((line) => line.split(" "));
8172

8273
return words.filter((word) => word.length > 0).length;
8374
}
8475

8576
function getLineCount(text) {
86-
let lines;
87-
return (lines = text.split("\n").length);
77+
return text.split("\n").length;
8878
}
89-
90-
// 1 4 20 sample-files/1.txt
91-
// 1 7 39 sample-files/2.txt
92-
// 5 24 125 sample-files/3.txt
93-
// 7 35 184 total

0 commit comments

Comments
 (0)