storagens
v1.0.2
Published
## Install
Downloads
72
Maintainers
Readme
StorageNamespace
Install
npm install storagens
# or
yarn add storagens
Using
Create instance
import { LocalStorage, SessionStorage } from "storagens";
const localStorage = new LocalStorage();
const sessionStorage = new SessionStorage();
// set namespace
const userLocalStorage = new LocalStorage("user");
const userSessionStorage = new SessionStorage("user");
Set value
userLocalStorage.set("name", "Bean"); // => void
// user.name: "{\"value\":\"Bean\",\"updateTime\":1623985976434,\"expireTime\":0}"
// expireTime default:0
Set value and set expireTime
userLocalStorage.set(
"token",
"A8515509F42D80553AE2495DCDBFE9A7",
Date.now() + 864e5
); // => void
// user.token: "{\"value\":\"A8515509F42D80553AE2495DCDBFE9A7\",\"updateTime\":1623986348117,\"expireTime\":1624072748116}"
Get data by key
userLocalStorage.get("name"); // => {value: "Bean", updateTime: 1623986570960, expireTime: 0}
Get value by key
userLocalStorage.getValue("name"); // => Bean
Get all data
userLocalStorage.all(); // => {name: {value: "Bean", updateTime: 1623987036202, expireTime: 0}, token: {value: "A8515509F42D80553AE2495DCDBFE9A7", updateTime: 1623987036203, expireTime: 1624073436202}}
Get all values
userLocalStorage.allValues(); // => {name: "Bean", token: "A8515509F42D80553AE2495DCDBFE9A7"}
Check if the key exists in the storage
userLocalStorage.has("name"); // => true
userLocalStorage.has("age"); // => false
Check if the key has expired
userLocalStorage.expired("token"); // => boolean
Clear all expired data
userLocalStorage.clearExpired(); // => number
Delete one data by key
userLocalStorage.delete("name"); // => void
Clear all data
userLocalStorage.clear(); // => void