cabinet.js
A simple interface for accessing local and session storage.
Installation
npm install cabinet-storage
Implementation Example
import Cabinet from "cabinet-storage"
Methods
Cabinet uses local storage by default. Access session storage under the Cabinet.session
namespace.
Cabinet.set(key, val[, metadata])
Creates an item inside local storage
Parameters
key
(String | Number)val
(Any)metadata
(Object: optional)
Examples
Cabinet.set("dog", "Scooby")
Cabinet.set("total", 50/(10-5))
Cabinet.set("person", {name: "Michael", age: 26, gender: "Male"});
Cabinet.set("animals", ["lion", "tiger", "bear"]);
Cabinet.set("friends", [{name: "Stan"}, {name: "Wendy"}]);
Cabinet.set("check", true);
Cabinet.set("reminder", new Date(Date.now() + 1000));
Working with expirations
Cabinet.set("reminder", "Thanks a ton", {
expires: new Date(Date.now() + 300000000)
})
Cabinet.set("partyime", "It's time to party", {
expires: {value: 30, unit: "day"}
})
Cabinet.set("dueDate", "Project due", {
expires: {value: 3, unit: "hour"}
})
Cabinet.get(key[, defaultVal])
Retrieves an item from local storage if it exists
Parameters
key
(String | Number)defaultVal
(Any: optional)
Examples
Cabinet.get("friends")
Cabinet.get("doesntExist")
Cabinet.get("friends").map(item => {
let name = item.name
})
Default Values
Cabinet.get("todos", [])
Cabinet.get("nameField", "Enter Your Name")
Cabinet.getAll()
Retrieves all items from local storage
Examples
Cabinet.getAll()
Cabinet.getAll().filter(val => typeof val === "number")
Cabinet.remove(key)
Removes an item from local storage if it exists
Parameters
key
(String | Number)
Examples
Cabinet.remove("reminder")
Cabinet.remove("doesntExist")
Cabinet.removeAll()
Removes all items from local storage
Examples
Cabinet.removeAll()
Cabinet.getMetadata(key)
Retrieves a set of attributes that Cabinet stores with an item inside local storage
Parameters
key
(String | Number)
Examples
const friends = Cabinet.getMetadata("friends")
friends.dateCreated
friends.expires
friends.source
friends.type
friends.val
let expires = new Date(Cabinet.getMetadata("reminder").expires)
if (Cabinet.getMetadata("reminder").source === "CABINET_STORAGE") {
}
Cabinet.removeExpired()
Removes all expired items from local storage
Examples
Cabinet.removeExpired()
Cabinet.removeExpired()
Cabinet.count()
Returns a count of the number of items saved in local storage
Examples
Cabinet.count()
Cabinet.keys()
Returns an array containing all of the keys in local storage
Examples
Cabinet.keys()
Session Storage Usage
Cabinet.session.methodName()
Access session storage under the Cabinet.session namespace.
Cabinet.session.get(key)
Cabinet.session.set(key, val)
Cabinet.session.getAll()
Cabinet.session.remove(key)
Cabinet.session.removeAll()
Cabinet.session.getMetadata()
Cabinet.session.removeExpired()
Cabinet.session.keys()
Cabinet.session.count()
Development Build
npm install --save-dev
npm run demo
Next, open /demo/dist/index.html
in your browser.
Interact with the module from /demo/src/demo.js
. You can log output to the console or render output to the examples index page.
Production Build
npm run build
production build is output to /dist/main.js