Skip to content

Commit 5ede2e8

Browse files
committed
implemented cat, ls, wc with java script
1 parent 3cb1e27 commit 5ede2e8

3 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require("fs");
4+
5+
const args = process.argv.slice(2);
6+
7+
let numberLines = false;
8+
let numberNonEmpty = false;
9+
10+
if (args[0] === "-n") {
11+
numberLines = true;
12+
args.shift();
13+
} else if (args[0] === "-b") {
14+
numberNonEmpty = true;
15+
args.shift();
16+
}
17+
let count = 1;
18+
19+
args.forEach((file) => {
20+
const content = fs.readFileSync(file, "utf-8");
21+
const lines = content.split(/\r?\n/);
22+
23+
24+
if (numberLines){
25+
lines.forEach((line) => {
26+
console.log(`${count} ${line}`);
27+
count++;
28+
});
29+
} else if (numberNonEmpty){
30+
lines.forEach((line) => {
31+
if (line !== "") {
32+
console.log(`${count} ${line}`);
33+
count++;
34+
} else {
35+
console.log("");
36+
}
37+
});
38+
} else {
39+
console.log(content);
40+
}
41+
});

implement-shell-tools/ls/my-ls.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require("node:fs");
4+
const path = require("node:path");
5+
6+
function parseArgs(args) {
7+
let showAll = false;
8+
let targetDir = ".";
9+
10+
for (const arg of args) {
11+
if (arg === "-a") {
12+
showAll = true;
13+
} else if (!arg.startsWith("-")) {
14+
targetDir = arg;
15+
}
16+
}
17+
18+
return { showAll, targetDir };
19+
}
20+
21+
function listDirectory(dirPath, showAll) {
22+
let files = fs.readdirSync(dirPath);
23+
24+
if (!showAll) {
25+
files = files.filter(file => !file.startsWith("."));
26+
}
27+
28+
return files.sort();
29+
}
30+
31+
function main() {
32+
const args = process.argv.slice(2);
33+
const { showAll, targetDir } = parseArgs(args);
34+
35+
const files = listDirectory(targetDir, showAll);
36+
console.log(files.join("\n"));
37+
}
38+
39+
main();
40+
41+

implement-shell-tools/wc/my-wc.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require("node:fs");
4+
5+
function countFile(filePath) {
6+
const content = fs.readFileSync(filePath, "utf8");
7+
8+
const lines = content.split("\n").length - 1;
9+
const words = content.trim() ? content.trim().split(/\s+/).length : 0;
10+
const chars = Buffer.byteLength(content, "utf8");
11+
12+
return { lines, words, chars };
13+
}
14+
15+
function main() {
16+
const args = process.argv.slice(2);
17+
18+
let flag = null;
19+
let files = [];
20+
21+
for (const arg of args) {
22+
if (arg === "-l" || arg === "-w" || arg === "-c") {
23+
flag = arg;
24+
} else {
25+
files.push(arg);
26+
}
27+
}
28+
29+
let totalLines = 0;
30+
let totalWords = 0;
31+
let totalChars = 0;
32+
33+
for (const file of files) {
34+
const { lines, words, chars } = countFile(file);
35+
36+
totalLines += lines;
37+
totalWords += words;
38+
totalChars += chars;
39+
40+
if (flag === "-l") {
41+
console.log(`${lines} ${file}`);
42+
} else if (flag === "-w") {
43+
console.log(`${words} ${file}`);
44+
} else if (flag === "-c") {
45+
console.log(`${chars} ${file}`);
46+
} else {
47+
console.log(`${lines} ${words} ${chars} ${file}`);
48+
}
49+
}
50+
51+
if (files.length > 1) {
52+
if (flag === "-l") {
53+
console.log(`${totalLines} total`);
54+
} else if (flag === "-w") {
55+
console.log(`${totalWords} total`);
56+
} else if (flag === "-c") {
57+
console.log(`${totalChars} total`);
58+
} else {
59+
console.log(`${totalLines} ${totalWords} ${totalChars} total`);
60+
}
61+
}
62+
}
63+
64+
main();
65+
66+
67+

0 commit comments

Comments
 (0)