-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.js
More file actions
139 lines (122 loc) · 3.12 KB
/
Copy pathtodo.js
File metadata and controls
139 lines (122 loc) · 3.12 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
//this file contains the functions to add, delete, read, update, list, mark as in progress, mark as done and log a todo item.
const fs = require('fs');
const _ = require('lodash');
const todosFilePath = 'todos.json';
//Add a todo item
const addTodo = (title) => {
const todos = fetchTodos();
const id = _.uniqueId();
const status = 'not done';
const createdAt = new Date();
todos.push({ id, title ,status, createdAt});
saveTodos(todos);
console.log('Todo added:', { id, title });
};
// Delete a todo item
const deleteTodoByTitle = (title) => {
let todos = fetchTodos();
let filteredTodos = todos.filter(
(todo) => todo.title !== title
);
saveTodos(filteredTodos);
return todos.length !== filteredTodos.length;
};
//Read a todo item
const readTodo = (id) => {
let todos = fetchTodos();
return todos.find((todo) => todo.id === id);
};
//Update a todo item
const updateTodo = (id, newTitle) => {
let todos = fetchTodos();
let todo = todos.find((todo) => todo.id === id);
if (todo) {
todo.title = newTitle;
todo.updatedAt = new Date().toISOString();
saveTodos(todos);
return todo;
}
return null;
};
//List all todo items
const listTodos = () => {
return fetchTodos();
};
//List all Done task
const listTodosDone = () => {
let todos = fetchTodos();
let doneTodos = todos.filter(
(todo) => todo.status === 'done');
return doneTodos;
};
//List all in progress task
const listTodosInProgress = () => {
let todos = fetchTodos();
let doneTodos = todos.filter(
(todo) => todo.status === 'in progress');
return doneTodos;
};
//List all Done task
const listTodosNotDone = () => {
let todos = fetchTodos();
let doneTodos = todos.filter(
(todo) => todo.status === 'not done');
return doneTodos;
};
//Mark a todo item as in progress
const markInProgress = (id) => {
let todos = fetchTodos();
let todo = todos.find((todo) => todo.id === id);
if (todo) {
todo.status = 'in proress';
saveTodos(todos);
return todo.title;
}
return null;
};
//Mark a todo item as done
const markDone = (id) => {
let todos = fetchTodos();
let todo = todos.find((todo) => todo.id === id);
if (todo) {
todo.status = 'done';
saveTodos(todos);
return todo.title;
}
return null;
};
//Utility functions
//Fetches todos from a file and returns them as an array.
//If the file does not exist or cannot be read, an empty array is returned.
const fetchTodos = () => { //
try {
const dataBuffer = fs.readFileSync(todosFilePath);
const dataJSON = dataBuffer.toString();
return JSON.parse(dataJSON);
} catch (e) {
return [];
}
};
//Saves todos to a file.
const saveTodos = (todos) => {
const dataJSON = JSON.stringify(todos);
fs.writeFileSync(todosFilePath, dataJSON);
};
const logTodo = (todo) => {
console.log('## ---## --- ##');
console.log(`${todo.title}`);
};
// Exporting function
module.exports = {
addTodo,
deleteTodoByTitle,
readTodo,
updateTodo,
listTodos,
listTodosInProgress,
listTodosDone,
listTodosNotDone,
markInProgress,
markDone,
logTodo
};