forked from fac-23/lusan-tfb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper-functions.js
53 lines (47 loc) · 1.43 KB
/
helper-functions.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
import products from "./database/products";
// local storage
function saveToLocalStorage(key, product) {
// get what is currently in local storage
const currentLocalStorage = JSON.parse(localStorage.getItem(key)) || [];
// add a value to the current storage
currentLocalStorage.push(product);
// set storage to include new value
localStorage.setItem(key, JSON.stringify(currentLocalStorage));
}
function removeFromLocalStorage(key, variety) {
// exclude the product of current variety
const filteredLocalStorage = JSON.parse(localStorage.getItem(key)).filter(
(product) => product.variety !== variety
);
// set local storage to the filtered array
localStorage.setItem(key, JSON.stringify(filteredLocalStorage));
return filteredLocalStorage;
}
function getFromLocalStorage(key) {
const saved = JSON.parse(localStorage.getItem(key));
return saved || [];
}
// database tags
// returns an array of product tags containing the keys which have a 'true' value
function generateTags(obj) {
const keys = Object.keys(obj);
return keys.filter((key) => {
if (obj[key]) {
return key;
}
});
}
// find product by variety
function findContent(capitalisedVariety) {
const foundObject = products.find((productObject) => {
return productObject.variety === capitalisedVariety;
});
return foundObject;
}
export {
saveToLocalStorage,
removeFromLocalStorage,
getFromLocalStorage,
generateTags,
findContent,
};