lbry-desktop/ui/js/utils.js

31 lines
914 B
JavaScript
Raw Normal View History

/**
* Thin wrapper around localStorage.getItem(). Parses JSON and returns undefined if the value
* is not set yet.
*/
export function getLocal(key, fallback=undefined) {
const itemRaw = localStorage.getItem(key);
return itemRaw === null ? fallback : JSON.parse(itemRaw);
}
/**
* Thin wrapper around localStorage.setItem(). Converts value to JSON.
*/
export function setLocal(key, value) {
localStorage.setItem(key, JSON.stringify(value));
2017-04-10 14:32:40 +02:00
}
/**
* Thin wrapper around localStorage.getItem(). Parses JSON and returns undefined if the value
* is not set yet.
*/
2017-04-13 20:52:26 +02:00
export function getSession(key, fallback=undefined) {
2017-04-10 14:32:40 +02:00
const itemRaw = sessionStorage.getItem(key);
2017-04-13 20:52:26 +02:00
return itemRaw === null ? fallback : JSON.parse(itemRaw);
2017-04-10 14:32:40 +02:00
}
/**
* Thin wrapper around localStorage.setItem(). Converts value to JSON.
*/
export function setSession(key, value) {
sessionStorage.setItem(key, JSON.stringify(value));
2017-04-09 17:06:23 +02:00
}