-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
87 lines (56 loc) · 1.7 KB
/
db.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
function eraseLocalStorageDatabase() {
localStorage.setItem('users', '[]');
localStorage.setItem('matches', '[]');
localStorage.setItem('bets', '[]');
displayStatusMessage('Erased all data.');
}
function createLocalStorageDatabase() {
if (!localStorage.getItem('users')) {
localStorage.setItem('users', '[]');
}
if (!localStorage.getItem('matches')) {
localStorage.setItem('matches', '[]');
}
if (!localStorage.getItem('bets')) {
localStorage.setItem('bets', '[]');
}
}
function addObjectToTable(name, object) {
var json = localStorage.getItem(name);
// remove closing bracket
json = json.substr(0, json.length - 1);
// if table is not empty, remove closing bracket, then add a comma (separator)
if (json !== "[") {
json += ",";
}
json += object + "]";
localStorage.setItem(name, json);
}
function getTable(name) {
return JSON.parse(localStorage.getItem(name));
}
function getObjectsFromTable(name, column, value) {
var wanted = [];
var table = getTable(name);
for (var i = 0; i < table.length; i++) {
var obj = table[i];
if (obj[column] === value) {
wanted.push(obj);
}
}
return wanted;
}
function removeObjectsFromTable(name, column, value) {
var wanted = [];
var table = getTable(name);
for (var i = 0; i < table.length; i++) {
var obj = table[i];
if (obj[column] !== value) {
wanted.push(obj);
}
}
localStorage.setItem(name, JSON.stringify(wanted));
}
function tableContainsObject(name, column, value) {
return getObjectsFromTable(name, column, value).length > 0;
}