Xianny 7423028fea
Replace lodash with built-ins where possible to reduce bundle size (#1766)
* add tslint rule to disallow lodash.isUndefined

* add tslint rule to disallow lodash.isNull

* apply fixes
2019-04-10 09:36:32 -07:00

46 lines
1.2 KiB
TypeScript

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 (item === null || item === 'undefined') {
return '';
}
return item;
},
setItem(key: string, value: string): void {
if (!localStorage.doesExist || value === undefined) {
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);
},
};