blob: 1e3258ce0ef8b258d7eaa63605e7016269763cf1 (
plain) (
blame)
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
|
import * as _ from 'lodash';
export const localStorage = {
doesExist(): boolean {
return !!window.localStorage;
},
getItemIfExists(key: string): string {
if (!this.doesExist) {
return undefined;
}
const item = window.localStorage.getItem(key);
if (_.isNull(item) || item === 'undefined') {
return '';
}
return item;
},
setItem(key: string, value: string): void {
if (!this.doesExist || _.isUndefined(value)) {
return;
}
window.localStorage.setItem(key, value);
},
removeItem(key: string): void {
if (!this.doesExist) {
return;
}
window.localStorage.removeItem(key);
},
getObject(key: string): object | undefined {
const item = localStorage.getItemIfExists(key);
if (item) {
return JSON.parse(item);
}
return undefined;
},
setObject(key: string, value: object): void {
localStorage.setItem(key, JSON.stringify(value));
},
getAllKeys(): string[] {
if (!this.doesExist) {
return [];
}
return _.keys(window.localStorage);
},
};
|