Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 108 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,109 @@
# todo
레벨2

```js
//todos.js

const todos = [
{
'name' : '자바스크립트 공부하기',
'tags' : ['programming', 'javascript']
'status' : 'todo'
'id' : 12123123
},
{
'name' : ' 그림 그리기',
'tags' : ['picture', 'favorite']
'status' : 'doing'
'id' : 312323
}
....
];

const show = (obj) => {
...
}

show("all");
show("todo");
```



```js
$ nodejs todos.js

현재상태 : todo: 1개, doing:2개, done:4개
todo리스트 : 총3건 : ' 자바스크립트 공부하기' , 'iOS공부하기'
```



todos는 object들의 배열로 이뤄져있고, show라는 method는 todos 배열을 조회하는데 있어서 다양한 옵션을 제공해준다.

- `all` : 모든 todos 현재상태를 조회하여 todo, doing, done의 갯수를 알려준다.
- `todo` : status가 todo인 object들만 조회하여 전체 todo의 갯수와 각 todo의 name을 나열해준다.



**수도코드**

```js
// INPUT objStr = 'all', 'todo', 'doing' or 'done'
const show = (objStr) => {
const props = {}
let todoSum = todo의 갯수
let doingSum = doinig의 갯수
let doneSum = todos의 갯수 - todoSum - doingSum

// 연산 내용 기억하기 위해 object 생성
props = {'todo' : todoSum, 'doing' : doingSum, 'done' : doneSum}

// all 에 대한 처리
if (objStr === 'all'){
console.log(`현재상태 : todo: ${todoSum}개, doing: ${doingSum}개, done: ${doneSum}개`)
return // all에 대한 수행이 끝났으므로 함수를 종료
}

// name들의 배열을 갖고 저장합니다.
let result = todos.filter(v => v.status === objStr).map(v=>v.name)

console.log(`${objStr}리스트 : 총${props[objStr]}건 : ${result.reduce((acc, cur)=> acc + ', ' + cur)}`)

return
}
```



**show()**

```js
const show = (objStr) => {
let todoSum = todos.filter(v => v.status === 'todo').length
let doingSum = todos.filter(v => v.status === 'doing').length
let doneSum = todos.length - todoSum - doingSum

const props = {'todo' : todoSum, 'doing' : doingSum, 'done' : doneSum}

if (objStr === 'all'){
console.log(`현재상태 : todo: ${todoSum}개, doing: ${doingSum}개, done: ${doneSum}개`)
return
}
result = todos.filter(v => v.status === objStr).map(v => v.name)
console.log(`${objStr}리스트 : 총${props[objStr]}건 : ${result.reduce((acc, cur)=> acc + ', ' + cur)}`)

return
}

show('all')
show('todo')
show('doing')
show('done')

// result
// 현재상태 : todo: 4개, doing: 2개, done: 2개
// todo리스트 : 총4건 : 자바스크립트 공부하기, 스위프트 배우기, 리엑트 공부하기, 알고리즘풀기
// doing리스트 : 총2건 : 그림 그리기, 금연하기
// done리스트 : 총2건 : 운동하기, 독서하기
```

112 changes: 112 additions & 0 deletions todoTools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const fs = require('fs')
const util = require('util')
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

// preparing for tasks
var data = fs.readFileSync('./todos.json')
var todos = JSON.parse(data)

module.exports.input = (query) => {
return new Promise((resolve, reject) => {
rl.question(query, (answer) => {
resolve(answer)
})
})
}

// help operations
const createId = (name) => {
let charCode = 0
let timeNow = new Date().getTime()
for(let i =0;i<name.length;i++){
charCode += name.charCodeAt(i)
}
const uniqueId = charCode + timeNow
return uniqueId
}

const checkId = (index) => {
if(index === -1){
console.log('잘못된 id값입니다. 다시 확인해주세요')
return false
}
return true
}

const sleep = msec => new Promise(resolve => setTimeout(resolve, msec))


