-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwc.js
More file actions
95 lines (85 loc) · 2.28 KB
/
wc.js
File metadata and controls
95 lines (85 loc) · 2.28 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { program } from "commander";
import { promises as fs } from "node:fs";
program
.name("count-containing-words")
.description("Counts words in a file that contain a particular character")
.option(
"-l",
"The number of lines in each input file is written to the standard output."
)
.option(
"-w",
"The number of words in each input file is written to the standard output."
)
.option(
"-c",
"The number of bytes in each input file is written to the standard output."
)
.argument("<path...>", "The file path to process")
.parse();
const argv = program.args;
const opts = program.opts();
const total = [];
const output = [];
const countsPerFile = [];
let columnWidth = 0;
const flag_c = (content) => {
return Buffer.byteLength(content, "utf8");
};
const flag_w = (content) => {
return content.match(/\b[\w']+\b/g).length;
};
const flag_l = (content) => {
return content.split("\n").length - 1;
};
const countAndDisplay = async (path) => {
const outputPerFile = [];
const content = await fs.readFile(path, "utf-8");
if (opts["l"]) {
outputPerFile.push(flag_l(content));
}
if (opts["w"]) {
outputPerFile.push(flag_w(content));
}
if (opts["c"]) {
outputPerFile.push(flag_c(content));
}
if (argv.length > 1) {
if (total.length == 0) {
total.push(...outputPerFile);
} else {
for (let index = 0; index < outputPerFile.length; index++) {
total[index] += outputPerFile[index];
}
}
countsPerFile.push(...outputPerFile);
}
outputPerFile.push(path);
output.push([...outputPerFile]);
};
const handleInput = async () => {
if (Object.keys(opts).length == 0) {
["l", "w", "c"].forEach((key) => (opts[key] = true));
}
for (const path of argv) {
await countAndDisplay(path);
}
const numOfColumns = Object.keys(opts).length;
if (argv.length > 1) {
total.push("total");
output.push(total);
}
for (const row of output) {
for (let i = 0; i < numOfColumns; i++) {
columnWidth = Math.max(columnWidth, String(row[i]).length);
}
}
for (let row of output) {
const line_parts = [];
for (let i = 0; i < numOfColumns; i++) {
line_parts.push(String(row[i]).padStart(columnWidth + 4));
}
console.log(line_parts.join(" "), row.at(-1));
}
};
handleInput();