forked from CodeYourFuture/Module-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.js
More file actions
44 lines (36 loc) · 899 Bytes
/
cat.js
File metadata and controls
44 lines (36 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const fs = require("fs");
// input from terminal
const args = process.argv.slice(2);
// check flags
const hasN = args.includes("-n");
const hasB = args.includes("-b");
// get only file names
const files = args.filter((arg) => !arg.startsWith("-"));
let count = 1;
// go through each file
for (let i = 0; i < files.length; i++) {
const content = fs.readFileSync(files[i], "utf-8");
const lines = content.split("\n");
// go through each line
for (let j = 0; j < lines.length; j++) {
const line = lines[j];
// if -b (number non-empty lines)
if (hasB) {
if (line.trim() !== "") {
console.log(count + " " + line);
count++;
} else {
console.log("");
}
}
// if -n (number all lines)
else if (hasN) {
console.log(count + " " + line);
count++;
}
// no flags
else {
console.log(line);
}
}
}