-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (39 loc) · 896 Bytes
/
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
class Storage {
constructor(prefix = 'pj', storage = sessionStorage) {
this.storage = storage
this.prefix = prefix + '_'
}
setItem (key, value) {
this.storage.setItem(this.prefix + key, JSON.stringify(value))
}
getItem (key) {
let val = this.storage.getItem(this.prefix + key)
try {
val = JSON.parse(val)
} catch (err) { /* */ }
return val
}
removeItem (key) {
this.storage.removeItem(this.prefix + key)
}
getKeyList () {
let arr = []
for (let i = 0; i < this.storage.length; i++) {
let key = this.storage.key(i)
if (key.indexOf(this.prefix) == 0) {
arr.push(key)
}
}
return arr
}
clear () {
let arr = this.getKeyList()
for (var i = 0; i < arr.length; i++) {
this.storage.removeItem(arr[i]);
}
}
clearAll () {
this.storage.clear()
}
}
export default Storage