-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathwc.js
More file actions
63 lines (51 loc) · 1.7 KB
/
wc.js
File metadata and controls
63 lines (51 loc) · 1.7 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
import { program } from "commander";
import { promises as fs } from "node:fs";
program
.name("wc command")
.description("Implement wc command to count words and lines")
.option("-l, --lines", "show number of lines only")
.option("-w, --words", "show number of words only")
.option("-c, --chars", "show number of characters only")
.argument("[paths...]", "The file path to process");
program.parse(process.argv);
const opts = program.opts();
let paths = program.args;
if (paths.length === 0) {
paths = ["."];
}
let totals = { lines: 0, words: 0, chars: 0 };
for (const filePath of paths) {
let content;
try {
content = await fs.readFile(filePath, "utf8");
} catch (err) {
console.error(`wc: cannot read file "${filePath}": ${err.message}`);
continue;
}
const lineCount = content.split("\n").length;
const wordCount = content.split(/\s+/).filter(Boolean).length;
const charCount = content.length;
totals.lines += lineCount;
totals.words += wordCount;
totals.chars += charCount;
const output = [];
if (!opts.lines && !opts.words && !opts.chars) {
output.push(lineCount, wordCount, charCount);
} else {
if (opts.lines) output.push(lineCount);
if (opts.words) output.push(wordCount);
if (opts.chars) output.push(charCount);
}
console.log(`${output.join(" ")} ${filePath}`);
}
if (paths.length > 1) {
const totalOutput = [];
if (!opts.lines && !opts.words && !opts.chars) {
totalOutput.push(totals.lines, totals.words, totals.chars);
} else {
if (opts.lines) totalOutput.push(totals.lines);
if (opts.words) totalOutput.push(totals.words);
if (opts.chars) totalOutput.push(totals.chars);
}
console.log(`${totalOutput.join(" ")} total`);
}