Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Сыч Эрнест Санатин Николай ФТ-201 #153

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 113 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,126 @@ function getFiles() {
}

function processCommand(command) {
switch (command) {
command = command.split(' ');
switch (command[0]) {
case 'exit':
process.exit(0);
break;
case 'show':
for (let comment of getTODOcomments(getFiles()).map(TODO => TODO.comment)){
console.log('');
console.log(comment);

}
break;
case 'important':
for (let comment of getTODOcomments(getFiles()).filter(TODO => TODO.importance > 0).map(TODO => TODO.comment)){
console.log('');
console.log(comment);
}
break;
case 'user':
for (let comment of getTODOcomments(getFiles())
.filter(TODO => TODO.user !== undefined && TODO.user.toLowerCase() === command[1].toLowerCase())
.map(TODO => TODO.comment)){
console.log('');
console.log(comment);
}
break;
case 'sort':
let array = getTODOcomments(getFiles());
if (command[1] === 'importance'){
array.sort((a, b) => b.importance - a.importance);
array = array.map(TODO => TODO.comment);
} else if (command[1] === 'date') {
let withoutDate = array.filter(TODO => TODO.date === undefined).map(TODO => TODO.comment);
let withDate = array.filter(TODO => TODO.date !== undefined).sort((a, b) => b.date - a.date).map(TODO => TODO.comment);
array = withDate.concat(withoutDate);
} else if (command[1] === 'user'){
let dict = {};
let undef = [];
for (let TODO of array){
if (TODO.user !== undefined) {
if (!(TODO.user.toLowerCase() in dict)) {
dict[TODO.user.toLowerCase()] = [];
}
dict[TODO.user.toLowerCase()].push(TODO.comment.replace(TODO.user + ';', '').trim());
} else {
undef.push(TODO.comment);
}
}
array = [];
for (let key in dict){
array.push(key+':');
for (let e of dict[key]){
array.push(e);
}
}
for (let e of undef){
array.push(e);
}
} else{
console.log('Такая сортировка не поддерживается');
}
for (let elem of array){
console.log();
console.log(elem);
}
break;
default:
console.log('wrong command');
break;
}
}

function getTODOcomments(files){
let TODO = [];
for (let file of files){
let tempFile = file;
while (true){

let index = tempFile.indexOf('// TODO ');
if (index === -1){
break;
}
tempFile = tempFile.slice(index);
let nIndex = tempFile.indexOf('\n');
if (nIndex === -1){
TODO.push(createTODOObject(tempFile.trim()))
break;
}
else{
TODO.push(createTODOObject(tempFile.slice(8, nIndex).trim()))
tempFile = tempFile.slice(nIndex);
}
}
}
return TODO;
}

function createTODOObject(TODOComment) {
let TODOobject = { user: undefined,
date: undefined,
importance: 0,
comment: undefined
};
let ops = TODOComment.split(';').map(
function (tag) {
return tag.trim()});
TODOobject.comment = TODOComment;
for (let e of ops.slice(0, -1)){
let probableDate = Date.parse(e)
if (Number.isNaN(probableDate)){
TODOobject.user = e;
}
else{
TODOobject.date = probableDate;
}
}
let impTemp = TODOobject.comment.match(new RegExp('!', 'g'));
TODOobject.importance = impTemp === null ? 0 : impTemp.length;
return TODOobject;
}


// TODO you can do it!