From 03969dae8b581703ca68c642488f8852699fd0f8 Mon Sep 17 00:00:00 2001 From: bozhkovkost <71607999+bozhkovkost@users.noreply.github.com> Date: Tue, 25 May 2021 21:09:04 +0500 Subject: [PATCH] =?UTF-8?q?todo-statistic=20=D0=91=D0=BE=D0=B6=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=9A.=D0=A1.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 115 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 8635050..b3cb242 100644 --- a/index.js +++ b/index.js @@ -1,25 +1,136 @@ 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)); } function processCommand(command) { - switch (command) { + let commands = command.split(" "); + switch (commands[0]) { case 'exit': process.exit(0); break; + case 'show': + console.log(toShow()); + break; + case 'important': + console.log(getImportant()); + break; + case 'user': + console.log(getComments(commands[1])); + break; + case 'sort': + switch (commands[1]){ + case 'importance': + console.log(sortImp()); + break; + case 'user': + console.log(sortUser()); + break; + case 'date': + console.log(sortDate()); + break; + } + break; default: console.log('wrong command'); break; } } +function toShow() { + let result = []; + let index; + for(let file of files) { + let arr = file.split('\r\n'); + for(let str of arr) { + if(str.includes('// TODO') && !str.includes('\'// TODO')) { + index = str.indexOf('// TODO'); + result.push(str.slice(index)); + } + } + } + return result; +} + +function getImportant(){ + let allTODO = toShow(); + let result = []; + for(let str of allTODO) { + if(str.includes('!')) result.push(str); + } + return result; +} + +function getComments(username) { + let allTODO = toShow(); + let result = []; + for(let str of allTODO){ + let arr = str.split(';'); + let name = arr[0].slice(8); + let comment = arr[2]; + if(name.toLowerCase() === username.toLowerCase()) + result.push(comment); + } + return result; +} + +function sortImp() { + let allTODO = toShow(); + return allTODO.sort(compareImp); +} + +function sortUser() { + let newAllTODO = toShow().sort().sort(compareUser); + return newAllTODO; +} + +function sortDate() { + let allTODO = toShow(); + return allTODO.sort(compareDate); +} + +function compareImp(a, b) { + return b.split('!').length - a.split('!').length; //если меньше 0, то a будет первее, если больше 0, то b будет первее +} + +function compareUser(a, b) { + let arrA = a.split(';'); + let arrB = b.split(';'); + if(arrA.length !== 1 && arrB.length !== 1) + { + let nameA = arrA[0].slice(8).toLowerCase(); + let nameB = arrB[0].slice(8).toLowerCase(); + if(nameA > nameB) return 1; + if(nameA < nameB) return -1; + if(nameA === nameB) return 0; + } + else if(arrA.length !== 1 && arrB.length === 1) return -1; + + else if(arrA.length === 1 && arrB.length !== 1) return 1; + + else return 0; +} + +function compareDate(a, b) { + let arrA = a.split(';'); + let arrB = b.split(';'); + if(arrA.length !== 1 && arrB.length !== 1) { + let dateA = arrA[1].split('-').join(''); + let dateB = arrB[1].split('-').join(''); + if(+dateA > +dateB) return -1; + else if(+dateA < +dateB) return 1; + else return 0; + } + else if(arrA.length !== 1 && arrB.length === 1) return -1; + + else if(arrA.length === 1 && arrB.length !== 1) return 1; + + else return 0; +} + // TODO you can do it!