Skip to content

Commit b476ec7

Browse files
Fix -b option and remove duplicated print logic
1 parent da8b88e commit b476ec7

File tree

1 file changed

+14
-17
lines changed
  • implement-shell-tools/cat/sample-files

1 file changed

+14
-17
lines changed

implement-shell-tools/cat/sample-files/myCat.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ const { program } = require("commander");
33
const fs = require("fs");
44
const path = require("path");
55

6-
7-
86
function expandWildcard(pattern) {
97
const dir = path.dirname(pattern);
108
const base = path.basename(pattern);
@@ -14,7 +12,7 @@ function expandWildcard(pattern) {
1412
let files;
1513
try {
1614
files = fs.readdirSync(dir);
17-
} catch (e) {
15+
} catch {
1816
console.error(`cat: ${pattern}: No such directory`);
1917
return [];
2018
}
@@ -36,23 +34,23 @@ function printFile(filename, options) {
3634
}
3735

3836
const lines = text.split("\n");
39-
if (lines[lines.length - 1] === "") {
40-
lines.pop();
41-
}
37+
if (lines[lines.length - 1] === "") lines.pop();
38+
4239
let counter = 1;
40+
const paddingSize = 6;
4341

4442
lines.forEach((line) => {
45-
if (options.numberAll) {
46-
const paddingSize = 6;
47-
console.log(`${String(counter).padStart(paddingSize)} ${line}`);
43+
const isEmpty = line.trim() === "";
44+
45+
const shouldNumber =
46+
options.numberAll ||
47+
(options.numberNonempty && !isEmpty);
48+
49+
if (shouldNumber) {
50+
console.log(
51+
`${String(counter).padStart(paddingSize)} ${line}`
52+
);
4853
counter++;
49-
} else if (options.numberNonempty) {
50-
if (line.trim() === "") {
51-
console.log("");
52-
} else {
53-
console.log(`${String(counter).padStart(paddingSize)} ${line}`);
54-
counter++;
55-
}
5654
} else {
5755
console.log(line);
5856
}
@@ -76,4 +74,3 @@ program
7674
});
7775

7876
program.parse();
79-

0 commit comments

Comments
 (0)