Skip to content

Commit d4720b1

Browse files
committed
added comments to ls
1 parent fa7436b commit d4720b1

2 files changed

Lines changed: 38 additions & 19 deletions

File tree

‎implement-shell-tools/cat/cat.js‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,25 @@ function executeFlags() {
4747
}
4848

4949
function bFlag() {
50-
return allFilesContents.map((fileContent) => {
50+
const resultFiles = [];
51+
52+
for (const fileContent of allFilesContents) {
53+
const formattedLines = [];
5154
let lineNumber = 1;
52-
return fileContent.map((line, index) => {
53-
if (line == "") {
54-
return `${line}`;
55+
56+
for (const line of fileContent) {
57+
if (line === "") {
58+
formattedLines.push(line);
59+
} else {
60+
const formattedLine = `${String(lineNumber++).padStart(6, " ")} ${line}`;
61+
formattedLines.push(formattedLine);
5562
}
56-
return `${String(lineNumber++).padStart(6, " ")} ${line}`;
57-
});
58-
});
63+
}
64+
65+
resultFiles.push(formattedLines);
66+
}
67+
68+
return resultFiles;
5969
}
6070

6171
function nFlag() {

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,8 @@ function ls(args) {
2626
print(populateOutput(currentWorkingDirectory, fsItems, dirArgs, fileArgs));
2727
}
2828

29-
function getFsItems(userArgs) {
30-
return userArgs.filter((arg) => !arg.startsWith("-"));
31-
}
32-
33-
function getCurrentWorkingDirectory(args) {
34-
const index = args[1].lastIndexOf("/");
35-
return args[1].slice(0, index);
36-
}
29+
//
30+
// ===== Flag Handling =====
3731

3832
function getFlags(userArgs) {
3933
return userArgs
@@ -60,10 +54,26 @@ function Flag1() {
6054
printInList = true;
6155
}
6256

57+
function makeList(outputString) {
58+
return outputString.replaceAll("\t", "\n").replaceAll("\n\n", "\n");
59+
}
60+
6361
function Flaga() {
6462
showAll = true;
6563
}
6664

65+
//
66+
// ===== Extracting data from arguments =====
67+
68+
function getCurrentWorkingDirectory(args) {
69+
const index = args[1].lastIndexOf("/");
70+
return args[1].slice(0, index);
71+
}
72+
73+
function getFsItems(userArgs) {
74+
return userArgs.filter((arg) => !arg.startsWith("-"));
75+
}
76+
6777
function checkArgsLength(fsItems) {
6878
if (fsItems.length == 0) fsItems.push(".");
6979
return fsItems;
@@ -79,6 +89,9 @@ function getFileArgs(currentWorkingDirectory, fsItems) {
7989
return fileArgs;
8090
}
8191

92+
//
93+
// ===== Outputting Data =====
94+
8295
function populateOutput(currentWorkingDirectory, fsItems, dirArgs, fileArgs) {
8396
let outputString = "";
8497
if (fsItems.length == 1) {
@@ -126,7 +139,3 @@ function print(outputString) {
126139
if (printInList) output = makeList(output);
127140
console.log(output.trimEnd());
128141
}
129-
130-
function makeList(outputString) {
131-
return outputString.replaceAll("\t", "\n").replaceAll("\n\n", "\n");
132-
}

0 commit comments

Comments
 (0)