forked from Irit-Basic-JS/2-todo-statistic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
187 lines (168 loc) · 4.93 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const {getAllFilePathsWithExtension, readFile} = require('./fileSystem');
const {readLine} = require('./console');
const files = getFiles();
console.log('Please, write your command!');
readLine(processCommand);
function getFiles()
{
const filePaths = getAllFilePathsWithExtension(process.cwd(), 'js');
return filePaths.map(path => readFile(path));
}
function processCommand(command)
{
let option = command.split(' ');
switch (option[0])
{
case 'exit':
process.exit(0);
break;
case 'show':
console.log(getTab(toShow()));
break;
case 'important':
console.log(getTab(getImportant()));
break;
case 'user':
console.log(getTab(getComments(option[1])));
break;
case 'sort':
switch (option[1]) {
case 'importance':
console.log(getTab(sortImportance()));
break;
case 'user':
console.log(getTab(sortUser()));
break;
case 'date':
console.log(getTab(sortDate()));
break;
default:
console.log('wrong command');
break;
}
break;
case 'date':
console.log(getTab(date(option[1])));
break;
default:
console.log('wrong command');
break;
}
}
function toShow()
{
let res = [];
files.map(code => code.split("\r\n")
.filter(line => line.includes("// TODO ") && !line.includes("\"// TODO \"")))
.forEach(t => t.forEach(line => res.push(line.split("// TODO ")[1])));
return res;
}
function getImportant()
{
return toShow().filter(line => line.endsWith("!"));
}
function getComments(userName)
{
return toShow().filter(line => line.toLowerCase().startsWith(`${userName.toLowerCase()};`));
}
function sortImportance()
{
return toShow().sort((a, b) => b.split("!").length - a.split("!").length);
}
function sortUser()
{
return toShow().sort((a, b) => {
let nameA=a.toLowerCase(), nameB=b.toLowerCase()
if (a.split(";").length !== 1)
{
if (nameA < nameB)
return -1
if (nameA > nameB)
return 1
return 0;
}
else if (b.split(";").length === 1)
return -1;
else
return 0;
});
}
function sortDate()
{
return toShow().sort((a, b) => {
let aInfo = a.split("; "), bInfo = b.split("; ");
if (aInfo.length === 3)
{
if (aInfo[1] > bInfo[1])
return -1;
return aInfo[1] < bInfo[1] ? 1 : 0;
}
else
return bInfo.length === 1 ? -1 : 0;
});
}
function date(value)
{
return toShow().filter(a => {
let aInfo = a.split("; ");
if (aInfo.length === 3)
return (aInfo[1] > value);
return false;
});
}
function truncateString (str, len)
{
let strSlice = str.slice(0, len);
return strSlice === str ? str + ' '.repeat(len - str.length) : strSlice.slice(0, len-3) + "...";
}
function getTab(value)
{
const sep = " | ";
let userMax = 4;
let dateMax = 4;
let commentMax = 7;
value.forEach(line =>
{
const lineInfo = line.split("; ");
if (lineInfo.length === 3)
{
userMax = Math.max(userMax, lineInfo[0].length);
dateMax = Math.max(dateMax, lineInfo[1].length);
commentMax = Math.max(commentMax, lineInfo[2].length);
}
else
{
commentMax = Math.max(commentMax, lineInfo[0].length);
}
});
userMax = Math.min(userMax, 10);
commentMax = Math.min(commentMax, 50);
let res = value.map(line =>
{
const lineInfo = line.split("; ");
if (lineInfo.length === 3)
{
const important = lineInfo[2].endsWith("!") ? '!' : ' ';
lineInfo[0] = truncateString(lineInfo[0], userMax);
lineInfo[2] = truncateString(lineInfo[2], commentMax);
return important + sep + lineInfo.join(sep);
}
else
{
const important = lineInfo[0].endsWith("!") ? '!' : ' ';
lineInfo[0] = truncateString(lineInfo[0], commentMax);
return important + sep + ' '.repeat(userMax) + sep + ' '.repeat(10) + sep + lineInfo[0];
}
});
let titleUser = truncateString("user", userMax);
let titleDate = truncateString("date", dateMax);
let titleComment = truncateString("comment", commentMax);
const title = "!" + sep + titleUser + sep + titleDate + sep + titleComment;
const titleLine = '-'.repeat(title.length);
res.unshift(title, titleLine);
res.push(titleLine);
return res;
}
// TODO you can do it!
// TODO lalalala
// TODO do re mi fa sol lya si