mirror of
https://github.com/Unleash/unleash.git
synced 2024-10-18 20:09:08 +02:00
35 lines
990 B
TypeScript
35 lines
990 B
TypeScript
// Get an item from localStorage.
|
|
// Returns undefined if the browser denies access.
|
|
export function getLocalStorageItem<T>(key: string): T | undefined {
|
|
try {
|
|
return parseStoredItem<T>(window.localStorage.getItem(key));
|
|
} catch (err: unknown) {
|
|
console.warn(err);
|
|
}
|
|
}
|
|
|
|
// Store an item in localStorage.
|
|
// Does nothing if the browser denies access.
|
|
export function setLocalStorageItem(key: string, value: unknown) {
|
|
try {
|
|
window.localStorage.setItem(
|
|
key,
|
|
JSON.stringify(value, (_key, value) =>
|
|
value instanceof Set ? [...value] : value
|
|
)
|
|
);
|
|
} catch (err: unknown) {
|
|
console.warn(err);
|
|
}
|
|
}
|
|
|
|
// Parse an item from localStorage.
|
|
// Returns undefined if the item could not be parsed.
|
|
function parseStoredItem<T>(data: string | null): T | undefined {
|
|
try {
|
|
return data ? JSON.parse(data) : undefined;
|
|
} catch (err: unknown) {
|
|
console.warn(err);
|
|
}
|
|
}
|