Skip to content
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
85 changes: 81 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,92 @@ function getFiles() {
return filePaths.map(path => readFile(path));
}

function extractTODO() {
let comments = [];
const todoConst = '//' + ' TODO';
for (const fileContent of files) {
const lines = fileContent.split('\n');
for (const line of lines) {
if (line.includes(todoConst)) {
comments.push(line.split(todoConst)[1].trim());
}
}
}
return comments;
}

function exctractImportant(comments) {
let important = [];
for (const comment of comments) {
if (comment.includes("!")) {
important.push(comment);
}
}
return important;
}

function filterByUser(user, comments) {
const userLowerCase = user.toLowerCase();
return comments.filter((comment) => comment.toLowerCase().startsWith(userLowerCase + ';'));
}

function filterByImportant(comments) {
return comments.sort((cmnt1, cmnt2) => cmnt2.split('!').length - cmnt1.split('!').length);
}

function sortComments(flag, comments) {
switch (flag) {
case 'importance':
return filterByImportant(comments);
case 'user':
return filterByUsers(comments);
case 'date':
return filterByDate(comments);
}
}

function filterByDate(comments) {
let arr = [];
for (const comment of comments) {
let date = new Date(comment.split(";")[1]);
arr.push({date, comment});
}
arr.sort((a, b) => b.date - a.date);
return arr.map(x => x.comment);
}

function filterByUsers(comments) {
let arr = [];
for (const comment of comments) {
let username = comment.split(";")[0].toLowerCase();
arr.push({username, comment});
}
arr.sort((a, b) => a.username.localeCompare(b.username));
return arr.map(x => x.comment);
}


function processCommand(command) {
switch (command) {
let [commandName, ...args] = command.split(' ');
const comments = extractTODO()
switch (commandName) {
case 'exit':
process.exit(0);
break;
case 'show':
console.log(comments);
break;
case 'important':
console.log(exctractImportant(comments));
break;
case 'user':
console.log(filterByUser(args[0], comments));
break;
case 'sort':
console.log(sortComments(args[0], comments));
break;
default:
console.log('wrong command');
break;
}
}

// TODO you can do it!
}