forked from kontur-web-courses/todo-statistic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (84 loc) · 2.9 KB
/
Copy pathindex.js
File metadata and controls
97 lines (84 loc) · 2.9 KB
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
const {getAllFilePathsWithExtension, readFile} = require('./fileSystem');
const {readLine} = require('./console');
const todoReg = /\/\/ TODO (.*)/;
const parse = /(?:(.*); )?(?:(\d\d\d\d-\d\d-\d\d);)(.*)/
const files = getFiles();
let todoLines = []
let importantLines = []
parseTODO();
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) {
switch (command.split(' ')[0]) {
case 'exit':
process.exit(0);
break;
case 'user':
const user = command.split(' ')[1].split(';')[0];
const todoByUser = getTodoByName(user);
for (let i =0; i < todoByUser.length; i++) {
console.log(`${i + 1} - ${todoByUser[i]}`);
}
break;
case 'show':
console.log(todoLines);
break;
case 'important':
console.log(importantLines);
break;
case 'sort':
const sortBy = command.split(' ')[1];
if (sortBy === 'importance') {
const sortedByImportance = todoLines.sort((a, b) => {
const aImportance = (a.match(/!/g) || []).length;
const bImportance = (b.match(/!/g) || []).length;
return bImportance - aImportance;
}
);
console.log(sortedByImportance);
} else if (sortBy === 'user') {
const sortedByUser = todoLines.sort((a, b) => {
const aUser = a.split(';')[0].toLowerCase();
const bUser = b.split(';')[0].toLowerCase();
if (a.includes(';') && !b.includes(';')) {
return -1;
}
if (!a.includes(';') && b.includes(';')) {
return 1;
}
if (aUser < bUser) return -1;
if (aUser > bUser) return 1;
return 0;
}
);
console.log(sortedByUser);
}
break;
default:
console.log('wrong command');
break;
}
}
function getTodoByName(name){
const commentsByName = todoLines.filter(line => line.toLowerCase().startsWith(name.toLowerCase()));
return commentsByName;
}
function parseTODO(){
for (const file of files) {
const lines = file.split(/\r?\n/);
for (const line of lines) {
const match = line.match(todoReg);
if (match) {
const comment = match[1];
todoLines.push(comment);
if (comment.includes('!')) {
importantLines.push(comment);
}
}
}
}
}