Skip to content

Commit 0517df6

Browse files
dev-dongwoncrongro
authored andcommitted
Step4-2 mission : TUNA & SNOW (#104)
* create todosList.json * create todos.js * create app.js for program runnung * Rix the name of function rename getCommandArrayByRegexp to getCmdList * excuteCommand 함수 수정 : argument 개수 판별 추가 인자 개수로 1개 초과 나올 경우 즉시 종료 후 프로그램 다시 실행 * isValidCommand 함수 변경 : obeject argument 추가 외부 변수에 접근하지 않고 객체를 인자로 받을 수 있도록 변경 * idGenerator 함수 변경 : 상수가 아닌 인자로 random number 생성 상수 99999 제거 후, max 값과 min 값으로 random 넘버 생성 * fix the Equal operator to strcit Equal operator
1 parent 82e620d commit 0517df6

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed

app.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const todos = require('./todos.js');
2+
3+
const readline = require('readline').createInterface({
4+
input: process.stdin,
5+
output: process.stdout
6+
});
7+
8+
const getCmdList = (command) => {
9+
const regexpForSeperateCommand = /[^\$]+/g;
10+
return command.match(regexpForSeperateCommand);
11+
};
12+
13+
const executeCommand = (commandArr) => {
14+
15+
if (arguments.length != 1) return;
16+
17+
if (commandArr.length == 2) {
18+
todos[commandArr[0]](commandArr[1]);
19+
} else if (commandArr.length == 3) {
20+
todos[commandArr[0]](commandArr[1], commandArr[2]);
21+
}
22+
};
23+
24+
const isValidCommand = (command, obj) => {
25+
let result = false;
26+
if (Object.keys(obj).includes(command)) result = true;
27+
return result;
28+
}
29+
30+
const delay = (time) => {
31+
return new Promise(function(resolve, reject){
32+
setTimeout(resolve, time);
33+
});
34+
}
35+
36+
const runProgram = (readline) => {
37+
38+
readline.setPrompt('명령하세요: ');
39+
readline.prompt();
40+
readline.on('line', (userInput) => {
41+
42+
try {
43+
const commandArr = getCmdList(userInput);
44+
const primaryCommand = commandArr[0];
45+
46+
if (!isValidCommand(primaryCommand, todos)) {
47+
console.log("올바르지 않은 명령어입니다")
48+
readline.prompt();
49+
}
50+
51+
delay(0)
52+
.then(() => {executeCommand(commandArr); return delay(0)})
53+
.then(() => {if (primaryCommand != 'show') return delay(2000)})
54+
.then(() => {if (primaryCommand == 'update') return delay(2500)})
55+
.then(() => {readline.prompt(); return delay(0)})
56+
57+
} catch (error) {
58+
console.log("명령어를 다시 입력해주세요")
59+
readline.prompt();
60+
}
61+
62+
}).on('close', () => {
63+
console.log("프로그램을 종료합니다.");
64+
process.exit();
65+
});
66+
}
67+
68+
runProgram(readline);

todos.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//todos.js
2+
const originData = require('./todosList.json');
3+
const convertedData = JSON.parse(JSON.stringify(originData)).data;
4+
const idGenerator = (max, min) => Math.floor(Math.random() * (max - min)) + 1;
5+
6+
const getArrByCondition = (arr, condition) => {
7+
return arr.reduce((acc, val) => {
8+
if (condition(val)) acc.push(val);
9+
return acc;
10+
}, []);
11+
};
12+
13+
const showStatusLazy = () => {
14+
setTimeout(() => {
15+
showData('all');
16+
}, 1000);
17+
}
18+
19+
const isDuplicated = (val, key, convertedData) => {
20+
let result = false;
21+
convertedData.forEach((element) => {
22+
if (val === element[key]) result = true;
23+
})
24+
return result;
25+
}
26+
27+
const showData = (type) => {
28+
if (type === 'all') {
29+
const numOfTodos = getArrByCondition(convertedData, (val) => {return val.status === "todo"}).length;
30+
const numOfDoings = getArrByCondition(convertedData, (val) => {return val.status === "doing"}).length;
31+
const numOfDones = getArrByCondition(convertedData, (val) => {return val.status === "done"}).length;
32+
33+
console.log(`현재상태 : todo: ${numOfTodos}개, doing: ${numOfDoings}개, done: ${numOfDones}개`);
34+
35+
} else {
36+
const objArr = getArrByCondition(convertedData, (val) => {return (type === val.status)});
37+
let result = `${type} 리스트 : 총${objArr.length}건 :`;
38+
getArrByCondition(objArr, (val) => {result += `, '${val.name}, ${val.id}번'`; return true});
39+
console.log(`${result}`);
40+
}
41+
}
42+
43+
const addData = (name, tags) => {
44+
const id = idGenerator(99999, 1);
45+
46+
if (isDuplicated(id, "id", convertedData)) {
47+
return addData(name, tags);
48+
}
49+
50+
let obj = {
51+
name,
52+
tags,
53+
status: 'todo',
54+
id,
55+
};
56+
57+
convertedData.push(obj);
58+
console.log(`${obj.name} 1개가 추가됐습니다.(id : ${obj.id})`);
59+
showStatusLazy();
60+
}
61+
62+
const deleteData = (id)=> {
63+
const target = getArrByCondition(convertedData, (val) => {return val.id === id})[0];
64+
if (!target) {console.log('일치하는 id가 없습니다'); return;}
65+
66+
convertedData.splice(convertedData.indexOf(target), 1);
67+
console.log(`${target.name} ${target.status}가 목록에서 삭제됐습니다.`);
68+
showStatusLazy();
69+
};
70+
71+
const updateData = (id, status)=> {
72+
const target = getArrByCondition(convertedData, (val) => {return val.id === id})[0];
73+
if (!target) {console.log('일치하는 id가 없습니다'); return;}
74+
75+
target.status = status;
76+
77+
setTimeout(() => {
78+
console.log(`${target.name}${status}로 상태가 변경되었습니다`);
79+
showStatusLazy();
80+
}, 3000);
81+
}
82+
83+
module.exports = {
84+
"show" : showData,
85+
"add" : addData,
86+
"update" : updateData,
87+
"delete" : deleteData,
88+
};

todosList.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"result" : true,
3+
"data": [
4+
{
5+
"name" : "자바스크립트 공부하기",
6+
"tags" : ["programming", "javascript"],
7+
"status" : "todo",
8+
"id" : 12123123
9+
},
10+
{
11+
"name" : "그림 그리기",
12+
"tags" : ["picture", "favorite"],
13+
"status" : "doing",
14+
"id" : 123
15+
},
16+
{
17+
"name" : "파이썬 공부하기",
18+
"tags" : ["programming", "javascript"],
19+
"status" : "done",
20+
"id" : 213232
21+
},
22+
{
23+
"name" : "자바 공부하기",
24+
"tags" : ["programming", "java"],
25+
"status" : "todo",
26+
"id" : 123345
27+
},
28+
{
29+
"name" : "음악 듣기",
30+
"tags" : ["music", "favorite"],
31+
"status" : "doing",
32+
"id" : 12123
33+
},
34+
{
35+
"name" : "정규식 공부하기",
36+
"tags" : ["programming", "RegExp"],
37+
"status" : "done",
38+
"id" : 1234
39+
}
40+
]
41+
}

0 commit comments

Comments
 (0)