@bjnstnkvc/session-storage
v1.0.7
Published
Simple wrapper for JavaScript Session Storage API.
Readme
SessionStorage
A class that provides a set of methods for interacting with the browser's session storage.
Installation & setup
NPM
You can install the package via npm:
npm install @bjnstnkvc/session-storageand then import it into your project
import { SessionStorage } from '@bjnstnkvc/session-storage';CDN
You can install the package via jsDelivr CDN:
<script src="https://cdn.jsdelivr.net/npm/@bjnstnkvc/session-storage/lib/main.min.js"></script>Usage
set
Set the value for a given key in the Session Storage.
Parameters
- key - String containing the name of the key.
- value - The value to be stored.
Example
SessionStorage.set('key', 'value'); get
Retrieve the value associated with the given key from the Session Storage.
Parameters
- key - String containing the name of the key.
- fallback (optional) - The fallback value in case the key does not exist. Defaults to
null.
Example
SessionStorage.get('key', 'default');You can also pass a closure as the default value. If the specified item is not found in the Session Storage, the closure will be executed and its result returned. This allows you to lazily load default values from other sources:
SessionStorage.get('key', () => 'default');remember
Retrieve the value associated with the given key, or execute the given callback and store the result in the Session Storage.
Parameters
- key - String containing the name of the key.
- fallback - Function you want to execute.
Example
SessionStorage.remember('key', () => 'default');all
Retrieve an array containing all keys and their associated values stored in the Session Storage.
Example
SessionStorage.all();Note: The
allmethod returns an array of objects withkeyandvalueproperties (e.g.[{ key: 'key', value: 'value' }])
remove
Remove the key and its associated value from the Session Storage.
Parameters
- key - String containing the name of the key to be deleted.
Example
SessionStorage.remove('key');clear
Clear all keys and their associated values from the Session Storage.
Example
SessionStorage.clear();has
Check if a key exists in the Session Storage.
Parameters
- key - String containing the name of the key to be checked.
Example
SessionStorage.has('key');hasAny
Check if any of the provided keys exist in the Session Storage.
Parameters
- keys - String or an array of strings containing the names of the keys to be checked.
Example
SessionStorage.hasAny(['key1', 'key2']);isEmpty
Check if the Session Storage is empty.
Example
SessionStorage.isEmpty();isNotEmpty
Check if the Session Storage is not empty.
Example
SessionStorage.isNotEmpty();keys
Retrieve an array containing all keys stored in the Session Storage.
Example
SessionStorage.keys();count
Retrieve the total number of items stored in the Session Storage.
Example
SessionStorage.count();dump
Print the value associated with a key to the console.
Parameters
- key - String containing the name of the key.
Example
SessionStorage.dump('key');fake
Replace the Session Storage instance with a fake implementation. This is particularly useful for testing purposes where you want to avoid interacting with the actual browser's Session Storage.
Example
SessionStorage.fake();restore
Restore the original Session Storage instance. This is typically used after fake() to return to using the real browser's Session Storage.
Example
SessionStorage.restore();isFake
Check if a fake Session Storage instance is currently being used.
Returns
- boolean -
trueif a fake instance is being used,falseotherwise.
Example
if (SessionStorage.isFake()) {
// ...
}