-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
63 lines (49 loc) · 1.36 KB
/
data.js
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
var currId = 0;
var todoListEntries = {};
var makeToDo = function(title, task) {
if (!title) throw "Must provide a title";
if (!task) throw "Must provide a task";
var newTask = { id: ++currId, title: title, task: task, done: false };
todoListEntries[newTask.id] = newTask;
return newTask;
};
var getToDo = function(id) {
if (!todoListEntries[id]) throw "No such entry exists";
return todoListEntries[id];
};
var finishToDo = function(id) {
var entry = getToDo(id);
entry.done = true;
return entry;
};
var updateToDo = function(id, newTitle, newTask) {
var entry = getToDo(id);
if (newTitle) entry.title = newTitle;
if (newTask) entry.task = newTask;
return entry;
};
var getAll = function() {
return Object.keys(todoListEntries).map(function(key) {
return todoListEntries[key];
});
};
var getFinished = function() {
return getAll().filter(function(entry) {
return entry.done;
});
};
var getUnfinished = function() {
return getAll().filter(function(entry) {
return !entry.done;
});
};
var exports = module.exports = {
getToDo: getToDo,
finishToDo: finishToDo,
updateToDo: updateToDo,
getAll: getAll,
getFinished: getFinished,
getUnfinished: getUnfinished,
makeToDo: makeToDo
};
exports.makeToDo("Finish ToDo List", "You should finish this list");