-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocal-storage.js
41 lines (30 loc) · 1.11 KB
/
local-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
/* Local Storage Class */
function mMLocalStorage(key) {
this.key = key || 'mobileMule.settings';
mMLocalStorage.prototype._init();
}
mMLocalStorage.prototype._init = function() {
if (!('localStorage' in window && window['localStorage'] !== null)) {
throw new Error('No LocalStorage supported on this browser!');
}
}
mMLocalStorage.prototype.set = function(name, value) {
var settings = JSON.parse(localStorage.getItem(this.key)) || {};
settings[name] = value;
localStorage.setItem(this.key, JSON.stringify(settings));
}
mMLocalStorage.prototype.get = function(name, defaultValue) {
defaultValue = defaultValue || undefined;
var settings = JSON.parse(localStorage.getItem(this.key)) || {};
return name in settings ? settings[name] : defaultValue;
}
mMLocalStorage.prototype.unset = function(name) {
var settings = JSON.parse(localStorage.getItem(this.key));
if (name in settings) {
delete settings[name];
localStorage.setItem(this.key, JSON.stringify(settings));
}
}
mMLocalStorage.prototype.erase = function() {
localStorage.removeItem(this.key);
}