-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyLocalStorage.js
executable file
·142 lines (115 loc) · 3.27 KB
/
MyLocalStorage.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
/*
This is a wrapper of localStorage and effects only the data stored
using this same class
*/
function MyLocalStorage() {
}
// namespace of the keys
MyLocalStorage.preKey = "";
// contains all the items with type "MyLocalStorageItem" in the local storage
MyLocalStorage.elements = [];
MyLocalStorage.init = function(namespace) {
MyLocalStorage.preKey = namespace + "_";
// loads the already stored data
for (i=0; i<=localStorage.length-1; i++) {
key = localStorage.key(i);
if(key.indexOf(this.preKey) != 0) {
continue;
}
value = localStorage.getItem(key);
item = new MyLocalStorageItem(key, value);
MyLocalStorage.elements[key] = item;
}
}
// verifies if the localStorage is available
MyLocalStorage.isSupported = function() {
return typeof(Storage) !== "undefined";
}
// adds a new element
MyLocalStorage.add = function(queryString) {
// max limit reached exception
try {
key = MyLocalStorage.generateRandomKey();
item = new MyLocalStorageItem(key, queryString);
MyLocalStorage.elements[key] = item;
return true;
} catch (err) {
if(err == QUOTA_EXCEEDED_ERR) {
return false;
}
}
}
// retrieves an element
MyLocalStorage.get = function(key) {
if(key in MyLocalStorage.elements) {
return MyLocalStorage.elements[key];
}
return null;
}
// removes an element
MyLocalStorage.remove = function(key) {
if(item = MyLocalStorage.get(key)) {
item.delete();
delete MyLocalStorage.elements[key];
}
}
// verifies if some elements are stored using this same class
MyLocalStorage.isEmpty = function() {
return MyLocalStorage.getLength() == 0;
}
// retrieves the number of elements in the local storage
MyLocalStorage.getLength = function() {
return Object.keys(MyLocalStorage.elements).length;
}
// removes all the elements added through this class
MyLocalStorage.empty = function() {
for(var k in MyLocalStorage.elements){
MyLocalStorage.remove(k);
}
}
MyLocalStorage.submitAll = function(callback) {
for(var k in MyLocalStorage.elements){
item = MyLocalStorage.elements[k];
item.submit(callback);
}
}
MyLocalStorage.generateRandomKey = function() {
var d = new Date();
return MyLocalStorage.preKey + d.getTime() + Math.floor((Math.random()*100)+1);
}
MyLocalStorage.show = function() {
console.log("\n#### LocalStorage");
if(MyLocalStorage.isEmpty()) {
console.log("## No data");
} else {
for(var key in MyLocalStorage.elements){
MyLocalStorage.elements[key].show();
}
}
console.log("####\n");
}
/* ---------------------------------------- */
/* - Item class --------------------------- */
/* ---------------------------------------- */
function MyLocalStorageItem(key, value) {
this.key = key;
localStorage.setItem(key, value);
}
MyLocalStorageItem.prototype.setValue = function(value) {
localStorage.setItem(this.key, value);
}
MyLocalStorageItem.prototype.getKey = function() {
return this.key;
}
MyLocalStorageItem.prototype.getValue = function() {
return localStorage.getItem(this.key);
}
MyLocalStorageItem.prototype.delete = function() {
localStorage.removeItem(this.key);
}
MyLocalStorageItem.prototype.submit = function(callback) {
callback(this);
}
MyLocalStorageItem.prototype.show = function() {
console.log("Key(" + this.getKey() + ")\tValue(" + this.getValue() + ")");
}