Skip to content

Commit 99dfd5d

Browse files
committed
implementing basic wc
1 parent de2cbfb commit 99dfd5d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

implement-shell-tools/wc/wc.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { promises as fs } from "node:fs";
2+
async function wcJsImplement() {
3+
let totalLines=0,totalWords=0,TotalBytes=0;
4+
let words;
5+
const commandLineArray=process.argv.slice(2);
6+
console.log(commandLineArray);
7+
for(const file of commandLineArray){
8+
const fileContent=await fs.readFile(file , 'utf-8');
9+
const lines=fileContent.split(/\r?\n/);
10+
let linesCount,wordsCount=0;
11+
//Calculate count of lines
12+
if(lines[lines.length-1].trim()===""){
13+
lines.pop();
14+
linesCount=lines.length;
15+
totalLines += linesCount;
16+
}
17+
//calculate count of words
18+
for(const line of lines){
19+
words=line.trim().split(/\s+/);
20+
if (words[0].trim() !== ""){
21+
wordsCount += words.length;
22+
}
23+
}
24+
totalWords = +wordsCount;
25+
26+
console.log(file);
27+
console.log(fileContent)
28+
console.log(lines);
29+
console.log(linesCount);
30+
console.log(totalLines);
31+
console.log(words);
32+
console.log(wordsCount);
33+
console.log(totalWords);
34+
}
35+
}
36+
wcJsImplement();

0 commit comments

Comments
 (0)