From 122d665ab12bceac2f6eaa0871f7facb5676c9af Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 22 Apr 2019 10:26:02 +0900 Subject: [PATCH 01/62] create todosList.json --- todosList.json | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 todosList.json diff --git a/todosList.json b/todosList.json new file mode 100644 index 0000000..1734fe5 --- /dev/null +++ b/todosList.json @@ -0,0 +1,41 @@ +{ + "result" : true, + "data": [ + { + "name" : "자바스크립트 공부하기", + "tags" : ["programming", "javascript"], + "status" : "todo", + "id" : 12123123 + }, + { + "name" : "그림 그리기", + "tags" : ["picture", "favorite"], + "status" : "doing", + "id" : 123 + }, + { + "name" : "파이썬 공부하기", + "tags" : ["programming", "javascript"], + "status" : "done", + "id" : 213232 + }, + { + "name" : "자바 공부하기", + "tags" : ["programming", "java"], + "status" : "todo", + "id" : 123345 + }, + { + "name" : "음악 듣기", + "tags" : ["music", "favorite"], + "status" : "doing", + "id" : 12123 + }, + { + "name" : "정규식 공부하기", + "tags" : ["programming", "RegExp"], + "status" : "done", + "id" : 1234 + } + ] +} From 740e861744a118f3374934c1de3bb923c18f6909 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 22 Apr 2019 10:26:28 +0900 Subject: [PATCH 02/62] create todos.js --- todos.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 todos.js diff --git a/todos.js b/todos.js new file mode 100644 index 0000000..3771754 --- /dev/null +++ b/todos.js @@ -0,0 +1,88 @@ +//todos.js +const originData = require('./todosList.json'); +const convertedData = JSON.parse(JSON.stringify(originData)).data; +const idGenerator = () => Math.floor(Math.random() * (99999)) + 1; + +const getArrByCondition = (arr, condition) => { + return arr.reduce((acc, val) => { + if (condition(val)) acc.push(val); + return acc; + }, []); +}; + +const showStatusLazy = () => { + setTimeout(() => { + showData('all'); + }, 1000); +} + +const isDuplicated = (val, key, convertedData) => { + let result = false; + convertedData.forEach((element) => { + if (val == element[key]) result = true; + }) + return result; +} + +const showData = (type) => { + if (type == 'all') { + const numOfTodos = getArrByCondition(convertedData, (val) => {return val.status == "todo"}).length; + const numOfDoings = getArrByCondition(convertedData, (val) => {return val.status == "doing"}).length; + const numOfDones = getArrByCondition(convertedData, (val) => {return val.status == "done"}).length; + + console.log(`현재상태 : todo: ${numOfTodos}개, doing: ${numOfDoings}개, done: ${numOfDones}개`); + + } else { + const objArr = getArrByCondition(convertedData, (val) => {return (type == val.status)}); + let result = `${type} 리스트 : 총${objArr.length}건 :`; + getArrByCondition(objArr, (val) => {result += `, '${val.name}, ${val.id}번'`; return true}); + console.log(`${result}`); + } +} + +const addData = (name, tags) => { + const id = idGenerator(); + + if (isDuplicated(id, "id", convertedData)) { + return addData(name, tags); + } + + let obj = { + name, + tags, + status: 'todo', + id, + }; + + convertedData.push(obj); + console.log(`${obj.name} 1개가 추가됐습니다.(id : ${obj.id})`); + showStatusLazy(); +} + +const deleteData = (id)=> { + const target = getArrByCondition(convertedData, (val) => {return val.id == id})[0]; + if (!target) {console.log('일치하는 id가 없습니다'); return;} + + convertedData.splice(convertedData.indexOf(target), 1); + console.log(`${target.name} ${target.status}가 목록에서 삭제됐습니다.`); + showStatusLazy(); +}; + +const updateData = (id, status)=> { + const target = getArrByCondition(convertedData, (val) => {return val.id == id})[0]; + if (!target) {console.log('일치하는 id가 없습니다'); return;} + + target.status = status; + + setTimeout(() => { + console.log(`${target.name}가 ${status}로 상태가 변경되었습니다`); + showStatusLazy(); + }, 3000); +} + +module.exports = { + "show" : showData, + "add" : addData, + "update" : updateData, + "delete" : deleteData, +}; \ No newline at end of file From 1aba53d29e8f13582d86cb703e63aaff1a182e86 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 22 Apr 2019 10:26:54 +0900 Subject: [PATCH 03/62] create app.js for program runnung --- app.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..2374120 --- /dev/null +++ b/app.js @@ -0,0 +1,65 @@ +const todos = require('./todos.js'); + +const readline = require('readline').createInterface({ + input: process.stdin, + output: process.stdout +}); + +const getCommandArrayByRegexp = (command) => { + const regexpForSeperateCommand = /[^\$]+/g; + return command.match(regexpForSeperateCommand); +}; + +const executeCommand = (commandArr) => { + if (commandArr.length == 2) { + todos[commandArr[0]](commandArr[1]); + } else if (commandArr.length == 3) { + todos[commandArr[0]](commandArr[1], commandArr[2]); + } +}; + +const isValidCommand = (command) => { + let result = false; + if (Object.keys(todos).includes(command)) result = true; + return result; +} + +const delay = (time) => { + return new Promise(function(resolve, reject){ + setTimeout(resolve, time); + }); +} + +const runProgram = (readline) => { + + readline.setPrompt('명령하세요: '); + readline.prompt(); + readline.on('line', (userInput) => { + + try { + const commandArr = getCommandArrayByRegexp(userInput); + const primaryCommand = commandArr[0]; + + if (!isValidCommand(primaryCommand)) { + console.log("올바르지 않은 명령어입니다") + readline.prompt(); + } + + delay(0) + .then(() => {executeCommand(commandArr); return delay(0)}) + .then(() => {if (primaryCommand != 'show') return delay(2000)}) + .then(() => {if (primaryCommand == 'update') return delay(2500)}) + .then(() => {readline.prompt(); return delay(0)}) + + } catch (error) { + console.log("명령어를 다시 입력해주세요") + readline.prompt(); + } + + }).on('close', () => { + console.log("프로그램을 종료합니다."); + process.exit(); + }); +} + +runProgram(readline); \ No newline at end of file From 0392d963a3c1a4464a21f2613239a69d9535ae43 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 22 Apr 2019 15:29:10 +0900 Subject: [PATCH 04/62] Rix the name of function rename getCommandArrayByRegexp to getCmdList --- app.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 2374120..35470bb 100644 --- a/app.js +++ b/app.js @@ -5,7 +5,7 @@ const readline = require('readline').createInterface({ output: process.stdout }); -const getCommandArrayByRegexp = (command) => { +const getCmdList = (command) => { const regexpForSeperateCommand = /[^\$]+/g; return command.match(regexpForSeperateCommand); }; @@ -37,7 +37,7 @@ const runProgram = (readline) => { readline.on('line', (userInput) => { try { - const commandArr = getCommandArrayByRegexp(userInput); + const commandArr = getCmdList(userInput); const primaryCommand = commandArr[0]; if (!isValidCommand(primaryCommand)) { From 9c75ce007b624b3c4b6aa8bd4613287e96ecef3c Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 22 Apr 2019 15:32:48 +0900 Subject: [PATCH 05/62] =?UTF-8?q?excuteCommand=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20:=20=20argument=20=EA=B0=9C=EC=88=98=20?= =?UTF-8?q?=ED=8C=90=EB=B3=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 인자 개수로 1개 초과 나올 경우 즉시 종료 후 프로그램 다시 실행 --- app.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app.js b/app.js index 35470bb..50892e7 100644 --- a/app.js +++ b/app.js @@ -11,6 +11,9 @@ const getCmdList = (command) => { }; const executeCommand = (commandArr) => { + + if (arguments.length != 1) return; + if (commandArr.length == 2) { todos[commandArr[0]](commandArr[1]); } else if (commandArr.length == 3) { From c78ff014dd639799ebc82d842cda9a4f3b9cb89d Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 22 Apr 2019 15:38:19 +0900 Subject: [PATCH 06/62] =?UTF-8?q?isValidCommand=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20:=20obeject=20argument=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 외부 변수에 접근하지 않고 객체를 인자로 받을 수 있도록 변경 --- app.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index 50892e7..874a42e 100644 --- a/app.js +++ b/app.js @@ -21,9 +21,9 @@ const executeCommand = (commandArr) => { } }; -const isValidCommand = (command) => { +const isValidCommand = (command, obj) => { let result = false; - if (Object.keys(todos).includes(command)) result = true; + if (Object.keys(obj).includes(command)) result = true; return result; } @@ -43,7 +43,7 @@ const runProgram = (readline) => { const commandArr = getCmdList(userInput); const primaryCommand = commandArr[0]; - if (!isValidCommand(primaryCommand)) { + if (!isValidCommand(primaryCommand, todos)) { console.log("올바르지 않은 명령어입니다") readline.prompt(); } From 3300bc8cc4962646deee339521178d94f765938a Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 22 Apr 2019 15:42:07 +0900 Subject: [PATCH 07/62] =?UTF-8?q?idGenerator=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20:=20=EC=83=81=EC=88=98=EA=B0=80=20?= =?UTF-8?q?=EC=95=84=EB=8B=8C=20=EC=9D=B8=EC=9E=90=EB=A1=9C=20random=20num?= =?UTF-8?q?ber=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 상수 99999 제거 후, max 값과 min 값으로 random 넘버 생성 --- todos.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/todos.js b/todos.js index 3771754..7e52656 100644 --- a/todos.js +++ b/todos.js @@ -1,7 +1,7 @@ //todos.js const originData = require('./todosList.json'); const convertedData = JSON.parse(JSON.stringify(originData)).data; -const idGenerator = () => Math.floor(Math.random() * (99999)) + 1; +const idGenerator = (max, min) => Math.floor(Math.random() * (max - min)) + 1; const getArrByCondition = (arr, condition) => { return arr.reduce((acc, val) => { @@ -41,7 +41,7 @@ const showData = (type) => { } const addData = (name, tags) => { - const id = idGenerator(); + const id = idGenerator(99999, 1); if (isDuplicated(id, "id", convertedData)) { return addData(name, tags); From 9394da2ca2b3226b086addc3b053272addafd757 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 22 Apr 2019 15:49:45 +0900 Subject: [PATCH 08/62] fix the Equal operator to strcit Equal operator --- todos.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/todos.js b/todos.js index 7e52656..796ed1d 100644 --- a/todos.js +++ b/todos.js @@ -19,21 +19,21 @@ const showStatusLazy = () => { const isDuplicated = (val, key, convertedData) => { let result = false; convertedData.forEach((element) => { - if (val == element[key]) result = true; + if (val === element[key]) result = true; }) return result; } const showData = (type) => { - if (type == 'all') { - const numOfTodos = getArrByCondition(convertedData, (val) => {return val.status == "todo"}).length; - const numOfDoings = getArrByCondition(convertedData, (val) => {return val.status == "doing"}).length; - const numOfDones = getArrByCondition(convertedData, (val) => {return val.status == "done"}).length; + if (type === 'all') { + const numOfTodos = getArrByCondition(convertedData, (val) => {return val.status === "todo"}).length; + const numOfDoings = getArrByCondition(convertedData, (val) => {return val.status === "doing"}).length; + const numOfDones = getArrByCondition(convertedData, (val) => {return val.status === "done"}).length; console.log(`현재상태 : todo: ${numOfTodos}개, doing: ${numOfDoings}개, done: ${numOfDones}개`); } else { - const objArr = getArrByCondition(convertedData, (val) => {return (type == val.status)}); + const objArr = getArrByCondition(convertedData, (val) => {return (type === val.status)}); let result = `${type} 리스트 : 총${objArr.length}건 :`; getArrByCondition(objArr, (val) => {result += `, '${val.name}, ${val.id}번'`; return true}); console.log(`${result}`); @@ -60,7 +60,7 @@ const addData = (name, tags) => { } const deleteData = (id)=> { - const target = getArrByCondition(convertedData, (val) => {return val.id == id})[0]; + const target = getArrByCondition(convertedData, (val) => {return val.id === id})[0]; if (!target) {console.log('일치하는 id가 없습니다'); return;} convertedData.splice(convertedData.indexOf(target), 1); @@ -69,7 +69,7 @@ const deleteData = (id)=> { }; const updateData = (id, status)=> { - const target = getArrByCondition(convertedData, (val) => {return val.id == id})[0]; + const target = getArrByCondition(convertedData, (val) => {return val.id === id})[0]; if (!target) {console.log('일치하는 id가 없습니다'); return;} target.status = status; From a69f0ed4dcad1fb704d84a20544fcc7a252b6b99 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 14:07:38 +0900 Subject: [PATCH 09/62] create todosData.json to load the mock data --- oop/todosData.json | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 oop/todosData.json diff --git a/oop/todosData.json b/oop/todosData.json new file mode 100644 index 0000000..61848e8 --- /dev/null +++ b/oop/todosData.json @@ -0,0 +1,23 @@ +{ + "result" : true, + "data": [ + { + "name" : "자바스크립트 공부하기", + "tags" : ["programming", "javascript"], + "status" : "todo", + "id" : 12123123 + }, + { + "name" : "그림 그리기", + "tags" : ["picture", "favorite"], + "status" : "doing", + "id" : 312323 + }, + { + "name" : "파이썬 공부하기", + "tags" : ["programming", "javascript"], + "status" : "done", + "id" : 213232 + } + ] +} \ No newline at end of file From 0fb09e84b4546a52803e3ec2f42a925bf853bb38 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 14:08:40 +0900 Subject: [PATCH 10/62] create app.js to run todos application --- oop/app.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 oop/app.js diff --git a/oop/app.js b/oop/app.js new file mode 100644 index 0000000..e69de29 From a687355a8743032f6f51f67ea75f88c94d4774e5 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 14:09:57 +0900 Subject: [PATCH 11/62] add the module : readLine module --- oop/app.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/oop/app.js b/oop/app.js index e69de29..fc2b085 100644 --- a/oop/app.js +++ b/oop/app.js @@ -0,0 +1,5 @@ +const readLine = require('readline').createInterface( { + input:process.stdin, + output:process.stdout, +}); + From 06f0dc4705124d9848d6bca78390da6811c69b16 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 14:11:08 +0900 Subject: [PATCH 12/62] add the program constructor --- oop/app.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/oop/app.js b/oop/app.js index fc2b085..6f87c1b 100644 --- a/oop/app.js +++ b/oop/app.js @@ -3,3 +3,18 @@ const readLine = require('readline').createInterface( { output:process.stdout, }); +Program.prototype = { + + runProgram : (readline) => { + readline.setPrompt('명령하세요: '); + readline.prompt(); + readline.on('line', (userInput) => { + readLine.prompt(); + + }).on('close', () => { + console.log("프로그램을 종료합니다."); + process.exit(); + }); + } + +}; \ No newline at end of file From fe7b8ef7c3f31d4ebfa602d6fa50f6ea51192572 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 14:11:37 +0900 Subject: [PATCH 13/62] add the run function : to run the program --- oop/app.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/oop/app.js b/oop/app.js index 6f87c1b..ef5abec 100644 --- a/oop/app.js +++ b/oop/app.js @@ -3,6 +3,9 @@ const readLine = require('readline').createInterface( { output:process.stdout, }); +function Program() { +} + Program.prototype = { runProgram : (readline) => { @@ -17,4 +20,10 @@ Program.prototype = { }); } -}; \ No newline at end of file +}; + + +const run = (() => { + const program = new Program(); + program.runProgram(readLine); +})(); \ No newline at end of file From 8d17131f241ecf8784972277d7d0304b1d1b8c23 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 14:13:20 +0900 Subject: [PATCH 14/62] create commandParser.js file : to parse the user's input String --- oop/commandParser.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 oop/commandParser.js diff --git a/oop/commandParser.js b/oop/commandParser.js new file mode 100644 index 0000000..73a1472 --- /dev/null +++ b/oop/commandParser.js @@ -0,0 +1,13 @@ +function CommandParser () { + +} + +CommandParser.prototype = { + getCmdList : (input) => { + const regexp = /[^\$]+/g; + return input.match(regexp); + }, +}; + +module.exports = CommandParser; + From 2fb6fa8b4bfc2b010c4d638b711c708b3eb9a5f6 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 14:14:43 +0900 Subject: [PATCH 15/62] add the executeCmd function Object : to execute the command line --- oop/commandParser.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/oop/commandParser.js b/oop/commandParser.js index 73a1472..7059b0d 100644 --- a/oop/commandParser.js +++ b/oop/commandParser.js @@ -3,10 +3,22 @@ function CommandParser () { } CommandParser.prototype = { - getCmdList : (input) => { + + getCmdList : (input) => { const regexp = /[^\$]+/g; return input.match(regexp); - }, + }, + + executeCmd : (command) => { + + if (command.length === 2) { + Instruction.prototype[command[0]](command[1]); + } else if (command.length === 3) { + Instruction.prototype[command[0]](command[1], command[2]); + } else { + // 예외처리 + } + } }; module.exports = CommandParser; From 5123c39865cc27a9865e0f5521d06bc01018f22e Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 14:15:43 +0900 Subject: [PATCH 16/62] create the instruction.js file : to instruct command --- oop/instruction.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 oop/instruction.js diff --git a/oop/instruction.js b/oop/instruction.js new file mode 100644 index 0000000..e510a88 --- /dev/null +++ b/oop/instruction.js @@ -0,0 +1,13 @@ +function Instruction() { + +} + +Instruction.prototype = { + + show : (status) => {console.log(status, "this is show method");}, + add : () => {}, + delete : () => {}, + update :() => {} +}; + +module.exports = Instruction; \ No newline at end of file From bc0d420d645fa01ec82073d0bebc489fa5c52dd7 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 14:16:26 +0900 Subject: [PATCH 17/62] add the Instruction module --- oop/commandParser.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/oop/commandParser.js b/oop/commandParser.js index 7059b0d..4e59d8f 100644 --- a/oop/commandParser.js +++ b/oop/commandParser.js @@ -1,3 +1,5 @@ +const Instruction = require('./instruction.js'); + function CommandParser () { } From 449de3ab307ee60e4b5899a5dfaaf07c97132bd1 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 14:17:57 +0900 Subject: [PATCH 18/62] implements the CommandParser method to parse the command line --- oop/app.js | 8 ++++++-- oop/commandParser.js | 1 - 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/oop/app.js b/oop/app.js index ef5abec..00a35ab 100644 --- a/oop/app.js +++ b/oop/app.js @@ -12,9 +12,13 @@ Program.prototype = { readline.setPrompt('명령하세요: '); readline.prompt(); readline.on('line', (userInput) => { - readLine.prompt(); - }).on('close', () => { + const cmdList = CommandParser.prototype.getCmdList(userInput); + CommandParser.prototype.executeCmd(cmdList); + + readLine.prompt(); + + }).on('close', () => { console.log("프로그램을 종료합니다."); process.exit(); }); diff --git a/oop/commandParser.js b/oop/commandParser.js index 4e59d8f..5e0e7ac 100644 --- a/oop/commandParser.js +++ b/oop/commandParser.js @@ -24,4 +24,3 @@ CommandParser.prototype = { }; module.exports = CommandParser; - From 56fa389bcca08895e78ed6b10037a123efc859fc Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 15:05:02 +0900 Subject: [PATCH 19/62] create utils.js to use common --- oop/utils.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 oop/utils.js diff --git a/oop/utils.js b/oop/utils.js new file mode 100644 index 0000000..e69de29 From 8f4d0775da484daa9b0f684a811bb1250c3cfb1e Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 15:06:20 +0900 Subject: [PATCH 20/62] add the getArrByCondition function : get array by condition arguments --- oop/utils.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/oop/utils.js b/oop/utils.js index e69de29..bfa588f 100644 --- a/oop/utils.js +++ b/oop/utils.js @@ -0,0 +1,15 @@ +function Utils () { + +} + +Utils.prototype = { + + getArrByCondition : (arr, condition) => { + return arr.reduce((acc, val) => { + if (condition(val)) acc.push(val); + return acc; + }, []); + }, +}; + +module.exports = Utils; \ No newline at end of file From 66e181a78d63ff76d4794eccd8e6e49ac294672d Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 15:07:17 +0900 Subject: [PATCH 21/62] add the modules : data status, utils moudule --- oop/instruction.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/oop/instruction.js b/oop/instruction.js index e510a88..8af03e0 100644 --- a/oop/instruction.js +++ b/oop/instruction.js @@ -1,3 +1,7 @@ +const originData = require('./todosdata.json'); +const convertedData = JSON.parse(JSON.stringify(originData)).data; +const Utils = require('./utils.js'); + function Instruction() { } From 132ad2d29709329252843b088ade3298ad8b6625 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 15:09:11 +0900 Subject: [PATCH 22/62] add the show function to show status of data --- oop/instruction.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/oop/instruction.js b/oop/instruction.js index 8af03e0..f32b811 100644 --- a/oop/instruction.js +++ b/oop/instruction.js @@ -8,7 +8,32 @@ function Instruction() { Instruction.prototype = { - show : (status) => {console.log(status, "this is show method");}, + show : (status) => { + const statusArr = ['all', 'todo', 'doing', 'done']; + + if (status === 'all') { + let [numOfTodos, numOfDoings, numOfDones] = [0,0,0]; + + convertedData.forEach((value) => { + + if (value.status === 'todo') numOfTodos++; + else if (value.status === 'doing') numOfDoings++; + else if (value.status === 'done') numOfDones++; + + }); + + console.log(`현재상태 : todo: ${numOfTodos}개, doing: ${numOfDoings}개, done: ${numOfDones}개`); + + } else if (statusArr.includes(status)) { + const tasks = Utils.prototype.getArrByCondition(convertedData, (val) => { return (status === val.status);}); + let message = `${status}리스트 총 ${tasks.length}건 : `; + tasks.forEach( obj => { + message += `'${obj.name}, ${obj.id}번,' `; + }); + + console.log(message); + } + }, add : () => {}, delete : () => {}, update :() => {} From bb27535505a5bcbc77a4dcba25ad2bdcd6a675b4 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 16:33:24 +0900 Subject: [PATCH 23/62] add getRandomId function --- oop/utils.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/oop/utils.js b/oop/utils.js index bfa588f..e931236 100644 --- a/oop/utils.js +++ b/oop/utils.js @@ -10,6 +10,12 @@ Utils.prototype = { return acc; }, []); }, + + getRadomId : (max, min) => { + Math.floor(Math.random() * (max-min)) + 1; + } + + }; module.exports = Utils; \ No newline at end of file From 1cacc05e0ca4b28a3649605c6a6ad2354cf278d9 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 16:33:59 +0900 Subject: [PATCH 24/62] add the show function --- oop/instruction.js | 1 + 1 file changed, 1 insertion(+) diff --git a/oop/instruction.js b/oop/instruction.js index f32b811..452dcc2 100644 --- a/oop/instruction.js +++ b/oop/instruction.js @@ -34,6 +34,7 @@ Instruction.prototype = { console.log(message); } }, + add : () => {}, delete : () => {}, update :() => {} From bc8f795f738b162db2e4bd5faee50ee612517376 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 16:35:26 +0900 Subject: [PATCH 25/62] add the adding object function --- oop/instruction.js | 67 +++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/oop/instruction.js b/oop/instruction.js index 452dcc2..84bd73d 100644 --- a/oop/instruction.js +++ b/oop/instruction.js @@ -3,41 +3,54 @@ const convertedData = JSON.parse(JSON.stringify(originData)).data; const Utils = require('./utils.js'); function Instruction() { - + } Instruction.prototype = { - show : (status) => { + show: (status) => { const statusArr = ['all', 'todo', 'doing', 'done']; - - if (status === 'all') { - let [numOfTodos, numOfDoings, numOfDones] = [0,0,0]; - - convertedData.forEach((value) => { - - if (value.status === 'todo') numOfTodos++; - else if (value.status === 'doing') numOfDoings++; - else if (value.status === 'done') numOfDones++; - - }); - + + if (status === 'all') { + let [numOfTodos, numOfDoings, numOfDones] = [0, 0, 0]; + + convertedData.forEach((value) => { + + if (value.status === 'todo') numOfTodos++; + else if (value.status === 'doing') numOfDoings++; + else if (value.status === 'done') numOfDones++; + + }); + console.log(`현재상태 : todo: ${numOfTodos}개, doing: ${numOfDoings}개, done: ${numOfDones}개`); - } else if (statusArr.includes(status)) { - const tasks = Utils.prototype.getArrByCondition(convertedData, (val) => { return (status === val.status);}); - let message = `${status}리스트 총 ${tasks.length}건 : `; - tasks.forEach( obj => { - message += `'${obj.name}, ${obj.id}번,' `; - }); - - console.log(message); - } + } else if (statusArr.includes(status)) { + const tasks = Utils.prototype.getArrByCondition(convertedData, (val) => { + return (status === val.status); + }); + let message = `${status}리스트 총 ${tasks.length}건 : `; + tasks.forEach(obj => { + message += `'${obj.name}, ${obj.id}번,' `; + }); + + console.log(message); + } }, - - add : () => {}, - delete : () => {}, - update :() => {} + + add: (name, tags) => { + const id = Utils.prototype.getRadomId(99999, 1); + let obj = { + name, + tags, + status: 'todo', + id, + }; + convertedData.push(obj); + console.log(`${obj.name} 1개가 추가됐습니다.(id : ${obj.id})`); + }, + + delete: () => {}, + update: () => {} }; module.exports = Instruction; \ No newline at end of file From acb59f886e11608fd6792bb421a5082a40740aee Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 16:37:02 +0900 Subject: [PATCH 26/62] add the delete function --- oop/instruction.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/oop/instruction.js b/oop/instruction.js index 84bd73d..10800a9 100644 --- a/oop/instruction.js +++ b/oop/instruction.js @@ -46,10 +46,19 @@ Instruction.prototype = { id, }; convertedData.push(obj); - console.log(`${obj.name} 1개가 추가됐습니다.(id : ${obj.id})`); + const message = `${obj.name} 1개가 추가됐습니다.(id : ${obj.id})`; + console.log(message); }, - delete: () => {}, + delete : (id) => { + const targetObj = Utils.prototype.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; + if (!targetObj) return; + + convertedData.splice(convertedData.indexOf(targetObj), 1); + let message = `${targetObj.name}이 ${targetObj.status}에서 삭제되었습니다.`; + console.log(message); + }, + update: () => {} }; From ad16068a5fd846800d3ad0c02ba0f4566b347867 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 16:37:45 +0900 Subject: [PATCH 27/62] add the update function : update object async function to update objcet --- oop/instruction.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/oop/instruction.js b/oop/instruction.js index 10800a9..eaa94ac 100644 --- a/oop/instruction.js +++ b/oop/instruction.js @@ -57,9 +57,18 @@ Instruction.prototype = { convertedData.splice(convertedData.indexOf(targetObj), 1); let message = `${targetObj.name}이 ${targetObj.status}에서 삭제되었습니다.`; console.log(message); - }, + }, + + update :(id, status) => { + const targetObj = Utils.prototype.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; + targetObj.status = status; - update: () => {} + const message = `${targetObj.name}가 ${targetObj.status}로 상태가 변경되었습니다`; + + setTimeout(() => { + console.log(message); + }, 3000); + }, }; module.exports = Instruction; \ No newline at end of file From 2a3347dc4c76670929141114c7275b3cedfb3742 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 16:39:49 +0900 Subject: [PATCH 28/62] =?UTF-8?q?=EC=9D=B8=EC=9E=90=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=9D=BC=20async=ED=95=98=EA=B2=8C=20=EC=9E=91=EB=8F=99?= =?UTF-8?q?=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8F=84=EB=A1=9D=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oop/app.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/oop/app.js b/oop/app.js index 00a35ab..b3bef6a 100644 --- a/oop/app.js +++ b/oop/app.js @@ -13,10 +13,15 @@ Program.prototype = { readline.prompt(); readline.on('line', (userInput) => { - const cmdList = CommandParser.prototype.getCmdList(userInput); - CommandParser.prototype.executeCmd(cmdList); - - readLine.prompt(); + const cmdList = CommandParser.prototype.getCmdList(userInput); + CommandParser.prototype.executeCmd(cmdList); + + Utils.prototype.delay(0) + .then(() => {if (cmdList[0] === 'update') return Utils.prototype.delay(3500);}) + .then(() => {return Utils.prototype.delay(1000);}) + .then(() => {if (cmdList[0] !== 'show') Instruction.prototype.show('all');}) + .catch(function(e) {console.log(e);}) + .then(() => {readLine.prompt();}); }).on('close', () => { console.log("프로그램을 종료합니다."); From c6c18dcfbf917c265b3413555c8f7e7222657eff Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 18:26:36 +0900 Subject: [PATCH 29/62] add the delay function : utils.js add the Promise function to delay the function --- oop/utils.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/oop/utils.js b/oop/utils.js index e931236..d5b5b05 100644 --- a/oop/utils.js +++ b/oop/utils.js @@ -13,9 +13,13 @@ Utils.prototype = { getRadomId : (max, min) => { Math.floor(Math.random() * (max-min)) + 1; - } - + }, + delay : (time) => { + return new Promise(function(resolve, reject){ + setTimeout(resolve, time); + }); + }, }; module.exports = Utils; \ No newline at end of file From 14d326f9e4672b64fe3b298c57d85e929963b46e Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 18:28:28 +0900 Subject: [PATCH 30/62] create exceptionHadling.js file --- oop/exceptionHandling.js | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 oop/exceptionHandling.js diff --git a/oop/exceptionHandling.js b/oop/exceptionHandling.js new file mode 100644 index 0000000..3c035ff --- /dev/null +++ b/oop/exceptionHandling.js @@ -0,0 +1,4 @@ +function CustomException() { + +} + From f7269e2acb57fff9dc7a718117346d73df960302 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 18:29:21 +0900 Subject: [PATCH 31/62] add the runtimeExceptions missingSeperatorException notExsitException sameStatusException commandMissingException --- oop/exceptionHandling.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/oop/exceptionHandling.js b/oop/exceptionHandling.js index 3c035ff..16be549 100644 --- a/oop/exceptionHandling.js +++ b/oop/exceptionHandling.js @@ -2,3 +2,29 @@ function CustomException() { } +CustomException.prototype = { + + missingSeperatorException : () => { + throw new Error("구분자 $가 존재하지 않습니다"); + }, + notExistIdException : () => { + throw new Error(`찾으시는 id가 존재하지 않습니다`); + }, + sameStatusException : () => { + throw new Error(`같은 상태로 업데이트 할 수 없습니다`); + }, + CommandMissingException : () => { + throw new Error(`올바른 명령어가 아닙니다`); + }, + + isValidSeperator : (input) => { + let result = true; + const regexp = /\$/g; + if (input.match(regexp) == null) result = false; + + return result; + } + +}; + +module.exports = CustomException; \ No newline at end of file From 5e352486dc0b5a73e2c5268eb4a77a8b86808da6 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 18:31:07 +0900 Subject: [PATCH 32/62] add the exception handling to run the application throw isValidSeperator Exception, isValidCommand catch and restart program --- oop/app.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/oop/app.js b/oop/app.js index b3bef6a..80f6506 100644 --- a/oop/app.js +++ b/oop/app.js @@ -11,9 +11,15 @@ Program.prototype = { runProgram : (readline) => { readline.setPrompt('명령하세요: '); readline.prompt(); - readline.on('line', (userInput) => { - + readline.on('line', (userInput) => { + + try { + + if (!ExceptionHandling.prototype.isValidSeperator(userInput)) {ExceptionHandling.prototype.missingSeperatorException();} const cmdList = CommandParser.prototype.getCmdList(userInput); + + if (!CommandParser.prototype.isValidCommand(cmdList[0], cmdArr)) {ExceptionHandling.prototype.CommandMissingException();} + CommandParser.prototype.executeCmd(cmdList); Utils.prototype.delay(0) @@ -22,6 +28,11 @@ Program.prototype = { .then(() => {if (cmdList[0] !== 'show') Instruction.prototype.show('all');}) .catch(function(e) {console.log(e);}) .then(() => {readLine.prompt();}); + + } catch(e) { + console.error(e.message); + readLine.prompt(); + } }).on('close', () => { console.log("프로그램을 종료합니다."); From eae8617aae86b7ca01ea7487487d5c6119495aed Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 18:33:39 +0900 Subject: [PATCH 33/62] add the modules to run program --- oop/app.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/oop/app.js b/oop/app.js index 80f6506..1be5d70 100644 --- a/oop/app.js +++ b/oop/app.js @@ -2,8 +2,13 @@ const readLine = require('readline').createInterface( { input:process.stdin, output:process.stdout, }); +const CommandParser = require('./commandParser.js'); +const Utils = require('./utils.js'); +const Instruction = require('./instruction.js'); +const ExceptionHandling = require('./exceptionHandling.js'); function Program() { + this.cmdArr = ['show','delete','update','add']; } Program.prototype = { @@ -12,7 +17,7 @@ Program.prototype = { readline.setPrompt('명령하세요: '); readline.prompt(); readline.on('line', (userInput) => { - + try { if (!ExceptionHandling.prototype.isValidSeperator(userInput)) {ExceptionHandling.prototype.missingSeperatorException();} @@ -45,5 +50,5 @@ Program.prototype = { const run = (() => { const program = new Program(); - program.runProgram(readLine); + program.runProgram(readLine, program.cmdArr); })(); \ No newline at end of file From 04875ac4212a7d7560ed9ba005bde7f7b5dc41d1 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 18:34:33 +0900 Subject: [PATCH 34/62] add the isValidCommand function --- oop/commandParser.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/oop/commandParser.js b/oop/commandParser.js index 5e0e7ac..fa19f97 100644 --- a/oop/commandParser.js +++ b/oop/commandParser.js @@ -20,7 +20,13 @@ CommandParser.prototype = { } else { // 예외처리 } - } + }, + + isValidCommand : (command, arr) => { + let result = false; + if (arr.includes(command)) result = true; + return result; + }, }; module.exports = CommandParser; From d85d5d2c21785a719d54677f749ceb5b96e417ac Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 18:35:36 +0900 Subject: [PATCH 35/62] add the exception handling about executCmd function --- oop/commandParser.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/oop/commandParser.js b/oop/commandParser.js index fa19f97..6d715c1 100644 --- a/oop/commandParser.js +++ b/oop/commandParser.js @@ -12,13 +12,19 @@ CommandParser.prototype = { }, executeCmd : (command) => { - - if (command.length === 2) { - Instruction.prototype[command[0]](command[1]); - } else if (command.length === 3) { - Instruction.prototype[command[0]](command[1], command[2]); - } else { - // 예외처리 + try { + + if (command.length === 2) { + Instruction.prototype[command[0]](command[1]); + } else if (command.length === 3) { + Instruction.prototype[command[0]](command[1], command[2]); + } else { + ExceptionHandling.prototype.CommandMissingException(); + } + + } catch (e) { + console.error(e.message); + return; } }, From 481a04910727bfa21d1f1dfb150bc1d5ea6a7067 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 18:36:41 +0900 Subject: [PATCH 36/62] add the module : ExceptionHandling module --- oop/commandParser.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/oop/commandParser.js b/oop/commandParser.js index 6d715c1..21f0ea1 100644 --- a/oop/commandParser.js +++ b/oop/commandParser.js @@ -1,4 +1,5 @@ const Instruction = require('./instruction.js'); +const ExceptionHandling = require('./exceptionHandling.js'); function CommandParser () { @@ -13,7 +14,7 @@ CommandParser.prototype = { executeCmd : (command) => { try { - + if (command.length === 2) { Instruction.prototype[command[0]](command[1]); } else if (command.length === 3) { From 3a7c33cc4600276dcc64726e764aaa16d1d2edbb Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 18:38:02 +0900 Subject: [PATCH 37/62] fix datas --- oop/todosData.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oop/todosData.json b/oop/todosData.json index 61848e8..282d7ba 100644 --- a/oop/todosData.json +++ b/oop/todosData.json @@ -5,7 +5,7 @@ "name" : "자바스크립트 공부하기", "tags" : ["programming", "javascript"], "status" : "todo", - "id" : 12123123 + "id" : 123 }, { "name" : "그림 그리기", From dfe2cef9b864723f5fed5ef671d3d4b645ea16c9 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 18:38:36 +0900 Subject: [PATCH 38/62] fix the runProgram function : missing argument --- oop/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oop/app.js b/oop/app.js index 1be5d70..1e4a4fa 100644 --- a/oop/app.js +++ b/oop/app.js @@ -13,7 +13,7 @@ function Program() { Program.prototype = { - runProgram : (readline) => { + runProgram : (readline, cmdArr) => { readline.setPrompt('명령하세요: '); readline.prompt(); readline.on('line', (userInput) => { From b81a4464f9d1a413bb40f3b8551b8f9c62f83838 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Thu, 25 Apr 2019 18:41:43 +0900 Subject: [PATCH 39/62] add the exception handling about add, update function --- oop/instruction.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/oop/instruction.js b/oop/instruction.js index eaa94ac..897f645 100644 --- a/oop/instruction.js +++ b/oop/instruction.js @@ -1,9 +1,10 @@ const originData = require('./todosdata.json'); const convertedData = JSON.parse(JSON.stringify(originData)).data; const Utils = require('./utils.js'); +const ExceptionHandling = require('./exceptionHandling.js'); function Instruction() { - + } Instruction.prototype = { @@ -51,16 +52,31 @@ Instruction.prototype = { }, delete : (id) => { - const targetObj = Utils.prototype.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; - if (!targetObj) return; + const targetObj = Utils.prototype.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; + + try { + if (!targetObj) ExceptionHandling.prototype.notExistIdException(); + } catch (e) { + console.error(e.message); + return; + } convertedData.splice(convertedData.indexOf(targetObj), 1); let message = `${targetObj.name}이 ${targetObj.status}에서 삭제되었습니다.`; console.log(message); + }, update :(id, status) => { - const targetObj = Utils.prototype.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; + const targetObj = Utils.prototype.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; + try { + if (!targetObj) ExceptionHandling.prototype.notExistIdException(); + if (targetObj.status === status) ExceptionHandling.prototype.sameStatusException(); + } catch (e) { + console.error(e.message); + return; + } + targetObj.status = status; const message = `${targetObj.name}가 ${targetObj.status}로 상태가 변경되었습니다`; From 1abed43256a1b11daf62d1b4d60dbdd036a5742b Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Fri, 26 Apr 2019 12:21:30 +0900 Subject: [PATCH 40/62] Rename constructor function : Program to RunTodoApp --- oop/app.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/oop/app.js b/oop/app.js index 1e4a4fa..607ec94 100644 --- a/oop/app.js +++ b/oop/app.js @@ -7,13 +7,13 @@ const Utils = require('./utils.js'); const Instruction = require('./instruction.js'); const ExceptionHandling = require('./exceptionHandling.js'); -function Program() { +function RunTodoApp() { this.cmdArr = ['show','delete','update','add']; } -Program.prototype = { +RunTodoApp.prototype = { - runProgram : (readline, cmdArr) => { + runProgram : (readline) => { readline.setPrompt('명령하세요: '); readline.prompt(); readline.on('line', (userInput) => { @@ -49,6 +49,6 @@ Program.prototype = { const run = (() => { - const program = new Program(); - program.runProgram(readLine, program.cmdArr); + const runTodoApp = new RunTodoApp(); + runTodoApp.runProgram(readLine); })(); \ No newline at end of file From 177979431578759d561132bb25297159e3d8c9e0 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Fri, 26 Apr 2019 12:24:41 +0900 Subject: [PATCH 41/62] Add module Objects in RunTodoApp constructor add cmdArr, commandParser, utils, instruction, customException module Obejct --- oop/app.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/oop/app.js b/oop/app.js index 607ec94..8a0fa61 100644 --- a/oop/app.js +++ b/oop/app.js @@ -8,12 +8,16 @@ const Instruction = require('./instruction.js'); const ExceptionHandling = require('./exceptionHandling.js'); function RunTodoApp() { - this.cmdArr = ['show','delete','update','add']; + this.cmdArr = ['show','delete','update','add']; + this.commandParser = new CommandParser(); + this.utils = new Utils(); + this.instruction = new Instruction(); + this.customException = new CustomException(); } RunTodoApp.prototype = { - runProgram : (readline) => { + runProgram (readline) { readline.setPrompt('명령하세요: '); readline.prompt(); readline.on('line', (userInput) => { From f0e6b23af87c8cb56955a01d3c55e1ae23c44eb4 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Fri, 26 Apr 2019 12:37:39 +0900 Subject: [PATCH 42/62] =?UTF-8?q?Instruction=20=EC=83=9D=EC=84=B1=EC=9E=90?= =?UTF-8?q?=EC=97=90=20=EB=AA=A8=EB=93=88=20=EA=B0=9D=EC=B2=B4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instruction 생성 시 utils, customException 객체 생성 되도록 추가 --- oop/instruction.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/oop/instruction.js b/oop/instruction.js index 897f645..f459e6d 100644 --- a/oop/instruction.js +++ b/oop/instruction.js @@ -1,10 +1,12 @@ const originData = require('./todosdata.json'); const convertedData = JSON.parse(JSON.stringify(originData)).data; + const Utils = require('./utils.js'); -const ExceptionHandling = require('./exceptionHandling.js'); +const CustomException = require('./customException.js'); function Instruction() { - + this.utils = new Utils(); + this.customException = new CustomException(); } Instruction.prototype = { @@ -69,7 +71,8 @@ Instruction.prototype = { update :(id, status) => { const targetObj = Utils.prototype.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; - try { + + try { if (!targetObj) ExceptionHandling.prototype.notExistIdException(); if (targetObj.status === status) ExceptionHandling.prototype.sameStatusException(); } catch (e) { From c8b894d48f8dcdc44dacd335db7f015d02018feb Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Fri, 26 Apr 2019 12:40:12 +0900 Subject: [PATCH 43/62] =?UTF-8?q?show=20=ED=95=A8=EC=88=98=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81=20:=20=EB=82=B4=EB=B6=80=20?= =?UTF-8?q?=EB=B6=84=EA=B8=B0=EB=A5=BC=20everyStatus,=20sigleStatus=20func?= =?UTF-8?q?tion=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oop/instruction.js | 78 ++++++++++++++-------------------------------- 1 file changed, 24 insertions(+), 54 deletions(-) diff --git a/oop/instruction.js b/oop/instruction.js index f459e6d..b9cbf4a 100644 --- a/oop/instruction.js +++ b/oop/instruction.js @@ -11,63 +11,33 @@ function Instruction() { Instruction.prototype = { - show: (status) => { - const statusArr = ['all', 'todo', 'doing', 'done']; - - if (status === 'all') { - let [numOfTodos, numOfDoings, numOfDones] = [0, 0, 0]; - - convertedData.forEach((value) => { - - if (value.status === 'todo') numOfTodos++; - else if (value.status === 'doing') numOfDoings++; - else if (value.status === 'done') numOfDones++; - - }); - - console.log(`현재상태 : todo: ${numOfTodos}개, doing: ${numOfDoings}개, done: ${numOfDones}개`); - - } else if (statusArr.includes(status)) { - const tasks = Utils.prototype.getArrByCondition(convertedData, (val) => { - return (status === val.status); - }); - let message = `${status}리스트 총 ${tasks.length}건 : `; - tasks.forEach(obj => { - message += `'${obj.name}, ${obj.id}번,' `; - }); - - console.log(message); - } - }, - - add: (name, tags) => { - const id = Utils.prototype.getRadomId(99999, 1); - let obj = { - name, - tags, - status: 'todo', - id, - }; - convertedData.push(obj); - const message = `${obj.name} 1개가 추가됐습니다.(id : ${obj.id})`; + everyStatus (convertedData) { + let [numOfTodos, numOfDoings, numOfDones] = [0,0,0]; + convertedData.forEach((value) => { + if (value.status === 'todo') numOfTodos++; + else if (value.status === 'doing') numOfDoings++; + else if (value.status === 'done') numOfDones++; + }); + console.log(`현재상태 : todo: ${numOfTodos}개, doing: ${numOfDoings}개, done: ${numOfDones}개`); + }, + + singleStatus (convertedData, status) { + const tasks = this.utils.getArrByCondition(convertedData, (val) => { return (status === val.status);}); + let message = `${status}리스트 총 ${tasks.length}건 : `; + tasks.forEach( obj => { + message += `'${obj.name}, ${obj.id}번,' `; + }); console.log(message); - }, + }, - delete : (id) => { - const targetObj = Utils.prototype.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; - - try { - if (!targetObj) ExceptionHandling.prototype.notExistIdException(); - } catch (e) { - console.error(e.message); - return; + show (status) { + const statusArr = ['all', 'todo', 'doing', 'done']; + if (status === 'all') { + this.everyStatus(convertedData); + } else if (statusArr.includes(status)) { + this.singleStatus(convertedData, status); } - - convertedData.splice(convertedData.indexOf(targetObj), 1); - let message = `${targetObj.name}이 ${targetObj.status}에서 삭제되었습니다.`; - console.log(message); - - }, + }, update :(id, status) => { const targetObj = Utils.prototype.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; From 31a8dc0b8873ca36e421e40577c1ac469ccbfbca Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Fri, 26 Apr 2019 12:42:42 +0900 Subject: [PATCH 44/62] =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=9E=90=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=EB=A5=BC=20=EC=9D=B4=EC=9A=A9=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20prototype=20object=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oop/instruction.js | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/oop/instruction.js b/oop/instruction.js index b9cbf4a..e55252b 100644 --- a/oop/instruction.js +++ b/oop/instruction.js @@ -39,25 +39,47 @@ Instruction.prototype = { } }, - update :(id, status) => { - const targetObj = Utils.prototype.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; - - try { - if (!targetObj) ExceptionHandling.prototype.notExistIdException(); - if (targetObj.status === status) ExceptionHandling.prototype.sameStatusException(); + add (name, tags) { + const id = this.utils.getRadomId(this.maxIdNum, this.minIdNum); + let obj = { + name, + tags, + status: 'todo', + id, + }; + convertedData.push(obj); + const message = `${obj.name} 1개가 추가됐습니다.(id : ${obj.id})`; + console.log(message); + }, + + delete (id) { + const targetObj = this.utils.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; + try { + if (!targetObj) this.customException.notExistIdException(); + } catch (e) { + console.error(e.message); + return; + } + convertedData.splice(convertedData.indexOf(targetObj), 1); + let message = `${targetObj.name}이 ${targetObj.status}에서 삭제되었습니다.`; + console.log(message); + }, + + update (id, status) { + const targetObj = this.utils.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; + try { + if (!targetObj) this.customException.notExistIdException(); + if (targetObj.status === status) this.customException.sameStatusException(); } catch (e) { console.error(e.message); return; } - targetObj.status = status; - const message = `${targetObj.name}가 ${targetObj.status}로 상태가 변경되었습니다`; - setTimeout(() => { - console.log(message); + console.log(message); }, 3000); - }, + } }; module.exports = Instruction; \ No newline at end of file From 1cabb8b07f0e1e1bc4ba21340af360d70d02df7b Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Fri, 26 Apr 2019 12:48:32 +0900 Subject: [PATCH 45/62] =?UTF-8?q?CustomerException=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oop/exceptionHandling.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/oop/exceptionHandling.js b/oop/exceptionHandling.js index 16be549..c3f3a78 100644 --- a/oop/exceptionHandling.js +++ b/oop/exceptionHandling.js @@ -1,29 +1,36 @@ function CustomException() { - + } CustomException.prototype = { - missingSeperatorException : () => { - throw new Error("구분자 $가 존재하지 않습니다"); + missingSeperatorException (input) { + if (!this.isValidSeperator(input)) throw new Error("구분자 $가 존재하지 않습니다"); }, - notExistIdException : () => { + notExistIdException () { throw new Error(`찾으시는 id가 존재하지 않습니다`); }, - sameStatusException : () => { + sameStatusException () { throw new Error(`같은 상태로 업데이트 할 수 없습니다`); }, - CommandMissingException : () => { + + CommandMissingException (command, arr) { + if (!this.isValidCommand(command, arr)) throw new Error(`올바른 명령어가 아닙니다`); }, - isValidSeperator : (input) => { + isValidSeperator (input) { let result = true; const regexp = /\$/g; if (input.match(regexp) == null) result = false; return result; - } + }, + isValidCommand(command, arr) { + let result = false; + if (arr.includes(command)) result = true; + return result; + }, }; From 9b2104efd279ed4f91b24091241c72ed0b2c6872 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Fri, 26 Apr 2019 12:50:54 +0900 Subject: [PATCH 46/62] =?UTF-8?q?getRandomId=20function=EC=97=90=EC=84=9C?= =?UTF-8?q?=20=EB=A7=A4=EC=A7=81=EB=84=98=EB=B2=84=20=EC=82=AD=EC=A0=9C,?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1=EC=9E=90=20=EC=9D=B8=EC=88=98=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 생성자에서 max 값과 min 값을 받을 수 있도록 수정 --- oop/instruction.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/oop/instruction.js b/oop/instruction.js index e55252b..c1838e8 100644 --- a/oop/instruction.js +++ b/oop/instruction.js @@ -7,6 +7,8 @@ const CustomException = require('./customException.js'); function Instruction() { this.utils = new Utils(); this.customException = new CustomException(); + this.minIdNum = 1; + this.maxIdNum = 99999; } Instruction.prototype = { From 049fcf2eef59080064a310e3b75cf753c15c4be1 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Fri, 26 Apr 2019 12:58:33 +0900 Subject: [PATCH 47/62] =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=9E=90=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=EC=97=90=20=EB=AA=A8=EB=93=88=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oop/commandParser.js | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/oop/commandParser.js b/oop/commandParser.js index 21f0ea1..86a0427 100644 --- a/oop/commandParser.js +++ b/oop/commandParser.js @@ -1,39 +1,40 @@ const Instruction = require('./instruction.js'); -const ExceptionHandling = require('./exceptionHandling.js'); +const CustomException = require('./customException.js'); function CommandParser () { - + this.instruction = new Instruction(); + this.customException = new CustomException(); } CommandParser.prototype = { - - getCmdList : (input) => { + getCmdList(input) { const regexp = /[^\$]+/g; return input.match(regexp); - }, - - executeCmd : (command) => { + }, + + executeCmd(command) { try { - + if (command.length === 2) { - Instruction.prototype[command[0]](command[1]); + instruction[command[0]](command[1]); } else if (command.length === 3) { - Instruction.prototype[command[0]](command[1], command[2]); + instruction[command[0]](command[1], command[2]); } else { - ExceptionHandling.prototype.CommandMissingException(); - } - + customException.CommandMissingException(); + } + } catch (e) { console.error(e.message); return; } - }, - - isValidCommand : (command, arr) => { - let result = false; - if (arr.includes(command)) result = true; - return result; - }, + + }, + + isValidCommand(command, arr) { + let result = false; + if (arr.includes(command)) result = true; + return result; + }, }; -module.exports = CommandParser; +module.exports = CommandParser; \ No newline at end of file From 3a3a29475390d3068b6f4786f1b4aa935bca9aba Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Fri, 26 Apr 2019 13:08:20 +0900 Subject: [PATCH 48/62] Rename ExceptionHandling to CustomException --- oop/CustomException.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 oop/CustomException.js diff --git a/oop/CustomException.js b/oop/CustomException.js new file mode 100644 index 0000000..c3f3a78 --- /dev/null +++ b/oop/CustomException.js @@ -0,0 +1,37 @@ +function CustomException() { + +} + +CustomException.prototype = { + + missingSeperatorException (input) { + if (!this.isValidSeperator(input)) throw new Error("구분자 $가 존재하지 않습니다"); + }, + notExistIdException () { + throw new Error(`찾으시는 id가 존재하지 않습니다`); + }, + sameStatusException () { + throw new Error(`같은 상태로 업데이트 할 수 없습니다`); + }, + + CommandMissingException (command, arr) { + if (!this.isValidCommand(command, arr)) + throw new Error(`올바른 명령어가 아닙니다`); + }, + + isValidSeperator (input) { + let result = true; + const regexp = /\$/g; + if (input.match(regexp) == null) result = false; + + return result; + }, + isValidCommand(command, arr) { + let result = false; + if (arr.includes(command)) result = true; + return result; + }, + +}; + +module.exports = CustomException; \ No newline at end of file From 5ad5c2e4de73c7442f454c07229f3bc1077f3355 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Fri, 26 Apr 2019 13:11:02 +0900 Subject: [PATCH 49/62] =?UTF-8?q?=ED=95=A8=EC=88=98=20=EC=9D=B4=EB=A6=84?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD=20:=20=20getradomId=20to=20getRandomID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oop/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/oop/utils.js b/oop/utils.js index d5b5b05..684baf9 100644 --- a/oop/utils.js +++ b/oop/utils.js @@ -11,8 +11,8 @@ Utils.prototype = { }, []); }, - getRadomId : (max, min) => { - Math.floor(Math.random() * (max-min)) + 1; + getRandomId : (max, min) => { + return Math.floor(Math.random() * (max-min)) + 1; }, delay : (time) => { From 88a39c746f7e364977717655120b00442d635579 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Fri, 26 Apr 2019 13:11:54 +0900 Subject: [PATCH 50/62] =?UTF-8?q?=ED=95=A8=EC=88=98=20=EB=B0=94=EB=80=90?= =?UTF-8?q?=20=EC=9D=B4=EB=A6=84=20getRandomID=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oop/app.js | 23 +++++++++++------------ oop/commandParser.js | 6 +++--- oop/exceptionHandling.js | 37 ------------------------------------- oop/instruction.js | 2 +- 4 files changed, 15 insertions(+), 53 deletions(-) delete mode 100644 oop/exceptionHandling.js diff --git a/oop/app.js b/oop/app.js index 8a0fa61..3a0b6ed 100644 --- a/oop/app.js +++ b/oop/app.js @@ -5,7 +5,7 @@ const readLine = require('readline').createInterface( { const CommandParser = require('./commandParser.js'); const Utils = require('./utils.js'); const Instruction = require('./instruction.js'); -const ExceptionHandling = require('./exceptionHandling.js'); +const CustomException = require('./CustomException.js'); function RunTodoApp() { this.cmdArr = ['show','delete','update','add']; @@ -23,21 +23,20 @@ RunTodoApp.prototype = { readline.on('line', (userInput) => { try { - - if (!ExceptionHandling.prototype.isValidSeperator(userInput)) {ExceptionHandling.prototype.missingSeperatorException();} - const cmdList = CommandParser.prototype.getCmdList(userInput); + + this.customException.missingSeperatorException(userInput); + const cmdList = this.commandParser.getCmdList(userInput); - if (!CommandParser.prototype.isValidCommand(cmdList[0], cmdArr)) {ExceptionHandling.prototype.CommandMissingException();} - - CommandParser.prototype.executeCmd(cmdList); + this.customException.CommandMissingException(cmdList[0], this.cmdArr); + this.commandParser.executeCmd(cmdList); - Utils.prototype.delay(0) - .then(() => {if (cmdList[0] === 'update') return Utils.prototype.delay(3500);}) - .then(() => {return Utils.prototype.delay(1000);}) - .then(() => {if (cmdList[0] !== 'show') Instruction.prototype.show('all');}) + this.utils.delay(0) + .then(() => {if (cmdList[0] === 'update') return this.utils.delay(3500);}) + .then(() => {return this.utils.delay(1000);}) + .then(() => {if (cmdList[0] !== 'show') this.instruction.show('all');}) .catch(function(e) {console.log(e);}) .then(() => {readLine.prompt();}); - + } catch(e) { console.error(e.message); readLine.prompt(); diff --git a/oop/commandParser.js b/oop/commandParser.js index 86a0427..1cbeed3 100644 --- a/oop/commandParser.js +++ b/oop/commandParser.js @@ -16,11 +16,11 @@ CommandParser.prototype = { try { if (command.length === 2) { - instruction[command[0]](command[1]); + this.instruction[command[0]](command[1]); } else if (command.length === 3) { - instruction[command[0]](command[1], command[2]); + this.instruction[command[0]](command[1], command[2]); } else { - customException.CommandMissingException(); + this.customException.CommandMissingException(); } } catch (e) { diff --git a/oop/exceptionHandling.js b/oop/exceptionHandling.js deleted file mode 100644 index c3f3a78..0000000 --- a/oop/exceptionHandling.js +++ /dev/null @@ -1,37 +0,0 @@ -function CustomException() { - -} - -CustomException.prototype = { - - missingSeperatorException (input) { - if (!this.isValidSeperator(input)) throw new Error("구분자 $가 존재하지 않습니다"); - }, - notExistIdException () { - throw new Error(`찾으시는 id가 존재하지 않습니다`); - }, - sameStatusException () { - throw new Error(`같은 상태로 업데이트 할 수 없습니다`); - }, - - CommandMissingException (command, arr) { - if (!this.isValidCommand(command, arr)) - throw new Error(`올바른 명령어가 아닙니다`); - }, - - isValidSeperator (input) { - let result = true; - const regexp = /\$/g; - if (input.match(regexp) == null) result = false; - - return result; - }, - isValidCommand(command, arr) { - let result = false; - if (arr.includes(command)) result = true; - return result; - }, - -}; - -module.exports = CustomException; \ No newline at end of file diff --git a/oop/instruction.js b/oop/instruction.js index c1838e8..f86f5b6 100644 --- a/oop/instruction.js +++ b/oop/instruction.js @@ -42,7 +42,7 @@ Instruction.prototype = { }, add (name, tags) { - const id = this.utils.getRadomId(this.maxIdNum, this.minIdNum); + const id = this.utils.getRandomId(this.maxIdNum, this.minIdNum); let obj = { name, tags, From 99fa6ec2755f35954d286dc32157ad2b75850c3a Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 29 Apr 2019 19:30:29 +0900 Subject: [PATCH 51/62] create init data --- class/todosList.json | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 class/todosList.json diff --git a/class/todosList.json b/class/todosList.json new file mode 100644 index 0000000..1734fe5 --- /dev/null +++ b/class/todosList.json @@ -0,0 +1,41 @@ +{ + "result" : true, + "data": [ + { + "name" : "자바스크립트 공부하기", + "tags" : ["programming", "javascript"], + "status" : "todo", + "id" : 12123123 + }, + { + "name" : "그림 그리기", + "tags" : ["picture", "favorite"], + "status" : "doing", + "id" : 123 + }, + { + "name" : "파이썬 공부하기", + "tags" : ["programming", "javascript"], + "status" : "done", + "id" : 213232 + }, + { + "name" : "자바 공부하기", + "tags" : ["programming", "java"], + "status" : "todo", + "id" : 123345 + }, + { + "name" : "음악 듣기", + "tags" : ["music", "favorite"], + "status" : "doing", + "id" : 12123 + }, + { + "name" : "정규식 공부하기", + "tags" : ["programming", "RegExp"], + "status" : "done", + "id" : 1234 + } + ] +} From b3ab8a3761be73d16d4d116260957006c437c8f9 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 29 Apr 2019 19:34:16 +0900 Subject: [PATCH 52/62] Refactoring app.js : prototype pattern to class --- class/app.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 class/app.js diff --git a/class/app.js b/class/app.js new file mode 100644 index 0000000..2d67124 --- /dev/null +++ b/class/app.js @@ -0,0 +1,55 @@ +const readLine = require('readline').createInterface( { + input:process.stdin, + output:process.stdout, +}); +const CommandParser = require('./commandParser.js'); +const Utils = require('./utils.js'); +const Instruction = require('./instruction.js'); +const CustomException = require('./customException.js'); + +const RunTodoApp = class { + constructor() { + this.cmdArr = ['show','delete','update','add','undo','redo']; + this.commandParser = new CommandParser(); + this.utils = new Utils(); + this.instruction = new Instruction(); + this.customException = new CustomException(); + } + + runProgram (readline) { + readline.setPrompt('명령하세요: '); + readline.prompt(); + readline.on('line', (userInput) => { + + try { + + this.customException.missingSeperatorException(userInput); + const cmdList = this.commandParser.getCmdList(userInput); + + this.customException.CommandMissingException(cmdList[0], this.cmdArr); + this.commandParser.executeCmd(cmdList); + + this.utils.delay(0) + .then(() => {if (cmdList[0] === 'update') return this.utils.delay(3500);}) + .then(() => {return this.utils.delay(1000);}) + .then(() => {if (cmdList[0] !== 'show') this.instruction.show('all');}) + .catch(function(e) {console.log(e);}) + .then(() => {readLine.prompt();}); + + } catch(e) { + console.error(e.message); + readLine.prompt(); + } + + }).on('close', () => { + console.log("프로그램을 종료합니다."); + process.exit(); + }); + } + +}; + +const run = (() => { + const runTodoApp = new RunTodoApp(); + runTodoApp.runProgram(readLine); +})(); \ No newline at end of file From 7129436aef2e6d70990a7db0a9074821558aef1d Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 29 Apr 2019 19:36:13 +0900 Subject: [PATCH 53/62] Refactoring customException.js prototype pattern to class --- class/customException.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 class/customException.js diff --git a/class/customException.js b/class/customException.js new file mode 100644 index 0000000..84db372 --- /dev/null +++ b/class/customException.js @@ -0,0 +1,34 @@ +const CustomException = class { + missingSeperatorException (input) { + if (!this.isValidSeperator(input)) throw new Error("구분자 $가 존재하지 않습니다"); + } + + notExistIdException () { + throw new Error(`찾으시는 id가 존재하지 않습니다`); + } + + sameStatusException () { + throw new Error(`같은 상태로 업데이트 할 수 없습니다`); + } + + CommandMissingException (command, arr) { + if (!this.isValidCommand(command, arr)) + throw new Error(`올바른 명령어가 아닙니다`); + } + + isValidSeperator (input) { + let result = true; + const regexp = /\$|undo|redo/g; + if (input.match(regexp) == null) result = false; + + return result; + } + + isValidCommand(command, arr) { + let result = false; + if (arr.includes(command)) result = true; + return result; + } +}; + +module.exports = CustomException; \ No newline at end of file From 348abc19a69e919b1d052c057de5d00d01189a12 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 29 Apr 2019 19:37:26 +0900 Subject: [PATCH 54/62] Refactoring utils.js : prototype to class --- class/utils.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 class/utils.js diff --git a/class/utils.js b/class/utils.js new file mode 100644 index 0000000..c39e17b --- /dev/null +++ b/class/utils.js @@ -0,0 +1,21 @@ +const Utils = class { + + getArrByCondition (arr, condition) { + return arr.reduce((acc, val) => { + if (condition(val)) acc.push(val); + return acc; + }, []); + } + + getRandomId (max, min) { + return Math.floor(Math.random() * (max-min)) + 1; + } + + delay (time) { + return new Promise(function(resolve, reject){ + setTimeout(resolve, time); + }); + } +}; + +module.exports = Utils; \ No newline at end of file From 587e2aae619cced300d448e89d91422d064b4fc7 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 29 Apr 2019 19:38:28 +0900 Subject: [PATCH 55/62] Feat utils.js : add deepCopy method --- class/utils.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/class/utils.js b/class/utils.js index c39e17b..4645791 100644 --- a/class/utils.js +++ b/class/utils.js @@ -15,7 +15,17 @@ const Utils = class { return new Promise(function(resolve, reject){ setTimeout(resolve, time); }); - } + } + + deepCopy (obj) { + if (obj === null || typeof(obj) !== "object") return obj; + + let copy = {}; + for(let key in obj) { + copy[key] = this.deepCopy(obj[key]); + } + return copy; + } }; module.exports = Utils; \ No newline at end of file From 29a123a9740fe41aab8d8a2b4964d251287b350e Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 29 Apr 2019 19:40:44 +0900 Subject: [PATCH 56/62] Rename : todosList.js to todosdata.js --- class/{todosList.json => todosdata.json} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename class/{todosList.json => todosdata.json} (100%) diff --git a/class/todosList.json b/class/todosdata.json similarity index 100% rename from class/todosList.json rename to class/todosdata.json From d8ec3e9238f8a368664a61e5946bef68301d408c Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 29 Apr 2019 19:42:18 +0900 Subject: [PATCH 57/62] Refactoring : prototype pattern to class --- class/commandParser.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 class/commandParser.js diff --git a/class/commandParser.js b/class/commandParser.js new file mode 100644 index 0000000..8c4b88e --- /dev/null +++ b/class/commandParser.js @@ -0,0 +1,42 @@ +const Instruction = require('./instruction.js'); +const CustomException = require('./customException.js'); + +const CommandParser = class { + constructor() { + this.instruction = new Instruction(); + this.customException = new CustomException(); + } + + getCmdList(input) { + const regexp = /[^\$]+|undo|redo/g; + return input.match(regexp); + } + + executeCmd(command) { + try { + + if (command.length === 1) { + this.instruction[command[0]](); + } else if (command.length === 2) { + this.instruction[command[0]](command[1]); + } else if (command.length === 3) { + this.instruction[command[0]](command[1], command[2]); + } else { + this.customException.CommandMissingException(); + } + + } catch (e) { + console.error(e.message); + return; + } + + } + + isValidCommand(command, arr) { + let result = false; + if (arr.includes(command)) result = true; + return result; + } +}; + +module.exports = CommandParser; \ No newline at end of file From 4889e9328800774d7802d03102a2e83730021db1 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 29 Apr 2019 19:43:39 +0900 Subject: [PATCH 58/62] Refactor instruction.js : prototype to class --- class/instruction.js | 137 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 class/instruction.js diff --git a/class/instruction.js b/class/instruction.js new file mode 100644 index 0000000..0b335fe --- /dev/null +++ b/class/instruction.js @@ -0,0 +1,137 @@ +const originData = require('./todosdata.json'); +const convertedData = JSON.parse(JSON.stringify(originData)).data; +const Utils = require('./utils.js'); +const CustomException = require('./customException.js'); + +const Instruction = class { + constructor() { + this.utils = new Utils(); + this.customException = new CustomException(); + this.minIdNum = 1; + this.maxIdNum = 99999; + } + + everyStatus (convertedData) { + let [numOfTodos, numOfDoings, numOfDones] = [0,0,0]; + convertedData.forEach((value) => { + if (value.status === 'todo') numOfTodos++; + else if (value.status === 'doing') numOfDoings++; + else if (value.status === 'done') numOfDones++; + }); + console.log(`현재상태 : todo: ${numOfTodos}개, doing: ${numOfDoings}개, done: ${numOfDones}개`); + } + + singleStatus (convertedData, status) { + const tasks = this.utils.getArrByCondition(convertedData, (val) => { return (status === val.status);}); + let message = `${status}리스트 총 ${tasks.length}건 : `; + tasks.forEach( obj => { + message += `'${obj.name}, ${obj.id}번,' `; + }); + console.log(message); + } + + show(status) { + const statusArr = ['all', 'todo', 'doing', 'done']; + if (status === 'all') { + this.everyStatus(convertedData); + } else if (statusArr.includes(status)) { + this.singleStatus(convertedData, status); + } + } + + add(name, tags) { + const id = this.utils.getRandomId(this.maxIdNum, this.minIdNum); + let obj = { + name, + tags, + status: 'todo', + id, + }; + convertedData.push(obj); + + if (arguments.length !== 3) { + this.createHistoryObj('add' ,obj); + } + + const message = `${obj.name} 1개가 추가됐습니다.(id : ${obj.id})`; + console.log(message); + } + + delete(id) { + const targetObj = this.utils.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; + try { + if (!targetObj) this.customException.notExistIdException(); + } catch (e) { + console.error(e.message); + return; + } + convertedData.splice(convertedData.indexOf(targetObj), 1); + + if (arguments.length == 1) { + this.createHistoryObj('delete', obj); + } + let message = `${targetObj.name}이 ${targetObj.status}에서 삭제되었습니다.`; + console.log(message); + } + + update(id, status) { + const targetObj = this.utils.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; + + try { + if (!targetObj) this.customException.notExistIdException(); + if (targetObj.status === status) this.customException.sameStatusException(); + } catch (e) { + console.error(e.message); + return; + } + + let preObj = {}; + if (arguments.length === 2) { + preObj = this.utils.deepCopy(targetObj); + } + + targetObj.status = status; + const nextObj = this.utils.deepCopy(targetObj); + + if (arguments.length === 2) { + this.createHistoryObj('update', preObj, nextObj); + } + + const message = `${targetObj.name}가 ${targetObj.status}로 상태가 변경되었습니다`; + setTimeout(() => { + console.log(message); + }, 3000); + } + + pushObj(arr, obj) { + if (arr.length === 3) { + arr.shift(); + arr.push(obj); + } else { + arr.push(obj); + } + } + + createHistoryObj(command, ...obj) { + let historyObj = {}; + + if (arguments.length == 3) { // update + historyObj = { + cmd: command, + pre: obj[0], + next: obj[1] + }; + + } else if (arguments.length == 2) { // add, delete + historyObj = { + cmd: command, + pre: obj[0] + }; + } + + this.pushObj(this.commandHistory.historyArr, historyObj); + this.commandHistory.pointer++; + } +}; + +module.exports = Instruction; \ No newline at end of file From da9dd1d0e7485a1a19ee4242fd996fb6dee53d31 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 29 Apr 2019 19:44:57 +0900 Subject: [PATCH 59/62] Feat : add commandHistory Object to constructor --- class/instruction.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/class/instruction.js b/class/instruction.js index 0b335fe..b768014 100644 --- a/class/instruction.js +++ b/class/instruction.js @@ -8,7 +8,11 @@ const Instruction = class { this.utils = new Utils(); this.customException = new CustomException(); this.minIdNum = 1; - this.maxIdNum = 99999; + this.maxIdNum = 99999; + this.commandHistory = { + historyArr : [], + pointer : -1, + }; } everyStatus (convertedData) { From a45f0ef2a5dfe4594eb92f8036665873fa093430 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 29 Apr 2019 19:47:21 +0900 Subject: [PATCH 60/62] Feat : add the undo method --- class/instruction.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/class/instruction.js b/class/instruction.js index b768014..0848322 100644 --- a/class/instruction.js +++ b/class/instruction.js @@ -135,7 +135,31 @@ const Instruction = class { this.pushObj(this.commandHistory.historyArr, historyObj); this.commandHistory.pointer++; - } + } + + undo() { + const targetObject = this.commandHistory.historyArr[this.commandHistory.pointer]; + const [command, preObj, checkSum] = [targetObject.cmd, targetObject.pre, true]; + + let message = ``; + + if (command === 'add') { + this.delete(preObj.id, checkSum); + message += `${preObj.id}번 항목 '${preObj.name}'가 ${preObj.status} 상태에서 삭제됐습니다`; + + } else if (command === 'delete') { + convertedData.push(preObj); + message += `${preObj.id}번 항목 '${preObj.name}'가 삭제에서 ${preObj.status} 상태로 변경됐습니다`; + + } else if (command === 'update') { + this.update(preObj.id, preObj.status, checkSum); + } + + console.log(message); + + if (this.commandHistory.pointer > -1) this.commandHistory.pointer--; + } + }; module.exports = Instruction; \ No newline at end of file From 8b95ae031efa6b59ea64919667883182c37b458f Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 29 Apr 2019 19:49:28 +0900 Subject: [PATCH 61/62] Refactor : change the preObj Object variable type let to const --- class/instruction.js | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/class/instruction.js b/class/instruction.js index 0848322..603f009 100644 --- a/class/instruction.js +++ b/class/instruction.js @@ -89,11 +89,7 @@ const Instruction = class { return; } - let preObj = {}; - if (arguments.length === 2) { - preObj = this.utils.deepCopy(targetObj); - } - + const preObj = this.utils.deepCopy(targetObj); targetObj.status = status; const nextObj = this.utils.deepCopy(targetObj); @@ -158,7 +154,29 @@ const Instruction = class { console.log(message); if (this.commandHistory.pointer > -1) this.commandHistory.pointer--; - } + } + + redo() { + if (this.commandHistory.pointer < 2) this.commandHistory.pointer++; + + const targetObject = this.commandHistory.historyArr[this.commandHistory.pointer]; + const [command, preObj, nextObj, checkSum] = [targetObject.cmd, targetObject.pre, targetObject.next, true]; + + let message = ``; + + if (command === 'add') { + convertedData.push(preObj); + message += `${preObj.id}번 항목 '${preObj.name}'가 ${preObj.status}에 추가되었습니다`; + + } else if (command === 'delete') { + this.delete(preObj.id, checkSum); + message += `${preObj.id}번 항목 '${preObj.name}'가 ${preObj.status} 상태에서 삭제됐습니다`; + + } else if (command === 'update') { + this.update(nextObj.id, nextObj.status, checkSum); + } + console.log(message); + } }; From 370a97695880d9f1382065d8168053442267f849 Mon Sep 17 00:00:00 2001 From: dev-dongwon Date: Mon, 29 Apr 2019 19:53:28 +0900 Subject: [PATCH 62/62] Fix : delete unnecessary files --- README.md | 2 - app.js | 68 -------------------------------- oop/CustomException.js | 37 ------------------ oop/app.js | 57 --------------------------- oop/commandParser.js | 40 ------------------- oop/instruction.js | 87 ----------------------------------------- oop/todosData.json | 23 ----------- oop/utils.js | 25 ------------ todos.js | 88 ------------------------------------------ todosList.json | 41 -------------------- 10 files changed, 468 deletions(-) delete mode 100644 README.md delete mode 100644 app.js delete mode 100644 oop/CustomException.js delete mode 100644 oop/app.js delete mode 100644 oop/commandParser.js delete mode 100644 oop/instruction.js delete mode 100644 oop/todosData.json delete mode 100644 oop/utils.js delete mode 100644 todos.js delete mode 100644 todosList.json diff --git a/README.md b/README.md deleted file mode 100644 index d86263b..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# todo -레벨2 diff --git a/app.js b/app.js deleted file mode 100644 index 874a42e..0000000 --- a/app.js +++ /dev/null @@ -1,68 +0,0 @@ -const todos = require('./todos.js'); - -const readline = require('readline').createInterface({ - input: process.stdin, - output: process.stdout -}); - -const getCmdList = (command) => { - const regexpForSeperateCommand = /[^\$]+/g; - return command.match(regexpForSeperateCommand); -}; - -const executeCommand = (commandArr) => { - - if (arguments.length != 1) return; - - if (commandArr.length == 2) { - todos[commandArr[0]](commandArr[1]); - } else if (commandArr.length == 3) { - todos[commandArr[0]](commandArr[1], commandArr[2]); - } -}; - -const isValidCommand = (command, obj) => { - let result = false; - if (Object.keys(obj).includes(command)) result = true; - return result; -} - -const delay = (time) => { - return new Promise(function(resolve, reject){ - setTimeout(resolve, time); - }); -} - -const runProgram = (readline) => { - - readline.setPrompt('명령하세요: '); - readline.prompt(); - readline.on('line', (userInput) => { - - try { - const commandArr = getCmdList(userInput); - const primaryCommand = commandArr[0]; - - if (!isValidCommand(primaryCommand, todos)) { - console.log("올바르지 않은 명령어입니다") - readline.prompt(); - } - - delay(0) - .then(() => {executeCommand(commandArr); return delay(0)}) - .then(() => {if (primaryCommand != 'show') return delay(2000)}) - .then(() => {if (primaryCommand == 'update') return delay(2500)}) - .then(() => {readline.prompt(); return delay(0)}) - - } catch (error) { - console.log("명령어를 다시 입력해주세요") - readline.prompt(); - } - - }).on('close', () => { - console.log("프로그램을 종료합니다."); - process.exit(); - }); -} - -runProgram(readline); \ No newline at end of file diff --git a/oop/CustomException.js b/oop/CustomException.js deleted file mode 100644 index c3f3a78..0000000 --- a/oop/CustomException.js +++ /dev/null @@ -1,37 +0,0 @@ -function CustomException() { - -} - -CustomException.prototype = { - - missingSeperatorException (input) { - if (!this.isValidSeperator(input)) throw new Error("구분자 $가 존재하지 않습니다"); - }, - notExistIdException () { - throw new Error(`찾으시는 id가 존재하지 않습니다`); - }, - sameStatusException () { - throw new Error(`같은 상태로 업데이트 할 수 없습니다`); - }, - - CommandMissingException (command, arr) { - if (!this.isValidCommand(command, arr)) - throw new Error(`올바른 명령어가 아닙니다`); - }, - - isValidSeperator (input) { - let result = true; - const regexp = /\$/g; - if (input.match(regexp) == null) result = false; - - return result; - }, - isValidCommand(command, arr) { - let result = false; - if (arr.includes(command)) result = true; - return result; - }, - -}; - -module.exports = CustomException; \ No newline at end of file diff --git a/oop/app.js b/oop/app.js deleted file mode 100644 index 3a0b6ed..0000000 --- a/oop/app.js +++ /dev/null @@ -1,57 +0,0 @@ -const readLine = require('readline').createInterface( { - input:process.stdin, - output:process.stdout, -}); -const CommandParser = require('./commandParser.js'); -const Utils = require('./utils.js'); -const Instruction = require('./instruction.js'); -const CustomException = require('./CustomException.js'); - -function RunTodoApp() { - this.cmdArr = ['show','delete','update','add']; - this.commandParser = new CommandParser(); - this.utils = new Utils(); - this.instruction = new Instruction(); - this.customException = new CustomException(); -} - -RunTodoApp.prototype = { - - runProgram (readline) { - readline.setPrompt('명령하세요: '); - readline.prompt(); - readline.on('line', (userInput) => { - - try { - - this.customException.missingSeperatorException(userInput); - const cmdList = this.commandParser.getCmdList(userInput); - - this.customException.CommandMissingException(cmdList[0], this.cmdArr); - this.commandParser.executeCmd(cmdList); - - this.utils.delay(0) - .then(() => {if (cmdList[0] === 'update') return this.utils.delay(3500);}) - .then(() => {return this.utils.delay(1000);}) - .then(() => {if (cmdList[0] !== 'show') this.instruction.show('all');}) - .catch(function(e) {console.log(e);}) - .then(() => {readLine.prompt();}); - - } catch(e) { - console.error(e.message); - readLine.prompt(); - } - - }).on('close', () => { - console.log("프로그램을 종료합니다."); - process.exit(); - }); - } - -}; - - -const run = (() => { - const runTodoApp = new RunTodoApp(); - runTodoApp.runProgram(readLine); -})(); \ No newline at end of file diff --git a/oop/commandParser.js b/oop/commandParser.js deleted file mode 100644 index 1cbeed3..0000000 --- a/oop/commandParser.js +++ /dev/null @@ -1,40 +0,0 @@ -const Instruction = require('./instruction.js'); -const CustomException = require('./customException.js'); - -function CommandParser () { - this.instruction = new Instruction(); - this.customException = new CustomException(); -} - -CommandParser.prototype = { - getCmdList(input) { - const regexp = /[^\$]+/g; - return input.match(regexp); - }, - - executeCmd(command) { - try { - - if (command.length === 2) { - this.instruction[command[0]](command[1]); - } else if (command.length === 3) { - this.instruction[command[0]](command[1], command[2]); - } else { - this.customException.CommandMissingException(); - } - - } catch (e) { - console.error(e.message); - return; - } - - }, - - isValidCommand(command, arr) { - let result = false; - if (arr.includes(command)) result = true; - return result; - }, -}; - -module.exports = CommandParser; \ No newline at end of file diff --git a/oop/instruction.js b/oop/instruction.js deleted file mode 100644 index f86f5b6..0000000 --- a/oop/instruction.js +++ /dev/null @@ -1,87 +0,0 @@ -const originData = require('./todosdata.json'); -const convertedData = JSON.parse(JSON.stringify(originData)).data; - -const Utils = require('./utils.js'); -const CustomException = require('./customException.js'); - -function Instruction() { - this.utils = new Utils(); - this.customException = new CustomException(); - this.minIdNum = 1; - this.maxIdNum = 99999; -} - -Instruction.prototype = { - - everyStatus (convertedData) { - let [numOfTodos, numOfDoings, numOfDones] = [0,0,0]; - convertedData.forEach((value) => { - if (value.status === 'todo') numOfTodos++; - else if (value.status === 'doing') numOfDoings++; - else if (value.status === 'done') numOfDones++; - }); - console.log(`현재상태 : todo: ${numOfTodos}개, doing: ${numOfDoings}개, done: ${numOfDones}개`); - }, - - singleStatus (convertedData, status) { - const tasks = this.utils.getArrByCondition(convertedData, (val) => { return (status === val.status);}); - let message = `${status}리스트 총 ${tasks.length}건 : `; - tasks.forEach( obj => { - message += `'${obj.name}, ${obj.id}번,' `; - }); - console.log(message); - }, - - show (status) { - const statusArr = ['all', 'todo', 'doing', 'done']; - if (status === 'all') { - this.everyStatus(convertedData); - } else if (statusArr.includes(status)) { - this.singleStatus(convertedData, status); - } - }, - - add (name, tags) { - const id = this.utils.getRandomId(this.maxIdNum, this.minIdNum); - let obj = { - name, - tags, - status: 'todo', - id, - }; - convertedData.push(obj); - const message = `${obj.name} 1개가 추가됐습니다.(id : ${obj.id})`; - console.log(message); - }, - - delete (id) { - const targetObj = this.utils.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; - try { - if (!targetObj) this.customException.notExistIdException(); - } catch (e) { - console.error(e.message); - return; - } - convertedData.splice(convertedData.indexOf(targetObj), 1); - let message = `${targetObj.name}이 ${targetObj.status}에서 삭제되었습니다.`; - console.log(message); - }, - - update (id, status) { - const targetObj = this.utils.getArrByCondition(convertedData, (val) => { return id == val.id;})[0]; - try { - if (!targetObj) this.customException.notExistIdException(); - if (targetObj.status === status) this.customException.sameStatusException(); - } catch (e) { - console.error(e.message); - return; - } - targetObj.status = status; - const message = `${targetObj.name}가 ${targetObj.status}로 상태가 변경되었습니다`; - setTimeout(() => { - console.log(message); - }, 3000); - } -}; - -module.exports = Instruction; \ No newline at end of file diff --git a/oop/todosData.json b/oop/todosData.json deleted file mode 100644 index 282d7ba..0000000 --- a/oop/todosData.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "result" : true, - "data": [ - { - "name" : "자바스크립트 공부하기", - "tags" : ["programming", "javascript"], - "status" : "todo", - "id" : 123 - }, - { - "name" : "그림 그리기", - "tags" : ["picture", "favorite"], - "status" : "doing", - "id" : 312323 - }, - { - "name" : "파이썬 공부하기", - "tags" : ["programming", "javascript"], - "status" : "done", - "id" : 213232 - } - ] -} \ No newline at end of file diff --git a/oop/utils.js b/oop/utils.js deleted file mode 100644 index 684baf9..0000000 --- a/oop/utils.js +++ /dev/null @@ -1,25 +0,0 @@ -function Utils () { - -} - -Utils.prototype = { - - getArrByCondition : (arr, condition) => { - return arr.reduce((acc, val) => { - if (condition(val)) acc.push(val); - return acc; - }, []); - }, - - getRandomId : (max, min) => { - return Math.floor(Math.random() * (max-min)) + 1; - }, - - delay : (time) => { - return new Promise(function(resolve, reject){ - setTimeout(resolve, time); - }); - }, -}; - -module.exports = Utils; \ No newline at end of file diff --git a/todos.js b/todos.js deleted file mode 100644 index 796ed1d..0000000 --- a/todos.js +++ /dev/null @@ -1,88 +0,0 @@ -//todos.js -const originData = require('./todosList.json'); -const convertedData = JSON.parse(JSON.stringify(originData)).data; -const idGenerator = (max, min) => Math.floor(Math.random() * (max - min)) + 1; - -const getArrByCondition = (arr, condition) => { - return arr.reduce((acc, val) => { - if (condition(val)) acc.push(val); - return acc; - }, []); -}; - -const showStatusLazy = () => { - setTimeout(() => { - showData('all'); - }, 1000); -} - -const isDuplicated = (val, key, convertedData) => { - let result = false; - convertedData.forEach((element) => { - if (val === element[key]) result = true; - }) - return result; -} - -const showData = (type) => { - if (type === 'all') { - const numOfTodos = getArrByCondition(convertedData, (val) => {return val.status === "todo"}).length; - const numOfDoings = getArrByCondition(convertedData, (val) => {return val.status === "doing"}).length; - const numOfDones = getArrByCondition(convertedData, (val) => {return val.status === "done"}).length; - - console.log(`현재상태 : todo: ${numOfTodos}개, doing: ${numOfDoings}개, done: ${numOfDones}개`); - - } else { - const objArr = getArrByCondition(convertedData, (val) => {return (type === val.status)}); - let result = `${type} 리스트 : 총${objArr.length}건 :`; - getArrByCondition(objArr, (val) => {result += `, '${val.name}, ${val.id}번'`; return true}); - console.log(`${result}`); - } -} - -const addData = (name, tags) => { - const id = idGenerator(99999, 1); - - if (isDuplicated(id, "id", convertedData)) { - return addData(name, tags); - } - - let obj = { - name, - tags, - status: 'todo', - id, - }; - - convertedData.push(obj); - console.log(`${obj.name} 1개가 추가됐습니다.(id : ${obj.id})`); - showStatusLazy(); -} - -const deleteData = (id)=> { - const target = getArrByCondition(convertedData, (val) => {return val.id === id})[0]; - if (!target) {console.log('일치하는 id가 없습니다'); return;} - - convertedData.splice(convertedData.indexOf(target), 1); - console.log(`${target.name} ${target.status}가 목록에서 삭제됐습니다.`); - showStatusLazy(); -}; - -const updateData = (id, status)=> { - const target = getArrByCondition(convertedData, (val) => {return val.id === id})[0]; - if (!target) {console.log('일치하는 id가 없습니다'); return;} - - target.status = status; - - setTimeout(() => { - console.log(`${target.name}가 ${status}로 상태가 변경되었습니다`); - showStatusLazy(); - }, 3000); -} - -module.exports = { - "show" : showData, - "add" : addData, - "update" : updateData, - "delete" : deleteData, -}; \ No newline at end of file diff --git a/todosList.json b/todosList.json deleted file mode 100644 index 1734fe5..0000000 --- a/todosList.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "result" : true, - "data": [ - { - "name" : "자바스크립트 공부하기", - "tags" : ["programming", "javascript"], - "status" : "todo", - "id" : 12123123 - }, - { - "name" : "그림 그리기", - "tags" : ["picture", "favorite"], - "status" : "doing", - "id" : 123 - }, - { - "name" : "파이썬 공부하기", - "tags" : ["programming", "javascript"], - "status" : "done", - "id" : 213232 - }, - { - "name" : "자바 공부하기", - "tags" : ["programming", "java"], - "status" : "todo", - "id" : 123345 - }, - { - "name" : "음악 듣기", - "tags" : ["music", "favorite"], - "status" : "doing", - "id" : 12123 - }, - { - "name" : "정규식 공부하기", - "tags" : ["programming", "RegExp"], - "status" : "done", - "id" : 1234 - } - ] -}