Skip to content

Commit 60b323e

Browse files
committed
cat updated
1 parent 78bdfd5 commit 60b323e

1 file changed

Lines changed: 52 additions & 25 deletions

File tree

  • implement-shell-tools/cat

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

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,73 @@
11
const fs = require("fs");
22

33
const arguments = process.argv;
4-
const filePaths = arguments.slice(2);
4+
const userArguments = arguments.slice(2);
55

66
const index = arguments[1].lastIndexOf("/");
77
const currentWorkingDirectory = arguments[1].slice(0, index + 1);
88

9-
let flag = filePaths[0];
10-
if (flag.charAt(0) == "-") {
11-
flag = filePaths.shift();
9+
const flags = userArguments
10+
.filter((argument) => argument.startsWith("-"))
11+
.map((argument) => argument.slice(1))
12+
.join("");
13+
14+
const flagHandlers = {
15+
b: bFlag,
16+
n: nFlag,
17+
};
18+
19+
const fileNames = userArguments.filter((argument) => !argument.startsWith("-"));
20+
let allFilesContents = [];
21+
22+
readFiles();
23+
executeFlags();
24+
printLines();
25+
26+
function readFiles() {
27+
fileNames.forEach((fileName) => {
28+
fileContent = readFile(fileName);
29+
allFilesContents.push(fileContent.split("\n"));
30+
});
1231
}
1332

14-
function splitFile(fileName) {
33+
function readFile(fileName) {
1534
const filePath = currentWorkingDirectory + fileName;
16-
const text = fs.readFileSync(filePath, "utf8").trimEnd();
17-
return text.split("\n");
35+
return fs.readFileSync(filePath, "utf8").trimEnd();
1836
}
1937

20-
function printFile(lines) {
21-
lines.forEach((line) => {
22-
console.log(line);
23-
});
38+
function executeFlags() {
39+
for (const flag of flags) {
40+
if (flagHandlers[flag]) {
41+
allFilesContents = flagHandlers[flag]();
42+
} else {
43+
console.error(`cat: illegal option -- ${flag}\nusage: cat [-belnstuv] [file ...]`);
44+
process.exit(1);
45+
}
46+
}
2447
}
2548

26-
function executeFlag(lines) {
27-
if (flag === "-n") {
28-
return lines.map((line, index) => `${String(index + 1).padStart(6, " ")} ${line}`);
29-
} else if (flag === "-b") {
49+
function bFlag() {
50+
return allFilesContents.map((fileContent) => {
3051
let lineNumber = 1;
31-
return lines.map((line) => {
32-
if (line === "") {
52+
return fileContent.map((line, index) => {
53+
if (line == "") {
3354
return `${line}`;
3455
}
3556
return `${String(lineNumber++).padStart(6, " ")} ${line}`;
3657
});
37-
} else {
38-
return lines;
39-
}
58+
});
59+
}
60+
61+
function nFlag() {
62+
return allFilesContents.map((fileContent) => {
63+
return fileContent.map((line, index) => `${String(index + 1).padStart(6, " ")} ${line}`);
64+
});
4065
}
4166

42-
filePaths.forEach((filePath) => {
43-
let lines = splitFile(filePath);
44-
lines = executeFlag(lines);
45-
printFile(lines);
46-
});
67+
function printLines() {
68+
allFilesContents.forEach((fileContent) => {
69+
fileContent.forEach((line) => {
70+
console.log(line);
71+
});
72+
});
73+
}

0 commit comments

Comments
 (0)