Skip to content

Commit d379ae6

Browse files
committed
refactor: remove dublicate code
1 parent a33518b commit d379ae6

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

implement-shell-tools/cat/cat.mjs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,27 @@ let hadError = false;
1717
for (const path of paths) {
1818
try {
1919
const content = await fs.readFile(path, "utf-8");
20-
if (options.b) {
20+
if (options.n || options.b) {
2121
const lines = content.split("\n");
22-
2322
if (lines[lines.length - 1] === "") {
2423
lines.pop();
2524
}
26-
for (const line of lines) {
27-
if (line.trim() !== "") {
28-
process.stdout.write(` ${lineNumber} ${line}\n`);
25+
26+
if (options.b) {
27+
for (const line of lines) {
28+
if (line.trim() !== "") {
29+
process.stdout.write(` ${lineNumber} ${line}\n`);
30+
lineNumber++;
31+
} else {
32+
process.stdout.write("\n");
33+
}
34+
}
35+
} else if (options.n) {
36+
for (const line of lines) {
37+
process.stdout.write(`${String(lineNumber).padStart(6)}\t ${line}\n`);
2938
lineNumber++;
30-
} else {
31-
process.stdout.write("\n");
3239
}
3340
}
34-
} else if (options.n) {
35-
const lines = content.split("\n");
36-
37-
if (lines[lines.length - 1] === "") {
38-
lines.pop();
39-
}
40-
41-
for (const line of lines) {
42-
process.stdout.write(`${String(lineNumber).padStart(6)}\t ${line}\n`);
43-
lineNumber++;
44-
}
4541
} else {
4642
process.stdout.write(content);
4743
}

0 commit comments

Comments
 (0)