You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
853 B
JavaScript
36 lines
853 B
JavaScript
function storageWrapper(cb) {
|
|
if (typeof window.localStorage !== 'undefined') {
|
|
return cb(window.localStorage);
|
|
} else {
|
|
console.log("localStorage not available");
|
|
}
|
|
}
|
|
|
|
|
|
export function persist(key, object) {
|
|
storageWrapper((storage) => {
|
|
storage.setItem(key, JSON.stringify(object));
|
|
});
|
|
};
|
|
|
|
export function push(key, object) {
|
|
storageWrapper((storage) => {
|
|
const item = storage.getItem() || "[]",
|
|
parsed = JSON.parse(item);
|
|
if (Array.isArray(parsed)) {
|
|
parsed.push(object);
|
|
storage.setItem(key, JSON.stringify(parsed));
|
|
}
|
|
});
|
|
}
|
|
|
|
export function load(key) {
|
|
return storageWrapper((storage) => {
|
|
const item = storage.getItem(key);
|
|
if (item) {
|
|
return JSON.parse(item);
|
|
}
|
|
return null;
|
|
});
|
|
}
|