Skip to content

Commit fc1680b

Browse files
committed
task: complete implementation of wc in javascript
1 parent 8ab9ff3 commit fc1680b

1 file changed

Lines changed: 142 additions & 0 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.mjs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import process from "node:process";
2+
import fs from "node:fs";
3+
import { parseArgs } from "node:util";
4+
5+
const options = {
6+
lines: {
7+
type: "boolean",
8+
short: "l",
9+
},
10+
words: {
11+
type: "boolean",
12+
short: "w",
13+
},
14+
bytes: {
15+
type: "boolean",
16+
short: "c",
17+
},
18+
};
19+
20+
const { values, positionals } = parseArgs({
21+
options,
22+
allowPositionals: true,
23+
});
24+
25+
if (positionals.length === 0) {
26+
process.stderr.write("Please provide a file.\n");
27+
process.exit(1);
28+
}
29+
30+
const results = [];
31+
32+
// read file and calculate counts
33+
for (const path of positionals) {
34+
const content = fs.readFileSync(path, "utf-8");
35+
36+
const lineCount = [...content].filter((char) => char === "\n").length;
37+
38+
const trimmedContent = content.trim();
39+
40+
const wordCount =
41+
trimmedContent === "" ? 0 : trimmedContent.split(/\s+/).length;
42+
43+
const byteCount = Buffer.byteLength(content);
44+
45+
results.push({
46+
path,
47+
lineCount,
48+
wordCount,
49+
byteCount,
50+
});
51+
}
52+
53+
let totalLines = 0;
54+
let totalWords = 0;
55+
let totalBytes = 0;
56+
57+
// calculate total counts
58+
for (const result of results) {
59+
totalLines += result.lineCount;
60+
totalWords += result.wordCount;
61+
totalBytes += result.byteCount;
62+
}
63+
64+
const allCounts = [];
65+
66+
for (const result of results) {
67+
const counts = getSelectedCounts(
68+
result.lineCount,
69+
result.wordCount,
70+
result.byteCount,
71+
);
72+
73+
allCounts.push(...counts);
74+
}
75+
76+
if (results.length > 1) {
77+
const totalCounts = getSelectedCounts(totalLines, totalWords, totalBytes);
78+
79+
allCounts.push(...totalCounts);
80+
}
81+
82+
// flags handling function
83+
function getSelectedCounts(lineCount, wordCount, byteCount) {
84+
const counts = [];
85+
86+
if (values.lines) {
87+
counts.push(lineCount);
88+
}
89+
90+
if (values.words) {
91+
counts.push(wordCount);
92+
}
93+
94+
if (values.bytes) {
95+
counts.push(byteCount);
96+
}
97+
98+
if (counts.length === 0) {
99+
counts.push(lineCount, wordCount, byteCount);
100+
}
101+
102+
return counts;
103+
}
104+
105+
// output formatting
106+
let largestByteCount = 0;
107+
108+
for (const result of results) {
109+
if (result.byteCount > largestByteCount) {
110+
largestByteCount = result.byteCount;
111+
}
112+
}
113+
114+
if (results.length > 1 && totalBytes > largestByteCount) {
115+
largestByteCount = totalBytes;
116+
}
117+
118+
const width = String(largestByteCount).length;
119+
120+
for (const result of results) {
121+
const counts = getSelectedCounts(
122+
result.lineCount,
123+
result.wordCount,
124+
result.byteCount,
125+
);
126+
127+
const formattedCounts = counts
128+
.map((count) => String(count).padStart(width))
129+
.join(" ");
130+
131+
process.stdout.write(`${formattedCounts} ${result.path}\n`);
132+
}
133+
134+
if (results.length > 1) {
135+
const totalCounts = getSelectedCounts(totalLines, totalWords, totalBytes);
136+
137+
const formattedTotals = totalCounts
138+
.map((count) => String(count).padStart(width))
139+
.join(" ");
140+
141+
process.stdout.write(`${formattedTotals} total\n`);
142+
}

0 commit comments

Comments
 (0)