Skip to content

Commit 8566cc4

Browse files
committed
Complete Module Tools: ls, cat, wc implementations
Complete Module Tools: ls, cat, wc implementations
1 parent fa3da24 commit 8566cc4

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

implement-shell-tools/cat/cat.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const fs = require("fs");
2+
3+
const args = process.argv.slice(2);
4+
5+
const showN = args.includes("-n");
6+
const showB = args.includes("-b");
7+
8+
const files = args.filter(a => !a.startsWith("-"));
9+
10+
let content = files.map(f => fs.readFileSync(f, "utf8")).join("");
11+
12+
let lines = content.split("\n");
13+
14+
// remove ONLY final empty line caused by trailing newline
15+
if (lines[lines.length - 1] === "") {
16+
lines.pop();
17+
}
18+
19+
let output = [];
20+
21+
if (showN) {
22+
let i = 1;
23+
for (const line of lines) {
24+
output.push(`${String(i).padStart(6)} ${line}`);
25+
i++;
26+
}
27+
} else if (showB) {
28+
let i = 1;
29+
for (const line of lines) {
30+
if (line !== "") {
31+
output.push(`${String(i).padStart(6)} ${line}`);
32+
i++;
33+
} else {
34+
output.push("");
35+
}
36+
}
37+
} else {
38+
output = lines;
39+
}
40+
41+
// IMPORTANT: add final newline like real cat
42+
process.stdout.write(output.join("\n") + "\n");

implement-shell-tools/wc/wc.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const fs = require("fs");
2+
3+
const args = process.argv.slice(2);
4+
5+
const showLines = args.includes("-l");
6+
const showWords = args.includes("-w");
7+
const showBytes = args.includes("-c");
8+
9+
const files = args.filter(a => !a.startsWith("-"));
10+
11+
function getStats(text) {
12+
return {
13+
lines: text.split("\n").length - 1,
14+
words: text.trim().split(/\s+/).filter(Boolean).length,
15+
bytes: Buffer.byteLength(text)
16+
};
17+
}
18+
19+
function format(num) {
20+
return String(num).padStart(8);
21+
}
22+
23+
let totalLines = 0;
24+
let totalWords = 0;
25+
let totalBytes = 0;
26+
27+
files.forEach(file => {
28+
const content = fs.readFileSync(file, "utf8");
29+
const stats = getStats(content);
30+
31+
totalLines += stats.lines;
32+
totalWords += stats.words;
33+
totalBytes += stats.bytes;
34+
35+
let output = [];
36+
37+
if (showLines) output.push(format(stats.lines));
38+
else if (showWords) output.push(format(stats.words));
39+
else if (showBytes) output.push(format(stats.bytes));
40+
else output.push(format(stats.lines), format(stats.words), format(stats.bytes));
41+
42+
output.push(file);
43+
44+
console.log(output.join(" "));
45+
});
46+
47+
if (files.length > 1) {
48+
let output = [];
49+
50+
if (showLines) output.push(format(totalLines));
51+
else if (showWords) output.push(format(totalWords));
52+
else if (showBytes) output.push(format(totalBytes));
53+
else output.push(format(totalLines), format(totalWords), format(totalBytes));
54+
55+
output.push("total");
56+
57+
console.log(output.join(" "));
58+
}

0 commit comments

Comments
 (0)