Skip to content

Commit b309bfe

Browse files
authored
Indentation cat-commander.js for improved readability
1 parent 644e962 commit b309bfe

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

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

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,37 @@ if (argv.length === 0) {
1818
process.exit(1);
1919
}
2020

21-
for (const path of argv){
22-
const option = program.opts();
23-
const content = await fs.readFile(path, "utf-8");
24-
if (option.number){
25-
// split content
26-
let lines = content.split(/\n/);
27-
// removes last line
28-
if (lines[lines.length - 1] === ""){
29-
lines = lines.slice(0,-1);
30-
}
31-
lines
32-
.map((line,index) => `${(index + 1).toString().padStart(6)}\t${line}`)
33-
.forEach(line => console.log(line))
34-
35-
} else if (option.b){
36-
let lines = content.split("\n");
37-
let linesCounter = 0;
38-
if (lines[lines.length - 1] === ""){
39-
lines = lines.slice(0,-1);
40-
}
41-
lines
42-
.map((line) => {
43-
if (line.trim().length > 0) {
44-
linesCounter++;
45-
return `${linesCounter.toString().padStart(6)}\t${line}`
46-
}else{
47-
return `${line}`
48-
}}
49-
)
50-
.forEach(line => console.log(line))
51-
}else {
52-
const output = content.endsWith("\n") ? content.slice(0,-1) : content;
53-
console.log(output);
21+
for (const path of argv) {
22+
const option = program.opts();
23+
const content = await fs.readFile(path, "utf-8");
24+
if (option.number) {
25+
// split content
26+
let lines = content.split(/\n/);
27+
// removes last line
28+
if (lines[lines.length - 1] === "") {
29+
lines = lines.slice(0, -1);
5430
}
31+
lines
32+
.map((line, index) => `${(index + 1).toString().padStart(6)}\t${line}`)
33+
.forEach((line) => console.log(line));
34+
} else if (option.b) {
35+
let lines = content.split("\n");
36+
let linesCounter = 0;
37+
if (lines[lines.length - 1] === "") {
38+
lines = lines.slice(0, -1);
39+
}
40+
lines
41+
.map((line) => {
42+
if (line.trim().length > 0) {
43+
linesCounter++;
44+
return `${linesCounter.toString().padStart(6)}\t${line}`;
45+
} else {
46+
return `${line}`;
47+
}
48+
})
49+
.forEach((line) => console.log(line));
50+
} else {
51+
const output = content.endsWith("\n") ? content.slice(0, -1) : content;
52+
console.log(output);
53+
}
5554
}

0 commit comments

Comments
 (0)