-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
176 lines (148 loc) · 2.86 KB
/
index.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
var ls;
/**
* test ls
* make sur ls exists and
* workaround `SecurityError: DOM Exception 18`
*/
try {
ls = window.localStorage;
ls.setItem('store-test', 'test');
ls.removeItem('store-test');
} catch(e) {
ls = {
clear: function () {},
getItem: function () {},
removeItem: function () {},
setItem: function () {}
};
}
/**
* Initialize a new store
*
* @param {String} [prefix] prefix added to localstorage keys
* @api private
*/
function Store(_) {
if (!(this instanceof Store)) return new Store(_);
this._ = _ || '';
}
/**
* expose new Store instance
*/
module.exports = new Store();
/**
* create a new store
* @param {String} prefix
*
* @api public
*/
Store.prototype.prefix = function(prefix) {
return new Store(prefix);
};
// alias prefix with ns
Store.prototype.ns = Store.prototype.prefix;
/**
* set a key, call JSON.stringify to serialize value
* @param {String} key
* @param {Object} val
*
* @return {Number} return val.length or 0 if anything has been thrown
*
* @api public
*/
Store.prototype.set = function(key, val) {
var data;
try {
data = JSON.stringify(val || null);
ls.setItem(this._ + key, data);
return data.length;
} catch(e) {
return 0;
}
};
/**
* set a key, value without serializing
* @param {String} key
* @param {String} val
*
* @return {[Error]} return null or Error if anything has been thrown
* @api public
*/
Store.prototype.setItem = function(key, val) {
if (!val) return;
try {
ls.setItem(this._ + key, val);
return null;
} catch(e) {
return e;
}
};
/**
* get a key call JSON.parse to deserialize value
* @param {String} [key]
* @return {Object}
*
* @api public
*/
Store.prototype.get = function(key) {
try {
return JSON.parse(ls.getItem(this._ + (key || '')));
} catch (_error) {
return null;
}
};
/**
* get Item as string without deserializing
* @param {String} [key]
* @return {Object}
*
* @api public
*/
Store.prototype.getItem = function(key) {
return ls.getItem(this._ + (key || ''));
};
/**
* save the prefix value
* shortcut for store.set('', val)
*
* @param {Object} val
* @api public
*/
Store.prototype.save = function(val) {
return this.set('', val);
};
/**
* unset a key
* @param {String} key
*
* @api public
*/
Store.prototype.unset = function(key) {
ls.removeItem(this._ + key);
};
// alias unset with del
Store.prototype.del = Store.prototype.unset;
/**
* clear all keys matching prefix
*
* @api public
*/
Store.prototype.clear = function() {
var prefix = this._,
keys = Object.keys(ls).filter(function(key) {
return key.indexOf(prefix) === 0;
});
keys.forEach(function(key) {
ls.removeItem(key);
});
};
// alias clear with reset
Store.prototype.reset = Store.prototype.clear;
/**
* clear all values
*
* @api public
*/
Store.prototype.clearAll = function() {
ls.clear();
};