@pikapower9080/easy-storage
v1.0.4
Published
Lightweight library of localStorage helper functions.
Downloads
40
Readme
@pikapower9080/easy-storage
easy-storage
is a JavaScript library that makes localStorage better by storing multiple values under one key.
Advantages of single-key storage
- Reduces risk of conflict between keys when multiple apps are running on one domain (e.g. localhost)
- Allows for the backing up, overwriting, and clearing of all stored data without touching data stored by other apps or userscripts
Usage
EasyStorage is made for use with ESM modules. It is documented with JSDoc for smart intellisense features. Here is a basic example of how to use the library:
import EasyStorage from '@pikapower9080/easy-storage'
const storage = new EasyStorage({
key: "my-app-name"
})
if (!storage.get("first-visit", false)) {
alert("Welcome!")
storage.set("first-visit", new Date().getTime())
}
Additional methods are available and should be auto-completed by your editor.
Options
You can pass these options when you call new EasyStorage({})
| Key | Description | Default |
| ------------------- | ---------------------------------------------------------------------------------- | -------- |
| key
| localStorage key to store all data in | config
|
| default
| Default values to set if there is no data stored or the data is corrupted | {}
|
| migration.enabled
| Enables the migration system which can move user data from an old key to a new one | false
|
| migration.old_key
| The old key to migrate old user data from (migration.enabled must be true) | N/A |
Documentation
EasyStorage
Main class for EasyStorage.
Kind: global class
easyStorage.keys
Array of keys present in storage.
Kind: instance property of EasyStorage
Example
console.log(storage.keys.length)
easyStorage.getAll() ⇒ object
Gets all stored data as an object.
Kind: instance method of EasyStorage
easyStorage.set(key, value)
Set the value for a key and save it to localStorage.
Kind: instance method of EasyStorage
| Param | Type | Description | | --- | --- | --- | | key | string | The key to be stored inside of the main localStorage key | | value | any | Value to set |
easyStorage.get(key, [defaultValue]) ⇒ any
Get the value for a key.
Kind: instance method of EasyStorage
Returns: any - The stored value
| Param | Type | Description | | --- | --- | --- | | key | string | The key to get the value from inside of the main localStorage key | | [defaultValue] | any | Value to return if the data is not present |
easyStorage.clearAll()
Clear all stored data.
Kind: instance method of EasyStorage
easyStorage.setAll(data)
Replace all stored data with an object.
Kind: instance method of EasyStorage
| Param | Type | Description | | --- | --- | --- | | data | object | Object to replace stored data with |
easyStorage.getAllOfType(type) ⇒ object
Gets all values of a certain type.
Kind: instance method of EasyStorage
Returns: object - Object with filtered values
| Param | Type | Description | | --- | --- | --- | | type | string | Type to get |
easyStorage.getAllIf(predicate)
Gets all key/value pairs where the specified condition is true.
Kind: instance method of EasyStorage
| Param | Type | Description | | --- | --- | --- | | predicate | function | Predicate to check for key/value pairs to be included |
Example
storage.getAllIf((key, value, index) => key.includes('@'))
Example
storage.getAllIf((key, value, index) => {
if (index % 2 === 0) {
return true
}
})
easyStorage.toArray([flat]) ⇒ object
Returns all values in array format.
Kind: instance method of EasyStorage
Returns: object - Array of keys and values
| Param | Type | Description | | --- | --- | --- | | [flat] | boolean | Makes the array a list of objects with key and value properties |
easyStorage.forEach(callback)
Executes a callback function for each stored key/value pair.
Kind: instance method of EasyStorage
| Param | Type | Description | | --- | --- | --- | | callback | function | Function to be executed for each key |