-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrocery-list.js
186 lines (147 loc) · 4.76 KB
/
grocery-list.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
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
let GroceryListItem = require('./grocery-list-item');
module.exports = class GroceryList {
constructor(name) {
if (typeof name !== "string" || name === "") {
throw new Error("A list must have a name that is a non-empty string.");
}
this.name = name;
this.items = [];
// Add a static property if not already set
GroceryList.allInstances = GroceryList.allInstances || [];
// Check that a list with that name doesn't exist already
/*GroceryList.allInstances.forEach(function(list){
if(list.name == name){
throw new Error("There's already a list with that name.");
}
});*/
// Push this to the static property allInstances
GroceryList.allInstances.push(this);
//everytime a new list is created, set all lists .active to 'false'
this.makeAllListsInactive();
//and then set THIS list .active to 'true'
this.active = true;
}
changeActiveList(indexOfInstance){
//set all instances .active to "false"
this.makeAllListsInactive()
//set the selected instance .active to "true"
GroceryList.allInstances[indexOfInstance].active = true;
}
makeAllListsInactive(){
GroceryList.allInstances.forEach(function(list){
list.active = false;
});
}
addItem(name, quantity, category) {
this.items.forEach(function(item) {
if (item.name == name) {
throw new Error("There's already an item with that name.");
}
});
let newItem = new GroceryListItem(name, quantity, category);
this.items.push(newItem);
}
removeItem(itemName) {
// ta bort matvara i sin shoppinglista.
let filteredArray = this.items.filter((item) => {
return item.name.indexOf(itemName);
});
this.items = filteredArray;
}
removeItemByIndex(index) {
// ta bort matvara i sin shoppinglista.
this.items.splice(index, 1)
}
buy(itemName) {
let item = {};
// Finn item med korrekt namn
for (var i = 0; i < this.items.length; i++) {
if (this.items[i].name === itemName) {
// Varan är nu köpt
this.items[i].bought = true;
item = this.items[i];
break;
}
}
if (item) { return true; }
return false;
}
buyIndex(indexOfItem){
this.items[indexOfItem].bought = true;
}
unboughtIndex(indexOfItem){
console.log(this.items);
this.items[indexOfItem].bought = false;
}
setItemBought(itemName){
let item = {};
// Finn item med korrekt namn
for (var i = 0; i < this.items.length; i++) {
if (this.items[i].name === itemName) {
// Varan är nu köpt
this.items[i].bought = true;
break
}
}
if (item) { return true; }
return false;
}
setItemUnbought(itemName){
let item = {};
// Finn item med korrekt namn
for (var i = 0; i < this.items.length; i++) {
if (this.items[i].name === itemName) {
// Varan är nu oköpt
this.items[i].bought = false;
break
}
}
if (item) { return true; }
return false;
}
sortAlphabetical(arg) {
// calling the function with an true or false arg
// true = ascending, false = reversed
if (typeof arg !== "boolean") {
throw new Error("An boolean argument need to be passed");
}
let sortedByName;
let x = this.items.slice();
if (arg) {
sortedByName = x.sort((a, b) => a.name > b.name);
} else {
sortedByName = x.sort((a, b) => a.name < b.name);
}
return sortedByName;
}
sortByCategory(bool) {
if(bool){
let sortedByCategory = this.items.slice().sort((a, b) => a.category > b.category);
return sortedByCategory;
}
else{
let sortedByCategory = this.items.slice().sort((b, a) => a.category > b.category);
return sortedByCategory;
}
}
displayAllItems() {
let filteredArray = this.items.slice().filter((item) => {
return item.bought === true || item.bought === false;
});
return filteredArray;
}
filterBoughtItems() {
// Visa endast köpta varor
let filteredArray = this.items.slice().filter((item) => {
return item.bought === true;
});
return filteredArray;
}
filterUnboughtItems() {
// Visa endast oköpta varor
let filteredArray = this.items.slice().filter((item) => {
return item.bought === false;
});
return filteredArray;
}
}