From 44f7d54f6254cdff4d4a994028fbacc03eef8584 Mon Sep 17 00:00:00 2001 From: coca-in-a-cola Date: Thu, 7 Oct 2021 22:31:49 +0500 Subject: [PATCH 1/2] Completed tasks 1-5 --- .vscode/launch.json | 17 ++++++ index.js | 131 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ab42783 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "pwa-node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/index.js" + } + ] +} \ No newline at end of file diff --git a/index.js b/index.js index 8635050..299d026 100644 --- a/index.js +++ b/index.js @@ -11,11 +11,70 @@ function getFiles() { return filePaths.map(path => readFile(path)); } -function processCommand(command) { +function showTODOs(data, important = false, user = undefined) { + data.forEach(todo => { + if ((user == undefined || (todo.user != undefined && (user.toLowerCase() == todo.user.toLowerCase()))) + && (!important || todo.importance)) + console.log(todo.raw); + }); +} + +function sortTODOs(data, param) +{ + let sortFunc = undefined; + switch (param) { + case 'date': + sortFunc = (a, b) => b.date - a.date; + break; + case 'importance': + sortFunc = (a, b) => b.importance - a.importance; + break; + case 'user': + sortFunc = (a, b) => a.user == undefined || b.user == undefined ? a.user - b.user : a.user.localeCompare(b.user); + break; + default: + console.log('wrong params'); + break; + } + if (sortFunc != undefined) { + data.sort(sortFunc); + showTODOs(data); + } +} + +function processCommand(input) { + let preProcessData = preProcessTODOS(getFiles()); + let fullCommand = input.split(" "); + let command = fullCommand[0]; + let params = []; + + if (fullCommand.length > 1) + params = fullCommand.slice(1, fullCommand.length) + switch (command) { case 'exit': process.exit(0); break; + case 'show': + showTODOs(preProcessData); + break; + case 'important': + showTODOs(preProcessData, true) + break; + case 'user': + if (params.length == 1) { + showTODOs(preProcessData, true, params[0]) + } + else + console.log('wrong params'); + break; + case 'sort': + if (params.length == 1) { + sortTODOs(preProcessData, params[0]); + } + else + console.log('wrong params'); + break; default: console.log('wrong command'); break; @@ -23,3 +82,73 @@ function processCommand(command) { } // TODO you can do it! + +function hasParam(params, expected) { + let result = false; + params.forEach(param => { + result = result || param == expected; + }); + return result; +} + + +function getTODOs(text) { + let result = []; + let index = 0; + while(index != -1 && index < text.length) { + let end = -1; + index = text.indexOf("// TODO", index); + if (index != -1) { + //убираем упоминания "// TODO" (в ковычках) + if (text[index-1] != `"` + || text[index+"// TODO".length] != `"`) { + end = text.indexOf(`\n`, index); + if (end == -1) + end = text.length - 1; + + result.push(text.substring(index,end)); + } + index++; + } + } + + return result; +} + +function parseTODO(todo) { + let result = {raw: todo, user: undefined, date: undefined}; + + let split = todo.split(';'); + + if (split.length > 1) { + result.user = split[0].substring ( + split[0].indexOf("// TODO") + "// TODO".length, split[0].length + ) + .replace(/\s/g, ''); + if (result.user == "") + result.user = undefined; + } + + if (split.length > 2) { + result.date = Date.parse( + split[1].substring(0, split[0].length+1).replace(/\s/g, '')); + } + + result.importance = (todo.match(/!/g) || []).length; + return result; +} + +function preProcessTODOS(texts) +{ + let result = []; + texts.forEach(text => { + getTODOs(text).forEach(todo => { + result.push(parseTODO(todo)); + }); + }); + + return result; +} + +//console.log(preProcessTODOS(getFiles())); +//processCommand("sort importance") \ No newline at end of file From 3db412600da8eb5d28b093d65adb23ad820647e5 Mon Sep 17 00:00:00 2001 From: coca-in-a-cola Date: Thu, 14 Oct 2021 21:23:14 +0500 Subject: [PATCH 2/2] all tasks done --- index.js | 143 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 127 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 299d026..5243ed4 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,115 @@ 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)); + let result = {}; + + filePaths.forEach(path => { + result[path] = readFile(path) + }); + return result; } -function showTODOs(data, important = false, user = undefined) { +function showTODOs(data, filterFunc) { + let content = []; data.forEach(todo => { - if ((user == undefined || (todo.user != undefined && (user.toLowerCase() == todo.user.toLowerCase()))) - && (!important || todo.importance)) - console.log(todo.raw); + if (filterFunc(todo)) + content.push(getTODOdata(todo)); + }); + + let headers = ["!", "user", "date", "comment", "path"]; + printTable(headers, content); +} + +function getTODOdata(todo) { + let raw = Object.values(todo); + raw.forEach((e, index) => { + if (e === undefined) + raw[index] = ""; + else { + if (index == 2) + raw[index] = (new Date(raw[index])).toISOString().split('T')[0]; + raw[index] = String(raw[index]) + } }); + + return raw; +} + +function printTable(headers, data) { + + let maxWidthArr = getMaxWidthArr(data.concat([[...headers]])); + console.log( + getHeaders(headers, maxWidthArr) + + "\n" + + getData(data, maxWidthArr) + ); + + function getHeaders(headers, maxWidthArr) { + let row = getRow(headers, maxWidthArr); + return row + '\n' + + `${'-'.repeat(row.length)}`; + } + + function getMaxWidthArr(allData, limit = 30) { + let maxWidthArr = Array(allData[0].length).fill(0); + + allData.forEach(row => { + for (let i = 0; i < row.length; ++i) { + maxWidthArr[i] = Math.max(maxWidthArr[i], row[i].length); + maxWidthArr[i] = Math.min(maxWidthArr[i], limit); + } + }); + + return maxWidthArr; + } + + function getData(table, maxWidthArr) { + let strings = []; + + table.forEach(row => { + strings.push(getRow(row, maxWidthArr)); + strings.push(getRow(Array(row.length).fill(""), maxWidthArr)); + }); + + return strings.join("\n") + } + + function getRow(textArr, maxWidthArr) { + let cells = []; + let limit = Math.max(...maxWidthArr); + textArr.forEach((text, index) => { + let cell = getCell(text.substring(0, limit), maxWidthArr[index]) + cells.push(cell); + }); + + let result = cells.join("|"); + + const overlength = (e) => e.length > limit; + + if (textArr.some(overlength)) { + result += "\n"; + let secondLevelText = []; + textArr.forEach(text => { + if (overlength(text)) + secondLevelText.push(text.substring(limit, text.length)); + else + secondLevelText.push("") + }); + + result += getRow(secondLevelText, maxWidthArr); + } + + return result + } + + function getCell(text, width) { + return ` ${text}${' '.repeat(width - text.length)} `; + } } function sortTODOs(data, param) @@ -38,7 +131,7 @@ function sortTODOs(data, param) } if (sortFunc != undefined) { data.sort(sortFunc); - showTODOs(data); + showTODOs(data, (e) => true); } } @@ -55,19 +148,33 @@ function processCommand(input) { case 'exit': process.exit(0); break; + case 'show': - showTODOs(preProcessData); + showTODOs(preProcessData, (todo) => true); break; + case 'important': - showTODOs(preProcessData, true) + showTODOs(preProcessData, (todo) => todo.importance > 0) break; + case 'user': if (params.length == 1) { - showTODOs(preProcessData, true, params[0]) + showTODOs(preProcessData, (todo) => + todo.user != undefined && (params[0].toLowerCase() == todo.user.toLowerCase())) + } + else + console.log('wrong params'); + break; + + case 'date': + if (params.length == 1) { + let date = Date.parse(params[0]); + showTODOs(preProcessData, (todo) => todo.date >= date) } else console.log('wrong params'); break; + case 'sort': if (params.length == 1) { sortTODOs(preProcessData, params[0]); @@ -75,6 +182,7 @@ function processCommand(input) { else console.log('wrong params'); break; + default: console.log('wrong command'); break; @@ -116,7 +224,7 @@ function getTODOs(text) { } function parseTODO(todo) { - let result = {raw: todo, user: undefined, date: undefined}; + let result = {importance: 0, user: undefined, date: undefined, message: undefined}; let split = todo.split(';'); @@ -135,15 +243,18 @@ function parseTODO(todo) { } result.importance = (todo.match(/!/g) || []).length; + result.message = split[split.length - 1].trim(); return result; } function preProcessTODOS(texts) { let result = []; - texts.forEach(text => { - getTODOs(text).forEach(todo => { - result.push(parseTODO(todo)); + Object.keys(texts).forEach(key => { + getTODOs(texts[key]).forEach(todo => { + let parsed = parseTODO(todo); + parsed.path = key; + result.push(parsed); }); }); @@ -151,4 +262,4 @@ function preProcessTODOS(texts) } //console.log(preProcessTODOS(getFiles())); -//processCommand("sort importance") \ No newline at end of file +//processCommand("show") \ No newline at end of file