aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/local_storage/local_storage.ts
blob: 13d9ca401d0de703abcd4bc582eece4ed643d05e (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 (!localStorage.doesExist) {
            return undefined;
        }
        const item = window.localStorage.getItem(key);
        if (_.isNull(item) || item === 'undefined') {
            return '';
        }
        return item;
    },
    setItem(key: string, value: string): void {
        if (!localStorage.doesExist || _.isUndefined(value)) {
            return;
        }
        window.localStorage.setItem(key, value);
    },
    removeItem(key: string): void {
        if (!localStorage.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 (!localStorage.doesExist) {
            return [];
        }
        return _.keys(window.localStorage);
    },
};