forked from NWC2/Node-Basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.js
More file actions
220 lines (195 loc) · 5.47 KB
/
tasks.js
File metadata and controls
220 lines (195 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Starts the application
* This is the function that is run when the app starts
*
* It prints a welcome line, and then a line with "----",
* then nothing.
*
* @param {string} name the name of the app
* @returns {void}
*/
function startApp(name) {
process.stdin.resume();
process.stdin.setEncoding("utf8");
process.stdin.on("data", onDataReceived);
console.log(`Welcome to ${name}'s application!`);
console.log("--------------------");
}
const fs = require("fs");
const tasks = loadTasks(); // Load tasks from the default or specified file
// ... rest of the code ...
// Load tasks from the default or specified file
function loadTasks() {
const saveFileName = process.argv[2] || "database.json";
try {
const data = fs.readFileSync(saveFileName, "utf8");
return JSON.parse(data);
} catch (error) {
console.log("Error loading tasks. Starting with an empty task list.");
return [];
}
}
// Save tasks to the specified file
function saveTasks() {
const saveFileName = process.argv[2] || "database.json";
try {
fs.writeFileSync(saveFileName, JSON.stringify(tasks), "utf8");
console.log("Tasks saved successfully.");
} catch (error) {
console.log("Error saving tasks.");
}
}
/**
* Decides what to do depending on the data that was received
* This function receives the input sent by the user.
*
* For example, if the user entered
* ```
* node tasks.js batata
* ```
*
* The text received would be "batata"
* This function then directs to other functions
*
* @param {string} text data typed by the user
* @returns {void}
*/
function onDataReceived(text) {
var textParts = text.trim().split(" ");
if (textParts[0] === "quit" || textParts[0] === "exit") {
quit();
} else if (textParts[0] === "hello") {
hello(text);
} else if (textParts[0] === "list") {
list();
} else if (textParts[0] === "add") {
add(text.substring(4).trim());
} else if (textParts[0] === "remove") {
remove(textParts[1]);
} else if (textParts[0] === "edit") {
edit(text);
} else if (textParts[0] === "check") {
checkTask(textParts[1]);
} else if (textParts[0] === "uncheck") {
uncheckTask(textParts[1]);
} else if (textParts[0] === "help") {
help();
} else {
unknownCommand(text);
}
}
function list() {
tasks.forEach((task, index) => {
console.log(`${index + 1}. ${task.title} [${task.done ? "✓" : " "}]`);
});
}
/**
* prints "unknown command"
* This function is supposed to run when all other commands have failed
*
* @param {string} c the text received
* @returns {void}
*/
function unknownCommand(c) {
console.log('unknown command: "' + c.trim() + '"');
}
// add function
function add(task) {
if (task.trim() !== "") {
tasks.push({ title: task, done: false });
console.log(`Task "${task}" added.`);
}
}
// remove function
function remove(index) {
let taskIndex = parseInt(index) - 1;
if (!index) {
taskIndex = -1;
}
if (isFinite(taskIndex) && taskIndex >= -1 && taskIndex <= tasks.length) {
const removedTask = tasks.splice(taskIndex, 1);
console.log(`Removed task: "${removedTask[0].title}".`);
} else {
console.log("Invalid task index. Task does not exist.");
}
}
/// edit function
function edit(text) {
const textParts = text.trim().split(" ");
let taskIndex, newText;
if (textParts.length < 2) {
console.log("Invalid format. Use 'edit index new text'.");
return;
}
taskIndex = parseInt(textParts[1]);
if (isNaN(taskIndex)) {
newText = text.substring(textParts[0].length).trim();
taskIndex = tasks.length - 1;
} else {
newText = text
.substring(textParts[0].length + textParts[1].length + 2)
.trim();
taskIndex--;
}
if (taskIndex >= 0 && taskIndex < tasks.length) {
tasks[taskIndex].title = newText;
console.log(`Task ${taskIndex + 1} updated to: "${newText}".`);
} else {
console.log("Invalid task index. Task does not exist.");
}
}
// check task
function checkTask(index) {
const taskIndex = parseInt(index);
if (isFinite(taskIndex) && taskIndex >= 1 && taskIndex <= tasks.length) {
tasks[taskIndex - 1].done = true;
console.log(`Task ${taskIndex} marked as done.`);
} else {
console.log("Invalid task index. Task does not exist.");
}
}
// uncheck task
function uncheckTask(index) {
const taskIndex = parseInt(index);
if (isFinite(taskIndex) && taskIndex >= 1 && taskIndex <= tasks.length) {
tasks[taskIndex - 1].done = false;
console.log(`Task ${taskIndex} marked as not done.`);
} else {
console.log("Invalid task index. Task does not exist.");
}
}
/**
* Says hello1
0 *
* @returns {void}
*/
// welcome function to type "hello X!" X can anything
function hello(name) {
var name1 = name.trim();
console.log(name1 + "!");
}
// help funiction to know how you can use this app
function help() {
console.log(
"Available commands:\n" +
"- 'hello x': Print hello message for the provided name.\n" +
"- 'list': List all tasks.\n" +
"- 'add x': Add a task 'x'.\n" +
"- 'remove [index]': Remove a task at the specified index.\n" +
"- 'check [index]': mark a task as done at the specified index.\n" +
"- 'uncheck [index]': mark a task as undone at the specified index.\n" +
"- 'quit' or 'exit': Quit the application."
);
}
/**
* Exits the applicationc
*
* @returns {void}
*/
function quit() {
console.log("Quitting now, goodbye!");
saveTasks();
process.exit();
}
// The following line starts the application
startApp("Abdallah Salami");