Skip to content

Commit a365602

Browse files
committed
all done and refactored
1 parent 84aee2d commit a365602

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

implement-shell-tools/cat/cat.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { program } from "commander";
22
import{promises as fs} from "node:fs";
3-
import process from "node:process";
3+
44

55
program
66
.name("cat")
77
.description("displays the contents of a file")
8-
.option('-n, --number', 'Number all output lines')
9-
.option("-b, --number-nonblank", 'Number non-blank output lines only')
8+
.option("-n, --number", "Number all output lines")
9+
.option("-b, --number-nonblank", "Number non-blank output lines only")
1010
.argument("<filepath>");
1111

1212
program.parse();

implement-shell-tools/ls/ls.mjs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {program} from "commander";
22
import {promises as fs} from "node:fs";
3-
import { readdir } from "node:fs";
43

54
program
65
.name("ls")
@@ -20,15 +19,15 @@ if (args.length === 0) {
2019
}
2120

2221
const path = args[0];
23-
const files = await fs.readdir(path, "utf-8");
22+
const files = await fs.readdir(path);
2423

2524
let visibleFiles = files;
2625
if (!opts.all) {
27-
visibleFiles = files.filter(file => !file.startsWith('.'));
26+
visibleFiles = files.filter(file => !file.startsWith("."));
2827
}
2928

3029
if (opts.one) {
3130
visibleFiles.forEach(file => console.log(file));
3231
} else {
33-
console.log(visibleFiles.join(' '));
32+
console.log(visibleFiles.join(" "));
3433
}

implement-shell-tools/wc/wc.mjs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,36 @@ if(args.length === 0){
2020
}
2121

2222
const path = args[0];
23-
const content = await fs.readFile(path, "utf-8")
23+
const content = await fs.readFile(path, "utf-8");
2424

2525

2626
// function to count the number of lines in a files
2727
function countLines(content){
28-
const lines = content.split(/\r?\n/)
29-
return lines.length
28+
const lines = content.split(/\r?\n/);
29+
return lines.length;
3030
}
3131

3232
//function to count the words
3333
function countWords(content){
3434
const words = content.trim().split(/\s+/);
35-
return words.length
35+
return words.length;
3636
}
3737

3838
//function to count the bytes
3939
function countBytes(content){
4040
return Buffer.byteLength(content, "utf-8");
4141
}
4242

43-
if (opts.line){
44-
console.log(countLines(content))
45-
}else if(opts.words){console.log(countWords(content), path)}
46-
else if(opts.bytes){console.log(countBytes(content), path)}
47-
else{
48-
console.log(countLines(content), countWords(content), countBytes(content), path)
49-
}
50-
51-
43+
const lines = countLines(content);
44+
const words = countWords(content);
45+
const bytes = countBytes(content);
46+
47+
if (opts.line) {
48+
console.log(lines);
49+
} else if (opts.words) {
50+
console.log(words, path);
51+
} else if (opts.bytes) {
52+
console.log(bytes, path);
53+
} else {
54+
console.log(lines, words, bytes, path);
55+
}

0 commit comments

Comments
 (0)