localstory
A lightweight (0.8kb) wrapper around browser storage APIs (localStorage / sessionStorage).
Features
- namespaces to avoid key collisions
- per-value TTL expires
- safe API (avoids throwing errors)
- zero dependencies
npm install localstory
Usage
const store = windowlocalStorage 'myNamespace' ttl: '45m' storestore // 'bar'store store // ['foo']storeclear
Namespacing
Use namespaces to freely use IDs without fear of collisions between different applications/stores in the same domain.
const horses = const ponies = horsesponies // undefined ponies horses // 'Thunder'ponies // 'Sparkle'
Expiry / TTL
Automatically expire values. Expiry is validated on reads and once on load (can be disabled with vacuum: false
). Each key can have it's own expiry time.
To enable, provide { ttl: [milliseconds] }
as second parameter to the set()
method:
store store // 'bar'store // one hour later... undefined // time parameter takes human readable strings: [s]econds, [m]inutes, [h]ours, [d]aysstore store // removes all expired keys // disable automatic vacuum on startupconst store = // set custom delay (in ms) for vacuum on startupconst store =
Tests
Uses the tape test runner. Run tests with npm test
after installing dependencies.
Contributing
https://github.com/ricardobeat/localstory
Made by Ricardo Tomasi