Skip to content

Commit 0353c8e

Browse files
committed
made corrections to the ls and wc implementation
1 parent 5cf24bb commit 0353c8e

2 files changed

Lines changed: 24 additions & 24 deletions

File tree

implement-shell-tools/ls/myls.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,25 @@ const directory = program.args[0] || ".";
1717
try {
1818
let files = await fs.readdir(directory);
1919

20-
// if "-a" is used, include hidden files; those that start with "."
20+
// if "-a" is used, include hidden files; those that start with "."
2121
if (options.a) {
2222
files = [".", "..", ...files];
2323
}
2424

25-
for (const file of files) {
26-
// if "-a" is not used, skip hidden files; those that start with "."
27-
if (!options.a && file.startsWith(".")) {
28-
continue;
29-
}
25+
// If "-a" is not used, filter hidden files out
26+
files = files.filter((file) => options.a || !file.startsWith("."));
27+
28+
// Sort alphabetically
29+
files.sort();
3030

31-
console.log(file); // print file name; one file per line
31+
if (options["1"]) {
32+
// Print one file per line
33+
for (const file of files) {
34+
console.log(file);
35+
}
36+
} else {
37+
// Print all files on a single line, separated by spaces
38+
console.log(files.join(" "));
3239
}
3340
} catch (error) {
3441
console.error(`Error reading directory ${directory}:`, error.message);

implement-shell-tools/wc/mywc.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ if (!options.l && !options.w && !options.c) {
2323
options.c = true;
2424
}
2525

26-
const showLines = options.l;
27-
const showWords = options.w;
28-
const showBytes = options.c;
26+
function calculateOutput(lines, words, bytes, label) {
27+
let output = "";
28+
if (options.l) output += `${lines.toString().padStart(8)}`;
29+
if (options.w) output += `${words.toString().padStart(8)}`;
30+
if (options.c) output += `${bytes.toString().padStart(8)}`;
31+
if (label) output += ` ${label}`;
32+
return output;
33+
}
2934

3035
// To support multiple files and a total
3136
let totalLines = 0;
@@ -44,25 +49,13 @@ for (const file of files) {
4449
totalWords += wordCount;
4550
totalBytes += byteCount;
4651

47-
let output = "";
48-
if (showLines) output += `${lineCount.toString().padStart(8)}`;
49-
if (showWords) output += `${wordCount.toString().padStart(8)}`;
50-
if (showBytes) output += `${byteCount.toString().padStart(8)}`;
51-
output += ` ${file}`;
52-
53-
console.log(output);
52+
console.log(calculateOutput(lineCount, wordCount, byteCount, file));
5453
} catch (err) {
5554
console.error(`Error reading file ${file}: ${err.message}`);
5655
}
5756
}
5857

5958
// If multiple files were given, show the total
6059
if (files.length > 1) {
61-
let totalOutput = "";
62-
if (showLines) totalOutput += `${totalLines.toString().padStart(8)}`;
63-
if (showWords) totalOutput += `${totalWords.toString().padStart(8)}`;
64-
if (showBytes) totalOutput += `${totalBytes.toString().padStart(8)}`;
65-
totalOutput += " total";
66-
67-
console.log(totalOutput);
60+
console.log(calculateOutput(totalLines, totalWords, totalBytes, "total"));
6861
}

0 commit comments

Comments
 (0)