-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathwc.js
More file actions
61 lines (50 loc) · 2.05 KB
/
wc.js
File metadata and controls
61 lines (50 loc) · 2.05 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
import { program } from "commander";
import { promises as fs } from "node:fs";
//configuring
program
.name("wc")
.description("Print newline, word, and byte counts for each file")
.option("-l, --lines", "Print the newline counts")
.option("-w, --words", "Print the word counts")
.option("-c, --bytes", "Print the byte counts")
.argument("<files...>", "File paths to process")
//Interpret the program
program.parse()
const options = program.opts()
const files = program.args //Interpreting parsed data
//helper function to calculate all counts
async function getCounts(file){
const content = await fs.readFile(file, "utf-8");
const lineCount = content.split("\n").length -1;//split("\n") returns one more element than the actual number so length-1,
const wordCount = content.trim().split(/\s+/).length;
const byteCount = Buffer.byteLength(content, "utf-8"); //Calculate how many bytes the string uses in UTF-8 (important because some characters use more than 1 byte)
return { lineCount, wordCount, byteCount };
}
//helper to remove duplicated output logic
function formatOutput(lines, words, bytes, filename, options) {
let formattedOutput = "";
if (options.lines) formattedOutput += `${lines} `;
if (options.words) formattedOutput+= `${words} `;
if (options.bytes) formattedOutput += `${bytes} `;
if (!options.lines && !options.words && !options.bytes) { // default: print all three
formattedOutput += `${lines} ${words} ${bytes} `;
}
return formattedOutput + filename;
}
//Initiating totals
let totalLines = 0;
let totalWords = 0;
let totalBytes = 0;
for (const file of files) {
const { lineCount, wordCount, byteCount } = await getCounts(file);
totalLines += lineCount;
totalWords += wordCount;
totalBytes += byteCount;
const formatted = formatOutput(lineCount, wordCount, byteCount, file, options);
console.log(formatted);
}
//Print totals if multiple files
if (files.length > 1) {
const formattedTotals = formatOutput(totalLines, totalWords, totalBytes, "total", options);
console.log(formattedTotals);
}