Skip to content

Commit 5cf24bb

Browse files
committed
wc implementation exercise
1 parent 996d79f commit 5cf24bb

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

implement-shell-tools/wc/mywc.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { program } from "commander";
2+
import { promises as fs } from "node:fs";
3+
4+
program
5+
.name("mywc")
6+
.description(
7+
"Counts lines, words, and bytes in files like the Unix wc command"
8+
)
9+
.option("-l", "counts the number of lines")
10+
.option("-w", "counts words")
11+
.option("-c", "counts bytes")
12+
.argument("<files...>", "Files to count");
13+
14+
program.parse();
15+
16+
const options = program.opts();
17+
const files = program.args;
18+
19+
// If there are no given flags, default to counting lines, words and bytes like the Unix wc command
20+
if (!options.l && !options.w && !options.c) {
21+
options.l = true;
22+
options.w = true;
23+
options.c = true;
24+
}
25+
26+
const showLines = options.l;
27+
const showWords = options.w;
28+
const showBytes = options.c;
29+
30+
// To support multiple files and a total
31+
let totalLines = 0;
32+
let totalWords = 0;
33+
let totalBytes = 0;
34+
35+
for (const file of files) {
36+
try {
37+
const content = await fs.readFile(file, "utf-8");
38+
39+
const lineCount = content.split("\n").length - 1;
40+
const wordCount = content.trim().split(/\s+/).filter(Boolean).length;
41+
const byteCount = Buffer.byteLength(content, "utf-8");
42+
43+
totalLines += lineCount;
44+
totalWords += wordCount;
45+
totalBytes += byteCount;
46+
47+
let output = "";
48+
if (showLines) output += `${lineCount.toString().padStart(8)}`;
49+
if (showWords) output += `${wordCount.toString().padStart(8)}`;
50+
if (showBytes) output += `${byteCount.toString().padStart(8)}`;
51+
output += ` ${file}`;
52+
53+
console.log(output);
54+
} catch (err) {
55+
console.error(`Error reading file ${file}: ${err.message}`);
56+
}
57+
}
58+
59+
// If multiple files were given, show the total
60+
if (files.length > 1) {
61+
let totalOutput = "";
62+
if (showLines) totalOutput += `${totalLines.toString().padStart(8)}`;
63+
if (showWords) totalOutput += `${totalWords.toString().padStart(8)}`;
64+
if (showBytes) totalOutput += `${totalBytes.toString().padStart(8)}`;
65+
totalOutput += " total";
66+
67+
console.log(totalOutput);
68+
}

implement-shell-tools/wc/package-lock.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "wc",
3+
"version": "1.0.0",
4+
"description": "You should already be familiar with the `wc` command line tool.",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"commander": "^14.0.0"
15+
}
16+
}

0 commit comments

Comments
 (0)