-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
42 lines (33 loc) · 1.02 KB
/
index.ts
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
interface ProxyStorage {
getItem: (key: string) => any;
setItem: (Key: string, value: string) => void;
removeItem: (key: string) => void;
clear: () => void;
}
// sessionStorage operate
class SessionStorageProxy implements ProxyStorage {
protected storage: ProxyStorage;
constructor(storageModel: ProxyStorage) {
this.storage = storageModel;
}
// 存
public setItem(key: string, value: any): void {
this.storage.setItem(key, JSON.stringify(value));
}
// 取
public getItem(key: string): any {
return JSON.parse(this.storage.getItem(key));
}
// 删
public removeItem(key: string): void {
this.storage.removeItem(key);
}
// 清空
public clear(): void {
this.storage.clear();
}
}
// localStorage operate
class LocalStorageProxy extends SessionStorageProxy implements ProxyStorage {}
export const storageSession = new SessionStorageProxy(sessionStorage);
export const storageLocal = new LocalStorageProxy(localStorage);