-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsimple-storage.js
74 lines (60 loc) · 1.36 KB
/
simple-storage.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
SimpleStorage = {
storage : window.localStorage,
setItem : function (key, value) {
this.storage.setItem(key, JSON.stringify(value));
},
getItem : function (key) {
return this.parse(this.storage.getItem(key));
},
parse : function (result) {
return JSON.parse(result);
},
getAll : function () {
items = [];
for (var i in this.storage){
if (i.length){
items.push({key:i, value:this.storage.getItem(i)});
}
}
if(items.length > 0){
return items;
}
return undefined;
},
removeItem : function (key) {
this.storage.removeItem(key);
},
clear : function () {
this.storage.clear();
},
key : function (key) {
return this.storage.key(key);
},
setTTL : function (key, value, time, callback) {
this.setItem(key, value);
setTimeout(function() {
SimpleStorage.removeItem(key);
callback();
}, time)
},
pushArray : function (key, value) {
arr = this.getItem(key);
arr.push(value);
this.setItem(key, arr);
},
getAllAsync : function (callback){
window.setTimeout(function() {
callback(SimpleStorage.getAll());
}, 0);
},
setItemAsync : function (key, value, callback){
window.setTimeout(function() {
callback(SimpleStorage.setItem(key, value));
}, 0);
},
getItemAsync : function (key, callback){
window.setTimeout(function() {
callback(SimpleStorage.getItem(key));
}, 0);
}
};