-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwcat.js
94 lines (80 loc) · 1.82 KB
/
wcat.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
let fs = require("fs");
let cmd = process.argv.slice(2);
let opt = [];
let file = [];
let str = ``;
for (let i = 0; i < cmd.length; i++) {
if (cmd[i].startsWith("-")) {
opt.push(cmd[i]);
} else {
file.push(cmd[i]);
}
}
for (let j = 0; j < file.length; j++) {
if (fs.existsSync(file[j])) {
str += fs.readFileSync(file[j]).toString();
} else {
console.log("Invalid file");
return;
}
}
str = str.split("\n");
if (opt.includes("-s")) {
str = convToSinLineBreak(str);
}
if (opt.includes("-b") && opt.includes("-n")) {
if (opt.indexOf("-n") > opt.indexOf("-b")) {
//implementing -b
str = addNumtoNonEmpty(str);
} else {
//implementing -n
str = addNumtoAllLines(str);
}
} else {
if (opt.includes("-n")) {
//implementing -n
str = addNumtoAllLines(str);
}
if (opt.includes("-b")) {
//implementing -b
str = addNumtoNonEmpty(str);
}
}
str = str.join("\n");
console.log(str);
function convToSinLineBreak(arr) {
let v = [];
let flag = false;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === "" || arr[i] == "\r") {
if (flag === true) {
continue;
} else {
y.push(arr[i]);
flag = true;
}
} else {
y.push(arr[i]);
flag = false;
}
}
return v;
}
//-n
function addNumtoAllLines(arr) {
for (let i = 1; i <= arr.length; i++) {
arr[i - 1] = i + " " + arr[i - 1];
}
return arr;
}
//-b
function addNumtoNonEmpty(arr) {
let numLine = 1;
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== "" && arr[i] !== "\r") {
arr[i] = numLine + " " + arr[i];
numLine++;
}
}
return arr;
}