Skip to content

Commit 3a859cc

Browse files
committed
refactor(wc): improve naming consistency and code clarity
- Rename parameters and variables for better descriptiveness: - files → filePaths - showAll → shouldShowAllStats - results → allFileStats - stats → fileStats - totals → grandTotals - label → displayName - s → stat (in reduce callbacks) - w → word (in filter callback) - output → outputColumns - format → formatColumn - printReport → printFormattedReport - Extract inline calculations to named variables in calculateFileStats() - lines, words, bytes for improved readability - Maintain all functionality and test coverage - No behavioral changes, purely refactoring for clarity
1 parent 4f5f3e6 commit 3a859cc

1 file changed

Lines changed: 29 additions & 26 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,59 @@ program
1111
.option("-l, --lines", "print the newline counts")
1212
.option("-w, --words", "print the word counts")
1313
.option("-c, --bytes", "print the byte counts")
14-
.action(async (files, options) => {
15-
// If no specific flags are provided, default to showing all
14+
.action(async (filePaths, options) => {
1615
const noFlagsProvided = !options.lines && !options.words && !options.bytes;
17-
const showAll = noFlagsProvided;
16+
const shouldShowAllStats = noFlagsProvided;
1817

19-
const results = [];
18+
const allFileStats = [];
2019

21-
for (const filePath of files) {
20+
for (const filePath of filePaths) {
2221
try {
23-
const stats = await calculateFileStats(filePath);
24-
results.push(stats);
25-
printReport(stats, options, showAll);
22+
const fileStats = await calculateFileStats(filePath);
23+
allFileStats.push(fileStats);
24+
printFormattedReport(fileStats, options, shouldShowAllStats);
2625
} catch (error) {
2726
console.error(`wc: ${filePath}: No such file or directory`);
2827
process.exitCode = 1;
2928
}
3029
}
3130

32-
if (results.length > 1) {
33-
const totals = {
34-
lineCount: results.reduce((sum, s) => sum + s.lineCount, 0),
35-
wordCount: results.reduce((sum, s) => sum + s.wordCount, 0),
36-
byteCount: results.reduce((sum, s) => sum + s.byteCount, 0),
37-
label: "total"
31+
if (allFileStats.length > 1) {
32+
const grandTotals = {
33+
lineCount: allFileStats.reduce((sum, stat) => sum + stat.lineCount, 0),
34+
wordCount: allFileStats.reduce((sum, stat) => sum + stat.wordCount, 0),
35+
byteCount: allFileStats.reduce((sum, stat) => sum + stat.byteCount, 0),
36+
displayName: "total"
3837
};
39-
printReport(totals, options, showAll);
38+
printFormattedReport(grandTotals, options, shouldShowAllStats);
4039
}
4140
});
4241

4342
async function calculateFileStats(filePath) {
4443
const fileBuffer = await fs.readFile(filePath);
4544
const fileContent = fileBuffer.toString();
4645

46+
const lines = fileContent.split("\n").length - 1;
47+
const words = fileContent.split(/\s+/).filter(word => word.length > 0).length;
48+
const bytes = fileBuffer.length;
49+
4750
return {
48-
lineCount: fileContent.split("\n").length - 1,
49-
wordCount: fileContent.split(/\s+/).filter(w => w.length > 0).length,
50-
byteCount: fileBuffer.length,
51-
label: filePath
51+
lineCount: lines,
52+
wordCount: words,
53+
byteCount: bytes,
54+
displayName: filePath
5255
};
5356
}
5457

55-
function printReport(stats, options, showAll) {
56-
const output = [];
57-
const format = (num) => String(num).padStart(4);
58+
function printFormattedReport(stats, options, shouldShowAllStats) {
59+
const outputColumns = [];
60+
const formatColumn = (count) => String(count).padStart(4);
5861

59-
if (showAll || options.lines) output.push(format(stats.lineCount));
60-
if (showAll || options.words) output.push(format(stats.wordCount));
61-
if (showAll || options.bytes) output.push(format(stats.byteCount));
62+
if (shouldShowAllStats || options.lines) outputColumns.push(formatColumn(stats.lineCount));
63+
if (shouldShowAllStats || options.words) outputColumns.push(formatColumn(stats.wordCount));
64+
if (shouldShowAllStats || options.bytes) outputColumns.push(formatColumn(stats.byteCount));
6265

63-
console.log(`${output.join("")} ${stats.label}`);
66+
console.log(`${outputColumns.join("")} ${stats.displayName}`);
6467
}
6568

6669
program.parse(process.argv);

0 commit comments

Comments
 (0)