|
| 1 | +/* |
| 2 | + Logging... |
| 3 | +*/ |
| 4 | + |
| 5 | +import readline from 'node:readline/promises' |
| 6 | + |
| 7 | +console.log('Logging...') |
| 8 | + |
| 9 | +console.clear() |
| 10 | + |
| 11 | +console.log('\nconsole.dir(<Object>)...\n') |
| 12 | + |
| 13 | +console.dir({ |
| 14 | + age: 22, |
| 15 | + userName: 'Lucas Hoz', |
| 16 | + address: { |
| 17 | + city: 'Buenas Aires', |
| 18 | + country: 'Argentina', |
| 19 | + }, |
| 20 | +}) |
| 21 | + |
| 22 | +console.log('\nconsole.error(<String>)...') |
| 23 | + |
| 24 | +console.error('\nAn error occurred!') |
| 25 | + |
| 26 | +console.log(`\nconsole.group(<Label>)\n<...Logs>\nconsole.groupEnd()...`) |
| 27 | + |
| 28 | +console.group('\nLog group') |
| 29 | +console.log('Log inside the group') |
| 30 | +console.groupEnd() |
| 31 | + |
| 32 | +console.log( |
| 33 | + `\nconsole.groupCollapsed(<Label>)\n<...Logs>\nconsole.groupEnd()...` |
| 34 | +) |
| 35 | + |
| 36 | +console.groupCollapsed('\nLog group (collapsed)') |
| 37 | +console.log('Log inside the group') |
| 38 | +console.groupEnd() |
| 39 | + |
| 40 | +console.log('\nconsole.info(<MESSAGE>)...') |
| 41 | + |
| 42 | +console.info('\nNew information!') |
| 43 | + |
| 44 | +console.log('\nconsole.log(<MESSAGE>)...') |
| 45 | + |
| 46 | +console.log('\nCommon log') |
| 47 | + |
| 48 | +console.log('\nconsole.table(<Array>)...\n') |
| 49 | + |
| 50 | +console.table([ |
| 51 | + 'Buenos Aires', |
| 52 | + 'Catamarca', |
| 53 | + 'Santa Fe', |
| 54 | + 'Jujuy', |
| 55 | + 'Santiago del Estero', |
| 56 | +]) |
| 57 | + |
| 58 | +console.log('\nconsole.warn(<MESSAGE>)...') |
| 59 | + |
| 60 | +console.warn('\nWarning!') |
| 61 | + |
| 62 | +console.log( |
| 63 | + `\nconsole.time(<Label>)\nconsole.timeLog(<Label>)\nconsole.timeEnd(<Label>)...\n` |
| 64 | +) |
| 65 | + |
| 66 | +console.time('Timer') |
| 67 | +console.timeLog('Timer') |
| 68 | +console.timeEnd('Timer') |
| 69 | + |
| 70 | +console.log( |
| 71 | + '\n# ---------------------------------------------------------------------------------- #\n' |
| 72 | +) |
| 73 | + |
| 74 | +/* |
| 75 | + Additional challenge... |
| 76 | +*/ |
| 77 | + |
| 78 | +console.log('Additional challenge...') |
| 79 | + |
| 80 | +interface Task { |
| 81 | + description: string |
| 82 | + title: string |
| 83 | +} |
| 84 | + |
| 85 | +interface ITaskManager { |
| 86 | + getShouldPrintLogs(): boolean |
| 87 | + |
| 88 | + setShouldPrintLogs(newValue: boolean): this |
| 89 | + |
| 90 | + addTask(newTask: Task): this |
| 91 | + deleteTaskByTitle(title: string): this |
| 92 | + printTasks(): this |
| 93 | +} |
| 94 | + |
| 95 | +class TaskManager implements ITaskManager { |
| 96 | + private shouldPrintLogs: boolean |
| 97 | + private tasks: Task[] |
| 98 | + |
| 99 | + constructor({ |
| 100 | + shouldPrintLogs, |
| 101 | + initialTasks, |
| 102 | + }: { |
| 103 | + shouldPrintLogs: boolean |
| 104 | + initialTasks: Task[] |
| 105 | + }) { |
| 106 | + this.shouldPrintLogs = shouldPrintLogs |
| 107 | + this.tasks = initialTasks |
| 108 | + } |
| 109 | + |
| 110 | + getShouldPrintLogs(): boolean { |
| 111 | + return this.shouldPrintLogs |
| 112 | + } |
| 113 | + |
| 114 | + setShouldPrintLogs(newValue: boolean): this { |
| 115 | + this.shouldPrintLogs = newValue |
| 116 | + return this |
| 117 | + } |
| 118 | + |
| 119 | + addTask(newTask: Task): this { |
| 120 | + if (this.shouldPrintLogs) { |
| 121 | + console.log('\naddTask (method) start execution...') |
| 122 | + console.time('addTask') |
| 123 | + } |
| 124 | + |
| 125 | + this.tasks.push(newTask) |
| 126 | + |
| 127 | + if (this.shouldPrintLogs) { |
| 128 | + console.log('\naddTask (method) ends execution!') |
| 129 | + console.timeEnd('addTask') |
| 130 | + } |
| 131 | + return this |
| 132 | + } |
| 133 | + |
| 134 | + deleteTaskByTitle(title: string): this { |
| 135 | + if (this.shouldPrintLogs) { |
| 136 | + console.log('\ndeleteTaskByTitle (method) start execution...') |
| 137 | + console.time('deleteTaskByTitle') |
| 138 | + } |
| 139 | + |
| 140 | + this.tasks = this.tasks.filter( |
| 141 | + (task) => |
| 142 | + task.title.trim().toUpperCase() !== title.trim().toUpperCase() |
| 143 | + ) |
| 144 | + |
| 145 | + if (this.shouldPrintLogs) { |
| 146 | + console.log('\ndeleteTaskByTitle (method) ends execution!') |
| 147 | + console.timeEnd('deleteTaskByTitle') |
| 148 | + } |
| 149 | + return this |
| 150 | + } |
| 151 | + |
| 152 | + printTasks(): this { |
| 153 | + if (this.shouldPrintLogs) { |
| 154 | + console.log('\nprintTasks (method) start execution...\n') |
| 155 | + console.time('printTasks') |
| 156 | + } |
| 157 | + |
| 158 | + for (const task of this.tasks) console.dir(task) |
| 159 | + |
| 160 | + if (this.shouldPrintLogs) { |
| 161 | + console.log('\nprintTasks (method) ends execution!') |
| 162 | + console.timeEnd('printTasks') |
| 163 | + } |
| 164 | + return this |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +type Operation = 'add task' | 'delete task by title' | 'print tasks' | 'exit' |
| 169 | +;(async () => { |
| 170 | + const taskManager: TaskManager = new TaskManager({ |
| 171 | + initialTasks: [], |
| 172 | + shouldPrintLogs: true, |
| 173 | + }) |
| 174 | + |
| 175 | + const rl = readline.createInterface({ |
| 176 | + input: process.stdin, |
| 177 | + output: process.stdout, |
| 178 | + }) |
| 179 | + |
| 180 | + let exit: boolean = false |
| 181 | + |
| 182 | + while (!exit) { |
| 183 | + const operation = (await rl.question( |
| 184 | + "\nWrite an operation ('Add task', 'Delete task by title', 'Print tasks', or 'Exit'): " |
| 185 | + )) as Operation |
| 186 | + |
| 187 | + switch (operation.trim().toUpperCase() as Uppercase<Operation>) { |
| 188 | + case 'ADD TASK': |
| 189 | + const newTaskTitle = await rl.question('\nTask title: ') |
| 190 | + |
| 191 | + const newTaskDescription = await rl.question( |
| 192 | + 'Task description: ' |
| 193 | + ) |
| 194 | + |
| 195 | + taskManager.addTask({ |
| 196 | + description: newTaskDescription, |
| 197 | + title: newTaskTitle, |
| 198 | + }) |
| 199 | + break |
| 200 | + |
| 201 | + case 'DELETE TASK BY TITLE': |
| 202 | + const taskTitleToDelete = await rl.question('\nTask title: ') |
| 203 | + taskManager.deleteTaskByTitle(taskTitleToDelete) |
| 204 | + break |
| 205 | + |
| 206 | + case 'PRINT TASKS': |
| 207 | + taskManager.printTasks() |
| 208 | + break |
| 209 | + |
| 210 | + case 'EXIT': |
| 211 | + exit = true |
| 212 | + console.log('\nApplication closed!') |
| 213 | + break |
| 214 | + |
| 215 | + default: |
| 216 | + console.log('\nInvalid operation! Try again...') |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + rl.close() |
| 221 | +})() |
0 commit comments