Skip to content

Commit 9bba89c

Browse files
committed
Refactor wc.js to improve code clarity and reduce redundancy
- Remove redundant noFlagsProvided variable - Use shouldShowAllStats directly in action handler - Restructure printFormattedReport logic with if-else block - Replace repetitive "shouldShowAllStats ||" conditions with cleaner conditional - Improves readability: shows all stats in one block, then handles individual flags
1 parent 2e77d1e commit 9bba89c

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ program
1212
.option("-w, --words", "print the word counts")
1313
.option("-c, --bytes", "print the byte counts")
1414
.action(async (filePaths, options) => {
15-
const noFlagsProvided = !options.lines && !options.words && !options.bytes;
16-
const shouldShowAllStats = noFlagsProvided;
15+
const shouldShowAllStats = !options.lines && !options.words && !options.bytes;
1716

1817
const allFileStats = [];
1918

@@ -59,9 +58,15 @@ function printFormattedReport(stats, options, shouldShowAllStats) {
5958
const outputColumns = [];
6059
const formatColumn = (count) => String(count).padStart(4);
6160

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));
61+
if (shouldShowAllStats) {
62+
outputColumns.push(formatColumn(stats.lineCount));
63+
outputColumns.push(formatColumn(stats.wordCount));
64+
outputColumns.push(formatColumn(stats.byteCount));
65+
} else {
66+
if (options.lines) outputColumns.push(formatColumn(stats.lineCount));
67+
if (options.words) outputColumns.push(formatColumn(stats.wordCount));
68+
if (options.bytes) outputColumns.push(formatColumn(stats.byteCount));
69+
}
6570

6671
console.log(`${outputColumns.join("")} ${stats.displayName}`);
6772
}

0 commit comments

Comments
 (0)