forked from Irit-Basic-JS/2-todo-statistic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
174 lines (156 loc) · 4.55 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
const { getAllFilePathsWithExtension, readFile } = require('./fileSystem');
const { readLine } = require('./console');
const { count } = 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 commands = command.split(" ");
switch (commands[0]) {
case 'exit':
process.exit(0);
break;
case 'show':
console.log(doHeader(toShow()));
break;
case 'important':
console.log(doHeader(important()));
break;
case 'user':
console.log(doHeader(user(commands[1])));
break;
case 'sort':
switch (commands[1]){
case 'importance':
console.log(doHeader(sortImportance()));
break;
case 'user':
console.log(sortUser());
case 'date':
console.log(doHeader(sortDate()));
break;
};
case 'date':
console.log(doHeader(doDate(commands[1])));
break;
default:
console.log('wrong command');
break;
}
};
function toShow(){
let lines = [];
for (let file of files)
lines.push(...file.split('\r\n'));
lines = lines.flat(Infinity);
const matches = [];
for (let line of lines) {
let search = line.indexOf(`// TODO`);
if (search != -1)
matches.push(line.substring(search));
}
return doTable(matches);
};
function important(){
let importantLines = [];
for (let line of toShow()){
if (line.includes('!')){
importantLines.push(line);
}
}
return importantLines;
};
function user(name){
return toShow().filter(line => line.toLowerCase().includes(name.toLowerCase()));
};
function sortImportance(){
return toShow().sort((line1, line2) => line2.split('!').length - line1.split('!').length);
};
function sortUser() {
return toShow().sort((x, y) => {
let name1=x.toLowerCase(), name2=y.toLowerCase()
if (x.split("|").length !== 1) {
if (name1 < name2)
return -1
if (name1 > name2)
return 1
return 0;
} else if (y.split("|").length === 1) {
return -1;
} else {
return 0;
}
});
}
function sortDate() {
return toShow().sort((x, y) => {
let line1 = x.split('|'), line2 = y.split('|');
if (line1[2].includes('-')&& line2[2].includes('-')) {
if (line1[2] > line2[2])
return -1;
if (line1[2] < line2[2])
return 1;
return 0;
}
else if (line2[2].indexOf('-')==-1) {
return -1;
}
else {
return 0;
}
});
}
function doDate(date1){
let res =[];
let inputDate=date1.split('-');
listDates= toShow().filter(line=> line.split('|').length>=2);
for(let line of listDates){
let flag = true;
lineDate= line.split(' | ')[2].split('-');
for(let i=0;i<inputDate.length;i++){
if(inputDate[i]>lineDate[i]){
flag=false;
}
}
if(flag) {
res.push(line);
}
}
return res;
};
function doTable(lines){
let res=[];
for (let line of lines){
let strArr =[];
let lineSplit = line.split(';');
if(line.includes('!'))
strArr.push('!');
else strArr.push(' ');
if (lineSplit.length!=3){
let a=' ';
strArr.push(a.padEnd(10,' '));
strArr.push(a.padEnd(11,' '));
strArr.push(lineSplit[0].split('TODO')[1].substring(0,50).padEnd(50,' '))
}
if (lineSplit.length==3){
strArr.push(lineSplit[0].split('TODO')[1].substring(0,10).padEnd(10,' '));
strArr.push(lineSplit[1].padEnd(10,' '));
strArr.push(lineSplit[2].substring(0,50).padEnd(50,' '));
}
res.push(strArr.join(' |'));
}
return res;
}
function doHeader(lines){
let res = ['!','user'.padEnd(10,' '),' date '.padEnd(11,' '),'comment'.padEnd(50,' ')].join(' |');
let line = ''.padEnd(res.length,'-');
lines.unshift(line);
lines.push(line);
lines.unshift(res);
return lines;
}
// TODO you can do it!