-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (64 loc) · 2.17 KB
/
Copy pathindex.js
File metadata and controls
77 lines (64 loc) · 2.17 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import process from "node:process";
import { promises as fs } from "node:fs";
//from coursework
// const content = await fs.readFile(path, "utf-8");
// const countOfWordsContainingEs = content
// .split(" ")
// .filter((word) => word.includes("e"))
// .length;
// console.log(countOfWordsContainingEs);
function countHelper(inputFiles) {
return {
lines: inputFiles.split('\n').length - 1,
words: inputFiles.split(' ').filter(w => w !== "").length,
bytes: inputFiles.length,
};
}
// * `wc -l sample-files/3.txt`
// * `wc -l sample-files/*`
async function countLines(listOfFiles) {
for (const file of listOfFiles) {
const content = await fs.readFile(file, "utf-8");
// const linesNumbered = content.split('\n').length-1
const counts = countHelper(content);
console.log(`${counts.lines} ${file}`)
}
}
// * `wc -w sample-files/3.txt`
async function countWords(listOfFiles) {
for (const file of listOfFiles) {
const content = await fs.readFile(file, "utf-8");
// const wordsCounted = content.split(" ").filter(word => word !== "").length;
// console.log(`${wordsCounted} ${file}`);
const counts = countHelper(content);
console.log(`${counts.words} ${file}`);
}
}
// * `wc -c sample-files/3.txt`
async function countBytes(listOfFiles) {
for (const file of listOfFiles) {
const content = await fs.readFile(file, "utf-8");
// const bytesCounted = content.length;
const counts = countHelper(content);
console.log(`${counts.bytes} ${file}`);
}
}
// * `wc sample-files/*`
async function countAll(listOfFiles) {
for (const file of listOfFiles) {
const content = await fs.readFile(file, "utf-8");
const counts = countHelper(content)
console.log(`${counts.lines} ${counts.words} ${counts.bytes} ${file}`);
}
}
const argv = process.argv.slice(2);
const files = argv.filter(arg => !arg.startsWith('-'));
if (argv.includes('-l')) {
await countLines(files);
} else if (argv.includes('-w')) {
await countWords(files);
} else if (argv.includes('-c')) {
await countBytes(files);
} else {
await countAll(files);
}