-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (36 loc) · 852 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
class Storage {
constructor(type) {
if (typeof window !== 'undefined') {
this.storage = window[type];
}
}
add(key, value, parse) {
this.storage.setItem(key, parse ? JSON.stringify(value) : value);
}
get(key, parse) {
const value = this.storage.getItem(key);
if (value === null) return false;
if (value && parse) return JSON.parse(value);
if (value) return value;
}
has(key) {
const value = this.storage.getItem(key);
if (value === null || value == undefined) {
return false;
}
return true;
}
del(key) {
this.storage.removeItem(key);
}
clear() {
this.storage.clear();
}
}
const types = ['localStorage', 'sessionStorage'];
const localStorage = new Storage(types[0]);
const sessionStorage = new Storage(types[1]);
export {
localStorage,
sessionStorage
};