storage.js is object store build against the browsers storage engines localStorage and sessionStorage. It provides a simplified API to set, get, update, remove, and clear stored objects.
NOTE: Browser-Only library
To use storage.js, you can include it in your project using ES6 module import syntax. If you are using Node.js, you might need to transpile the code with Babel or a similar tool since some browsers do not support ES6 modules natively.
import Storage from './storage.js';
Storageor{Storage}
The Storage class constructor takes an optional parameter to specify the type of storage (local or session). default local
import {Storage} from './storage.js';
// Initialize with localStorage (default)
const local = new Storage();
// Initialize with sessionStorage
const session = new Storage('session');| Method | Description | Notes |
|---|---|---|
constructor(type) |
Initializes the storage type. | Defaults to 'local' if not specified. Accepts 'session' for sessionStorage. |
get(key) |
Retrieves a value from the storage by key. | Returns null if the key does not exist. |
set(key, value) |
Sets a value in the storage by key. | The value is automatically stringified using JSON.stringify(). |
update(key, json) |
Updates an existing item with new data. | If the key does not exist, it creates a new item. |
remove(key) |
Removes an item from the storage by key. | Does nothing if the key does not exist. |
clear() |
Clears all items from the storage. |
Browser tests are always a pain, but this will launch a browser, and run the tests there.
bun run testThis will run your tests and provide feedback on whether they pass or fail.
MIT License file for details.