// operations(add, update, delete, show)
module.exports.show = (objStr) => {
let todoSum = todos.filter(v => v.status === 'todo').length
let doingSum = todos.filter(v => v.status === 'doing').length
let doneSum = todos.length - todoSum - doingSum

const props = {'todo' : todoSum, 'doing' : doingSum, 'done' : doneSum}

if (objStr === 'all'){
console.log(`현재상태 : todo: ${todoSum}개, doing: ${doingSum}개, done: ${doneSum}개`)
return
}
result = todos.filter(v => v.status === objStr).map(v => v.name)
console.log(`${objStr}리스트 : 총${props[objStr]}건 : ${result.reduce((acc, cur)=> acc + ', ' + cur)}`)

return
}

module.exports.add = async (name, tags) => {
tags = JSON.parse(tags)
uniqueId = createId(name)

todos.push({'name' : name, 'tags': tags, 'status': 'todo', 'id': uniqueId})
console.log(`${name} 1개가 추가됐습니다. (id : ${uniqueId})`)

await sleep(1000)
this.show('all')
return
}

module.exports.update = async (id, status) => {
id*=1
index = todos.findIndex(element => element.id === id)
if(!checkId(index)){
return
}
const {name, tags} = todos[index]
todos.splice(index, 1, {name: name, id: id, tags: tags, status: status})

await sleep(3000)
console.log(`${name}가 ${status}로 상태가 변경되었습니다.`)

await sleep(1000)
this.show('all')
return
}

module.exports.delete = async (id) => {
id*=1
index = todos.findIndex(element => element.id === id)
if(!checkId(index)){
return
}
console.log(`${todos[index].name} ${todos[index].status}가 목록에서 삭제됩니다.`)
todos.splice(index, 1)

await sleep(1000)
this.show('all')
return
}

// terminate program
module.exports.shutdownRl = async () => {
await rl.close()
return
}
module.exports.saveFile = async () => {
todos = JSON.stringify(todos)
fs.writeFileSync("todos.json" ,todos)
}
49 changes: 49 additions & 0 deletions todos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const tools = require('./todoTools'); // 세미콜론이 없으면 뒤에 iife에서 오류나는 언어가 있다?

const main = async () => {
let argu = ''
console.log('안녕!이라고 입력하면 사용법을 알려줍니다.')
while(1){
argu = await tools.input('명령해주세요! >> ')
let getOut = false
let props = argu.split('$')

switch(props[0]) {
case 'show':
tools.show(props[1])
break

case 'add':
await tools.add(props[1], props[2])
break

case 'update':
await tools.update(props[1], props[2])
break

case 'delete':
await tools.delete(props[1])
break

case 'exit':
getOut = true
console.log('자료를 저장하고 프로그램을 종료합니다.')
break

default:
console.log('=======사용법==================================================================')
console.log('명령어: show, add, update, delete, exit')
console.log('show$옵션 >> 옵션 all, todo, doing 또는 done을 통해 해당 기록을 볼 수 있습니다.')
console.log('add$할일$["태그",] >> 할일을 todo 상태로 생성합니다.')
console.log('update$아이디$상태 >> 해당 아이디의 할일의 상태를 바꿔줍니다.')
console.log('delete$아이디 >> 해당 아이디의 할일을 지웁니다.')
console.log('================================================================================')
}
if(getOut === true){
break
}
}
tools.shutdownRl()
tools.saveFile()
}
main()
66 changes: 66 additions & 0 deletions todos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[
{
"name": " 그림 그리기",
"id": 312324124213,
"tags": [
"picture",
"favorite"
],
"status": "doing"
},
{
"name": "스위프트 배우기",
"tags": [
"programming",
"javascript"
],
"status": "todo",
"id": 121212341243123
},
{
"name": "밤코하기",
"id": 1555586598936,
"tags": [
"졸림",
"버스에 자리많아짐"
],
"status": "doing"
},
{
"name": "wakeup",
"tags": [
"wake",
"up"
],
"status": "todo",
"id": 1555637210754
},
{
"name": "자바스크립트 배우기",
"id": 1555639875203,
"tags": [
"javascript",
"react",
"nodejs"
],
"status": "doing"
},
{
"name": "코틀린배우기",
"tags": [
"java",
"코드량 줄어듬"
],
"status": "todo",
"id": 1555639759007
},
{
"name": "잠자기",
"id": 1555639627186,
"tags": [
"8시간",
"7시기상"
],
"status": "done"
}
]