From debe8cad08fa5d857b7af6ef6969f207fc087360 Mon Sep 17 00:00:00 2001 From: Teelirium <74907594+Teelirium@users.noreply.github.com> Date: Thu, 14 Oct 2021 16:02:28 +0500 Subject: [PATCH 1/3] completed tasks 1-10 --- index.js | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8635050..b468987 100644 --- a/index.js +++ b/index.js @@ -8,18 +8,118 @@ readLine(processCommand); function getFiles() { const filePaths = getAllFilePathsWithExtension(process.cwd(), 'js'); - return filePaths.map(path => readFile(path)); + return filePaths.map(path => ({ + content: readFile(path), + filename: path.split('/').reverse()[0] + })); } function processCommand(command) { - switch (command) { + const commandArgs = command.split(' '); + let comments = []; + switch (commandArgs[0]) { case 'exit': process.exit(0); break; + case 'show': + comments = getComments(); + break; + case 'important': + comments = getComments().filter(x => x.important); + break; + case 'user': + comments = getComments().filter(x => x.user.toLowerCase().includes(commandArgs[1].toLowerCase())); + break; + case 'sort': + comments = getSortedComments(commandArgs[1]); + break; + case 'date': + comments = getComments().filter(x => x.date > Date.parse(commandArgs[1])); + break; default: console.log('wrong command'); break; } + if (comments) showComments(comments); +} + +function getSortedComments(sortingType) { + switch (sortingType) { + case 'importance': + return getComments().sort((a, b) => +b.important - +a.important); + case 'user': + return getComments().sort((a, b) => a.user.localeCompare(b.user)); + case 'date': + return getComments().sort((a, b) => +b.date - +a.date); + default: + return 'wrong command'; + } +} + +const TODOSTRING = "// TODO"; + +function getComments() { + let matches = []; + for (let file of files) { + let lines = file.content.split('\n').filter(x => x.includes(TODOSTRING) && !x.includes(`"${TODOSTRING}"`)); + for (let line of lines) { + let tokens = line.substring(line.indexOf(TODOSTRING) + TODOSTRING.length).split('; '); + let match = { + important: line.includes('!'), + user: tokens.length < 3 ? "" : tokens[0], + date: Date.parse(tokens[1]), + text: tokens[2] || tokens[0], + filename: file.filename, + }; + matches.push(match); + } + } + return matches; +} + +function showComments(comments) { + strings = getCommentStrings(comments); + let divider = Array(strings[0].length).fill('-').join('') + console.log(strings[0]); + console.log(divider); + strings.slice(1).forEach(x => console.log(x)); + console.log(divider); +} + +const fieldSizeLimit = [ 1, 10, 10, 50, 15 ]; + +function getCommentStrings(comments) { + let commentArrs = []; + commentArrs.push(['!', 'user', 'date', 'comment', 'filename']); + comments.forEach(x => commentArrs.push(formatComment(x))); + + for (let i = 0; i < fieldSizeLimit.length; i++) { + let fieldSizes = []; + commentArrs.forEach(x => fieldSizes.push(x[i].trim().length)); + + let fieldSize = Math.min(Math.max(...fieldSizes), fieldSizeLimit[i]); + commentArrs.forEach(x => x[i] = ` ${x[i].padEnd(fieldSize, ' ')} `); + } + return commentArrs.map(x => x.join('|')); +} + +function formatComment(comment) { + let commentAdapted = []; + commentAdapted = [ + comment.important ? "!" : "", + comment.user.toLowerCase().trim(), + comment.date ? new Date(comment.date).toISOString().substring(0, 10) : "", + comment.text.replace('!', '').trim(), + comment.filename + ]; + let result = []; + for (let i = 0; i < commentAdapted.length; i++) { + let shortened = commentAdapted[i].length > fieldSizeLimit[i] ? + `${commentAdapted[i].substring(0, fieldSizeLimit[i] - 1)}…` : + commentAdapted[i]; + result.push(shortened); + } + return result; } // TODO you can do it! From b15826dbfac28fd2f70406506de5551f5343e145 Mon Sep 17 00:00:00 2001 From: Teelirium <74907594+Teelirium@users.noreply.github.com> Date: Thu, 14 Oct 2021 16:13:50 +0500 Subject: [PATCH 2/3] wrong command fixes --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b468987..e9be68d 100644 --- a/index.js +++ b/index.js @@ -37,10 +37,10 @@ function processCommand(command) { comments = getComments().filter(x => x.date > Date.parse(commandArgs[1])); break; default: - console.log('wrong command'); break; } if (comments) showComments(comments); + else console.log('wrong command'); } function getSortedComments(sortingType) { @@ -52,7 +52,7 @@ function getSortedComments(sortingType) { case 'date': return getComments().sort((a, b) => +b.date - +a.date); default: - return 'wrong command'; + return; } } From 499c3a0a94b5d144dd2e30d5d0196e31074b46c4 Mon Sep 17 00:00:00 2001 From: Teelirium <74907594+Teelirium@users.noreply.github.com> Date: Thu, 14 Oct 2021 23:44:23 +0500 Subject: [PATCH 3/3] fix sort importance --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index e9be68d..4b28ddf 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ function getFiles() { function processCommand(command) { const commandArgs = command.split(' '); + commandArgs.forEach(x => x.trim()); let comments = []; switch (commandArgs[0]) { case 'exit': @@ -46,7 +47,7 @@ function processCommand(command) { function getSortedComments(sortingType) { switch (sortingType) { case 'importance': - return getComments().sort((a, b) => +b.important - +a.important); + return getComments().sort((a, b) => +b.text.split('!').length - +a.text.split('!').length); case 'user': return getComments().sort((a, b) => a.user.localeCompare(b.user)); case 'date': @@ -109,7 +110,7 @@ function formatComment(comment) { comment.important ? "!" : "", comment.user.toLowerCase().trim(), comment.date ? new Date(comment.date).toISOString().substring(0, 10) : "", - comment.text.replace('!', '').trim(), + comment.text.trim(), comment.filename ]; let result = [];