mirror of
https://github.com/coalaura/whiskr.git
synced 2025-09-09 09:19:54 +00:00
49 lines
757 B
JavaScript
49 lines
757 B
JavaScript
![]() |
function storeValue(key, value) {
|
||
|
if (!value) {
|
||
|
localStorage.removeItem(key);
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
localStorage.setItem(key, JSON.stringify(value));
|
||
|
}
|
||
|
|
||
|
function loadValue(key, fallback = false) {
|
||
|
const raw = localStorage.getItem(key);
|
||
|
|
||
|
if (!raw) {
|
||
|
return fallback;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
const value = JSON.parse(raw);
|
||
|
|
||
|
if (!value) {
|
||
|
throw new Error("no value");
|
||
|
}
|
||
|
|
||
|
return value;
|
||
|
} catch {}
|
||
|
|
||
|
return fallback;
|
||
|
}
|
||
|
|
||
|
function uid() {
|
||
|
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
||
|
}
|
||
|
|
||
|
function make(tag, ...classes) {
|
||
|
const el = document.createElement(tag);
|
||
|
|
||
|
el.classList.add(...classes);
|
||
|
|
||
|
return el;
|
||
|
}
|
||
|
|
||
|
function escapeHtml(text) {
|
||
|
return text
|
||
|
.replace(/&/g, "&")
|
||
|
.replace(/</g, "<")
|
||
|
.replace(/>/g, ">");
|
||
|
}
|