Skip to content

Commit 17f2c7e

Browse files
committed
implementing flags -l , -c , -w
1 parent 303b96f commit 17f2c7e

File tree

1 file changed

+43
-3
lines changed
  • implement-shell-tools/wc

1 file changed

+43
-3
lines changed

implement-shell-tools/wc/wc.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import { promises as fs } from "node:fs";
22
async function wcJsImplement() {
3+
34
let totalLines=0,totalWords=0,TotalBytes=0;
45
let words,bytes;
56
const commandLineArray=process.argv.slice(2);
6-
//console.log(commandLineArray);
7+
const flags = commandLineArray.filter((item) => item.startsWith("-"));
8+
9+
if(flags.length !==0 )
10+
{
11+
commandLineArray.shift();
12+
}
13+
714
for(const file of commandLineArray){
815
const fileContent=await fs.readFile(file , 'utf-8');
916
const fileBuffer=await fs.readFile(file);
@@ -26,9 +33,42 @@ async function wcJsImplement() {
2633
}
2734
}
2835
totalWords += wordsCount;
29-
console.log(`${String(linesCount).padStart(4)}${String(wordsCount).padStart(4)}${String(bytes).padStart(4)} ${file}`);
36+
37+
switch (flags[0]) {
38+
case "-l":
39+
console.log(`${String(linesCount).padStart(4)} ${file}`);
40+
break;
41+
case "-w":
42+
console.log(`${String(wordsCount).padStart(4)} ${file}`);
43+
break;
44+
case "-c":
45+
console.log(`${String(bytes).padStart(4)} ${file}`);
46+
break;
47+
default:
48+
console.log(`${String(linesCount).padStart(4)}${String(wordsCount).padStart(4)}${String(bytes).padStart(4)} ${file}`);
49+
}
3050

51+
52+
}
53+
if(commandLineArray.length>1){
54+
switch (flags[0]) {
55+
case "-l":
56+
console.log(`${String(totalLines).padStart(4)} total`);
57+
break;
58+
case "-w":
59+
console.log(`${String(totalWords).padStart(4)} total`);
60+
break;
61+
case "-c":
62+
console.log(`${String(TotalBytes).padStart(4)} total`);
63+
break;
64+
default :
65+
console.log(
66+
`${String(totalLines).padStart(4)}${String(totalWords).padStart(
67+
4
68+
)}${String(TotalBytes).padStart(4)} total`
69+
);
70+
}
3171
}
32-
console.log(`${String(totalLines).padStart(4)}${String(totalWords).padStart(4)}${String(TotalBytes).padStart(4)} total`);
72+
3373
}
3474
wcJsImplement();

0 commit comments

Comments
 (0)