forked from Irit-Basic-JS/2-todo-statistic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (64 loc) · 2.19 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
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) {
command = command.split(' ');
switch (command[0]) {
case 'exit':
process.exit(0);
break;
case 'show':
console.log(findTODO().map(x=>x.full));
break;
case 'important':
console.log(findTODO().map(x=>x.full).filter(x => x.includes('!')));
break;
case 'user':
console.log(findTODO().map(x=>x.full).filter(x => x.toLowerCase().includes(command[1].toLowerCase()+';')));
break;
case 'sort':
console.log(SortTODO(command[1]).map(x=>x.full))
break;
default:
console.log('wrong command');
break;
}
}
function findTODO(){
allComments=[]
for (let file of files){
allLines = file.split('\r\n').filter(x => (x.includes('// TODO ')) & !(x.includes('\'// TODO \'')));
for (let line of allLines){
let parsedLine = line.substring(line.indexOf('// TODO ')+'// TODO '.length).split('; ');
let allInfo = {
inside:parsedLine[2]||parsedLine[0],
date: Date.parse(parsedLine[1]),
user: parsedLine[0],
full: line.substring(line.indexOf('// TODO ')),
importance: line.split('!').length
}
allComments.push(allInfo);
}
}
return allComments
}
function SortTODO(comType){
switch (comType){
case 'importance':
return findTODO().sort((x, y) => +(y.importance) - +(x.importance));
case 'user':
return findTODO().sort((x, y) => x.user.localeCompare(y.user))
case 'date':
return findTODO().sort((x, y) => +(y.date) - +(x.date));
default:
console.log('wrong command');
return;
}
}
// TODO you can do it!