aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/local_storage/local_storage.ts
blob: d94e6877b18b5c5d05734819bff2067b9ea65c52 (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
import * as _ from 'lodash';

export const localStorage = {
    doesExist() {
        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) {
        if (!this.doesExist || _.isUndefined(value)) {
            return;
        }
        window.localStorage.setItem(key, value);
    },
    removeItem(key: string) {
        if (!this.doesExist) {
            return;
        }
        window.localStorage.removeItem(key);
    },
    getAllKeys(): string[] {
        if (!this.doesExist) {
            return [];
        }
        return _.keys(window.localStorage);
    },
};