Helpers to make local storage easier to use and just like cookie, having expire time.
import localStore from '@hooshid/local-store'set— Add item to local storage.get— Get item from local storageremove— Remove item from local storage.clearAll— Removes all or just Prefixed items from local storage.has— Check item exist in local storage.getExpireTime— Get item expire time.getRemainTime— Get remaining TTL of a item.
Set an item in local storage.
set(key: string, value: string, ttl?: number | string): voidkey- The key of the item to set.value- The value to set.ttl- The time life in seconds format for living cache in localStorage.ttl- The time life in default is one year, if you want set time life you should set in seconds or set like below format.
m for minute
h for hour
d for day- 60m -> 60 minute
- 1h -> 1 hour (equal to 60m)
- 24h -> 24 hours (equal to 1d)
- 1d -> 1 day
- 30d -> 30 days
just use 1 above method and don't mixed (1d12h does not work -> 36h work fine!)
Get an item from local storage.
get(key: string): string | nullkey— The key of the item to get.
Remove item from local storage.
remove(key: string): voidkey— The key of the item to remove.
Removes all or just Prefixed items from local storage.
clearAll(prefix?: string): voidprefix— if prefix provided, only keys start with prefix cleared and if prefix undefined all keys cleared.
Check key is exist
has(key: string): booleankey— The key of the item.
Get item expire time.
getExpireTime(key: string): null|numberkey— The key of the item.
Get remaining time of a item.
getRemainTime(key: string): null|numberkey— The key of the item.
- method for clear expired caches
- output js script and define types separately