Skip to content

Commit d1a86b0

Browse files
wc is done but the readme file does not syas if we need to do it with multiple flags
1 parent 9ee1b40 commit d1a86b0

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

implement-shell-tools/wc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ It must act the same as `wc` would, if run from the directory containing this RE
1414

1515
Matching any additional behaviours or flags are optional stretch goals.
1616

17-
We recommend you start off supporting no flags for one file, then add support for multiple files, then add support for the flags.
17+
We recommend you start off supporting no flags for one file, then add support for multiple files, then add support for the flags

implement-shell-tools/wc/wc.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { promises as fs } from "node:fs";
2+
3+
const args = process.argv.slice(2);
4+
console.log(args)
5+
6+
const showLines = args.includes("-l");
7+
const showWords = args.includes("-w");
8+
const showChars = args.includes("-c");
9+
const paths = args.filter(arg => !arg.startsWith("-")) || ".";
10+
console.log(paths)
11+
12+
// const direct = await fs.readdir(path);
13+
14+
// console.log(direct);
15+
let totalLines = 0;
16+
let totalWords = 0;
17+
let totalchars = 0;
18+
19+
if(showLines){
20+
for (const path of paths){
21+
const content = await fs.readFile(path, "utf-8");
22+
const lines = content.split("\n").length;
23+
24+
totalLines += lines;
25+
}
26+
console.log("line: ", totalLines);
27+
}
28+
else if(showWords){
29+
for (const path of paths){
30+
const content = await fs.readFile(path, "utf-8");
31+
const words = content.split(/\s+/).filter(Boolean).length;
32+
33+
totalWords += words;
34+
}
35+
console.log("words: ", totalWords);
36+
}
37+
else if(showChars){
38+
for (const path of paths){
39+
const content = await fs.readFile(path, "utf-8");
40+
const char = content.length;
41+
42+
totalchars += char;
43+
}
44+
console.log("chars", totalchars)
45+
}
46+
else{
47+
for (const path of paths){
48+
const content = await fs.readFile(path, "utf-8");
49+
const lines = content.split("\n").length;
50+
const words = content.split(/\s+/).filter(Boolean).length;
51+
const char = content.length;
52+
53+
totalLines += lines;
54+
totalWords += words;
55+
totalchars += char;
56+
}
57+
console.log("lines: ", totalLines, " words", totalWords, " char:", totalchars)
58+
}
59+
60+

0 commit comments

Comments
 (0)