-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathwc.js
More file actions
60 lines (49 loc) · 1.46 KB
/
wc.js
File metadata and controls
60 lines (49 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { promises as fs } from "node:fs";
const args = process.argv.slice(2);
console.log(args)
const showLines = args.includes("-l");
const showWords = args.includes("-w");
const showChars = args.includes("-c");
const paths = args.filter(arg => !arg.startsWith("-")) || ".";
console.log(paths)
// const direct = await fs.readdir(path);
// console.log(direct);
let totalLines = 0;
let totalWords = 0;
let totalchars = 0;
if(showLines){
for (const path of paths){
const content = await fs.readFile(path, "utf-8");
const lines = content.split("\n").length;
totalLines += lines;
}
console.log("line: ", totalLines);
}
else if(showWords){
for (const path of paths){
const content = await fs.readFile(path, "utf-8");
const words = content.split(/\s+/).filter(Boolean).length;
totalWords += words;
}
console.log("words: ", totalWords);
}
else if(showChars){
for (const path of paths){
const content = await fs.readFile(path, "utf-8");
const char = content.length;
totalchars += char;
}
console.log("chars", totalchars)
}
else{
for (const path of paths){
const content = await fs.readFile(path, "utf-8");
const lines = content.split("\n").length;
const words = content.split(/\s+/).filter(Boolean).length;
const char = content.length;
totalLines += lines;
totalWords += words;
totalchars += char;
}
console.log("lines: ", totalLines, " words", totalWords, " char:", totalchars)
}