storage.js Module Documentation

Bahasa Indonesia

storage.js saves, reads, and deletes data in the browser’s localStorage and sessionStorage. Unlike the built-in API, values are JSON.stringify/JSON.parsed automatically (so objects and arrays can be stored directly), and localStorage data can be given an expiry in days, hours, or seconds.

Usage (Import)

import { setLocal, getLocal, deleteLocal } from "https://cdn.jsdelivr.net/gh/crootjs/lib@0.0.43/storage.js";

setLocal(key, value)

Saves value to localStorage with no expiry: it stays until it is deleted or the site data is cleared. value can be anything that converts to JSON (string, number, boolean, null, object, array).

Returns true when saved, false when storage is blocked or full.

setLocal("prefs", { theme: "dark", fontSize: 16 });

setLocalWithExpireDay(key, value, exdays)

setLocalWithExpireHour(key, value, exhour)

setLocalWithExpireSecond(key, value, exsecs)

Same as setLocal, with an expiry of exdays days, exhour hours, or exsecs seconds from now. After that, getLocal returns null and removes the key from storage. Expired data is not removed until it is read.

The time unit differs per function. The cache.ttl option in api.js also uses seconds, like setLocalWithExpireSecond.

setLocalWithExpireHour("articleDraft", { title: "Flooding in Dayeuhkolot" }, 2); // valid for 2 hours

getLocal(key)

Reads a value from localStorage, already parsed from JSON. Returns null when the key does not exist, has expired, is not JSON, or storage is blocked.

const prefs = getLocal("prefs"); // { theme: "dark", fontSize: 16 } or null

getLocal is only for keys written through this module. A value written with plain localStorage.setItem gives null (if it is not JSON) or undefined.

deleteLocal(key)

Removes the key from localStorage. Returns nothing.

setSession(key, value)

getSession(key)

deleteSession(key)

Same as setLocal, getLocal, and deleteLocal, but for sessionStorage: the data is gone when the tab is closed. There are no expiry variants.

setSession("formStep", 2);
const step = getSession("formStep"); // 2

Notes