-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-storage.js
181 lines (154 loc) · 3.86 KB
/
web-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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Web Storage 0.2 | https://github.com/brandonhall/web-storage
* Copyright (c) Brandon Hall <b@brandonhall.me> | The MIT License
*/
;(function(window) {
window.WebStorage = {
/**
* LocalStorage Handling
*/
Local: {
/**
* Set localStorage key/value pair
* @param name
* @param value
*/
set: function (name, value) {
if (name === undefined || value === undefined) {
throw new Error('name or value missing from WebStorage.Local.set');
}
if (!this._localStorageTest()) {
return;
}
localStorage.setItem(name, JSON.stringify(value));
},
/**
* Get localStorage value
* @param name
*/
get: function (name) {
if (name === undefined) {
throw new Error('name missing from WebStorage.Local.get');
}
if (!this._localStorageTest()) {
return;
}
var val = localStorage.getItem(name);
try {
return JSON.parse(val);
}
catch(e) {
return val;
}
},
/**
* Remove localStorage item
* @param name
*/
remove: function (name) {
if (name === undefined) {
throw new Error('name missing from WebStorage.Local.remove');
}
if (this._localStorageTest()) {
localStorage.removeItem(name);
}
},
/**
* Check to see if localStorage is enabled
* @returns {*|boolean}
*/
enabled: function () {
return this._localStorageTest();
},
/**
* Ensure localStorage is available and working
* @returns {boolean}
* @private
*/
_localStorageTest: function () {
var lsTest = '4GIwa24sFzOI';
try {
localStorage.setItem(lsTest, lsTest);
localStorage.removeItem(lsTest);
return true;
}
catch (e) {
return false;
}
}
},
/**
* SessionStorage Handling
*/
Session: {
/**
* Set sessionStorage key/value pair
* @param name
* @param value
*/
set: function (name, value) {
if (name === undefined || value === undefined) {
throw new Error('name or value missing from WebStorage.Session.set');
}
if (!this._sessionStorageTest()) {
return;
}
sessionStorage.setItem(name, JSON.stringify(value));
},
/**
* Get sessionStorage value
* @param name
*/
get: function (name) {
if (name === undefined) {
throw new Error('name missing from WebStorage.Local.get');
}
if (!this._sessionStorageTest()) {
return;
}
var val = sessionStorage.getItem(name);
try {
return JSON.parse(val);
}
catch(e) {
return val;
}
},
/**
* Remove sessionStorage item
* @param name
*/
remove: function (name) {
if (name === undefined) {
throw new Error('name missing from WebStorage.Session.remove');
}
if (this._sessionStorageTest()) {
sessionStorage.removeItem(name);
}
},
/**
* Check to see if sessionStorage is enabled
* @returns {*|boolean}
*/
enabled: function () {
return this._sessionStorageTest();
},
/**
* Ensure sessionStorage is available and working
* @returns {boolean}
* @private
*/
_sessionStorageTest: function () {
var ssTest = 'aMq0Tn8vse6W';
try {
sessionStorage.setItem(ssTest, ssTest);
sessionStorage.removeItem(ssTest);
return true;
}
catch (e) {
return false;
}
}
}
}
})(window, undefined);