forked from getify/h5ive-DEPRECATED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.h5ive.js
78 lines (59 loc) · 1.67 KB
/
storage.h5ive.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
/*! storage.h5ive.js | (c) Kyle Simpson | MIT License: http://getify.mit-license.org */
(function(h5){
if (!h5) throw new Error("storage.h5ive: core.h5ive required.");
h5.storage = function(opts) {
var store, publicAPI, expires;
opts = opts || {};
if ("expires" in opts && typeof opts.expires === "number" && opts.expires > 0) {
expires = opts.expires + (new Date()).getTime();
}
if (opts.expires == "session") store = sessionStorage;
else store = localStorage;
function save(data) {
var key, val;
for (key in data) {
val = { "h5ive:data": data[key] };
if (expires) val["h5ive:expires"] = expires;
store.setItem(key,JSON.stringify(val));
}
return publicAPI;
}
function discard(keys) {
if (Object.prototype.toString.call(keys) != "[object Array]") keys = [keys];
for (var i=0; i<keys.length; i++) {
store.removeItem(keys[i]);
}
return publicAPI;
}
function get(keys) {
var i, val, ret = {}, now = (new Date()).getTime();
if (Object.prototype.toString.call(keys) !== "[object Array]") keys = [keys];
for (i=0; i<keys.length; i++) {
val = ret[keys[i]] = store.getItem(keys[i]);
try {
val = JSON.parse(val);
if ("h5ive:data" in val) {
if ("h5ive:expires" in val && now >= val["h5ive:expires"]) {
delete ret[keys[i]];
store.removeItem(keys[i]);
continue;
}
ret[keys[i]] = val["h5ive:data"];
}
}
catch (err) { }
}
if (keys.length < 2) {
if (keys.length > 0 && (keys[0] in ret)) return ret[keys[0]];
else return;
}
return ret;
}
publicAPI = {
save: save,
discard: discard,
get: get
};
return publicAPI;
};
})(this.h5);