forked from fac-13/jikt-todo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
104 lines (93 loc) · 3.7 KB
/
logic.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
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
// Part 1. Fill in any missing parts of the todoFunction object!
// you can access these on todo.todoFunctions
// For part one we expect you to use tdd
var todoFunctions = {
// todoFunctions.generateId() will give you a unique id
// You do not need to understand the implementation of this function.
generateId: (function() {
var idCounter = 0;
function incrementCounter() {
return (idCounter += 1);
}
return incrementCounter;
})(),
//cloneArrayOfObjects will create a copy of the todos array
//changes to the new array don't affect the original
cloneArrayOfObjects: function(todos) {
return todos.map(function(todo){
return JSON.parse(JSON.stringify(todo));
});
},
addTodo: function(todos, newTodo, newId) {
// should leave the input argument todos unchanged (you can use cloneArrayOfObjects)
// returns a new array, it should contain todos with the newTodo added to the end.
// add an id to the newTodo. You can use the generateId function to create an id.
// hint: array.concat
let localTodos = this.cloneArrayOfObjects(todos);
newTodo.id = newId;
// newTodo.id=this.generateId();
let result = localTodos.concat([newTodo]);
return result;
},
deleteTodo: function(todos, idToDelete) {
// should leave the input argument todos unchanged (you can use cloneArrayOfObjects)
// return a new array, this should not contain any todo with an id of idToDelete
// hint: array.filter
let localTodos = this.cloneArrayOfObjects(todos);
let filteredTodos = localTodos.filter(function(item){
return item.id!==idToDelete;
});
return filteredTodos;
},
markTodo: function(todos, idToMark) {
// should leave the input argument todos unchanged (you can use cloneArrayOfObjects)
// in the new todo array, all elements will remain unchanged except the one with id: idToMark
// this element will have its done value toggled
// hint: array.map
let localTodos = this.cloneArrayOfObjects(todos);
let toggledArray = localTodos.map(function(item){
if(item.id===idToMark){
item.done=!item.done;
}
return item;
})
return toggledArray;
},
sortDescending: function(item1, item2){
return item2.id-item1.id;
},
sortAscending: function(item1, item2){
return item1.id-item2.id;
},
sortZA: function(item1, item2){
if(item1.description < item2.description) return 1;
if(item1.description > item2.description) return -1
return 0;
},
sortAZ: function(item1, item2){
if(item1.description < item2.description) return -1;
if(item1.description > item2.description) return 1
return 0;
},
sortDone: function(item1, item2){
if(item1.done === item2.done) return -1;
if(item1.done !== item2.done) return 1
return 0;
},
sortTodos: function(todos, sortFunction) {
// stretch goal! Do this last
// should leave the input arguement todos unchanged (you can use cloneArrayOfObjects)
// sortFunction will have same signature as the sort function in array.sort
// hint: array.slice, array.sort
let localTodos = this.cloneArrayOfObjects(todos);
let sortedTodos = localTodos.sort(sortFunction);
return sortedTodos;
},
};
// Why is this if statement necessary?
// The answer has something to do with needing to run code both in the browser and in Node.js
// See this article for more details:
// http://www.matteoagosti.com/blog/2013/02/24/writing-javascript-modules-for-both-browser-and-node/
if (typeof module !== 'undefined') {
module.exports = todoFunctions;
}