Methods
(static) getLocal(key) → {*}
Retrieve a value from localStorage by key. Automatically unwraps the storage envelope and checks TTL expiration. Expired items are removed from localStorage.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string
|
The localStorage key |
Returns:
- Type:
-
*
The stored value, or null if the key doesn't exist, has expired, or localStorage is unavailable
Example
setLocal('user', { name: 'Nikola' })
getLocal('user') // => { name: 'Nikola' }
getLocal('nonexistent') // => null
(static) isExpired(timestamp, ttl) → {boolean}
Check if a timestamp has exceeded its TTL
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
timestamp |
number
|
The timestamp in milliseconds to check |
ttl |
number
|
The time-to-live in milliseconds |
Returns:
- Type:
-
boolean
True if the timestamp has expired, false otherwise
Example
isExpired(Date.now() - 1000, 500) // => true
isExpired(Date.now() - 500, 1000) // => false
(static) isLocalStorageAvailable() → {boolean}
Check if localStorage is available and functional. Caches the result after the first successful check for performance.
- Source:
Returns:
- Type:
-
boolean
True if localStorage is available, false otherwise
Example
if (isLocalStorageAvailable()) {
setLocal('key', 'value')
}
(static) removeLocal(key)
Remove an item from localStorage by key
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string
|
The localStorage key to remove |
Example
setLocal('key', 'value')
removeLocal('key')
getLocal('key') // => null
(static) setLocal(key, value, ttlopt, updateopt) → {*}
Store a JSON-serializable value in localStorage wrapped in an envelope with a timestamp. Optionally set a TTL for automatic expiration on read. When update is true, preserves the original __storedAt timestamp and TTL of an existing item.
- Source:
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
key |
string
|
The localStorage key |
||
value |
*
|
The value to store (must be JSON-serializable) |
||
ttl |
number
|
<optional> |
Optional time-to-live in milliseconds |
|
update |
boolean
|
<optional> |
false |
If true, preserve the original __storedAt and TTL |
Returns:
- Type:
-
*
The stored value, or null if localStorage is unavailable, the value can't be serialized, or the item has expired (update mode)
Example
setLocal('key', 'value') // => 'value'
setLocal('key', { foo: 'bar' }, 60000) // expires in 1 minute
setLocal('key', 'new', null, true) // update value, keep original timestamp and TTL
(static) updateLocal(key, value) → {*}
Update the value of an existing localStorage item, preserving the original __storedAt timestamp and TTL. If the key doesn't exist or has no envelope, behaves like setLocal.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string
|
The localStorage key |
value |
*
|
The new value to store (must be JSON-serializable) |
Returns:
- Type:
-
*
The stored value, or null if localStorage is unavailable, the item has expired, or the value can't be serialized
Example
setLocal('key', 'old', 60000)
updateLocal('key', 'new') // => 'new' (keeps original timestamp and TTL)