localstorage
JSON-aware wrappers around localStorage, with an availability check and
safe no-ops when storage is unavailable (private browsing, quota, etc.).
Import: @rtorcato/browser-common/localstorage
๐ MDN: Web Storage API ยท ๐ caniuse: namevalue-storage
Exampleโ
import {
isLocalStorageAvailable,
setLocalStorage,
getLocalStorage,
removeLocalStorage,
} from '@rtorcato/browser-common/localstorage'
if (isLocalStorageAvailable()) {
setLocalStorage('prefs', { theme: 'dark' })
const prefs = getLocalStorage('prefs') as { theme: string } | null
removeLocalStorage('prefs')
}
Exportsโ
isLocalStorageAvailable()โ feature check, verified with a write/remove probesetLocalStorage(key, value)โ JSON-stringifies and storesvaluegetLocalStorage(key)โ parses and returns the stored value, ornull(caller must narrow the type)removeLocalStorage(key)โ removes a single keyclearLocalStorage()โ clears all keys
See the API reference for full signatures.