Skip to content

Commit 0c384d7

Browse files
Restructuring code to use object for data/output
1 parent 2d17c59 commit 0c384d7

1 file changed

Lines changed: 49 additions & 26 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.js

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,75 @@ import { program } from "commander";
22
import { promises as fs } from "node:fs";
33

44
program
5-
.name("node-cat")
6-
.description("A Node.js implementation of the Unix cat command")
7-
.option("-n, --number", "Number all output lines")
8-
.option(
9-
"-b, --numberNonBlank",
10-
"Numbers only non-empty lines. Overrides -n option"
11-
)
5+
.name("node-wc")
6+
.description("A Node.js implementation of the Unix wc command")
7+
.option("-l, --lines", "Print the newline counts")
8+
.option("-w, --words", "Print the word counts")
9+
.option("-c, --bytes", "Print the byte counts")
1210
.argument("<path...>", "The file path to process");
1311

1412
program.parse();
1513

1614
const paths = program.args;
17-
const { number, numberNonBlank } = program.opts();
15+
const { lines, words, bytes } = program.opts();
16+
17+
const showAll = !lines && !words && !bytes;
1818

1919
// --- Read files and sizes ---
2020
let content = "";
21-
let fileSize,
22-
wordCount,
23-
lineCount,
24-
lineCountTotal = 0,
21+
let output = [];
22+
23+
let lineCountTotal = 0,
2524
wordCountTotal = 0,
2625
fileSizeTotal = 0;
2726

27+
if (Object.keys(program.opts()).length === 1) {
28+
}
29+
2830
for (const path of paths) {
31+
let fileStats;
32+
let data = {};
33+
2934
content = await fs.readFile(path, "utf-8");
3035
if (content.endsWith("\n")) {
3136
content = content.slice(0, -1);
3237
}
33-
fileSize = await fs.stat(path);
34-
fileSizeTotal += fileSize.size;
35-
wordCount = getWordCount(content);
36-
wordCountTotal += wordCount;
37-
lineCount = getLineCount(content);
38-
lineCountTotal += lineCount;
38+
39+
data.lineCount = getLineCount(content);
40+
lineCountTotal += data.lineCount;
41+
42+
data.wordCount = getWordCount(content);
43+
wordCountTotal += data.wordCount;
44+
45+
fileStats = await fs.stat(path);
46+
data.fileSize = fileStats.size;
47+
fileSizeTotal += data.fileSize;
48+
49+
data.path = path;
50+
output.push(data);
51+
}
52+
53+
console.log(output);
54+
55+
if (paths.length > 1) {
3956
console.log(
40-
`${String(lineCount).padStart(3)}${String(wordCount).padStart(4)}${String(
41-
fileSize.size
42-
).padStart(4)} ${path}`
57+
`${String(lineCountTotal).padStart(3)}${String(wordCountTotal).padStart(
58+
4
59+
)}${String(fileSizeTotal).padStart(4)} total`
4360
);
4461
}
4562

46-
console.log(
47-
`${String(lineCountTotal).padStart(3)}${String(wordCountTotal).padStart(
48-
4
49-
)}${String(fileSizeTotal).padStart(4)} total`
50-
);
63+
// output.push(String(fileSize.size).padStart(4));
64+
// console.log(`${output.join("")} ${path}`);
65+
66+
function formatOutput({ lineCount, wordCount, fileSize, path }) {
67+
let output = [];
68+
if (lines || showAll) output.push(String(lineCount).padStart(3));
69+
if (words || showAll) output.push(String(wordCount).padStart(4));
70+
if (bytes || showAll) output.push(String(fileSize).padStart(4));
71+
72+
return `${output.join("")} ${path}`;
73+
}
5174

5275
function getWordCount(text) {
5376
let words, lines;

0 commit comments

Comments
 (0)