Skip to content

Commit 668b8ce

Browse files
Start basic wc implementation
1 parent 6cedb45 commit 668b8ce

File tree

1 file changed

+30
-0
lines changed
  • implement-shell-tools/wc

1 file changed

+30
-0
lines changed

implement-shell-tools/wc/wc.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { program } from "commander";
2+
import { promises as fs } from "node:fs";
3+
4+
//config the program
5+
program
6+
.name("wc")
7+
.description(
8+
"The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input"
9+
)
10+
.argument("<path>", "The file path to process");
11+
12+
//interpret the program
13+
program.parse();
14+
15+
//use the parsed data
16+
const path = program.args[0];
17+
18+
//read the file
19+
const content = await fs.readFile(path, "utf-8");
20+
21+
//count lines
22+
const lines = content.split("\n").length - 1;
23+
24+
//count words split by any whitespace
25+
const words = content.split(/\s+/).filter((word) => word.length > 0).length;
26+
27+
//count character
28+
const characters = content.length;
29+
30+
console.log(`${lines} ${words} ${characters} ${path}`);

0 commit comments

Comments
 (0)