This is a library written in ES6. It provides an abstraction layer for using the HTML5 web storages, localStorage
and sessionStorage
. On top of utilizing these Storage
types, a Global
storage is introduced for temporary data storage.
npm install --save web-storage-es6
- Local - A type of
localStorage
. Data persists until explicitly deleted by user. It has no expiration date - Session - A type of
sessionStorage
. Data lasts for as long as the browser is open and survives over page reloads - Global - Stores data in the global
window
variable. Data only lasts inside a single page session and will be destroyed upon page reload
// Include library
const WebStorageES6 = require('web-storage-es6');
// Create a local storage with 'default' namespace
var localStorage = new WebStorageES6('Local');
// Create a session storage with 'default' namespace
var sessionStorage = new WebStorageES6('Session');
// Create a global storage with 'custom' namespace
var customGlobalStorage = new WebStorageES6('Global', 'custom');
// Sets 'var1' to 'value1'
localStorage.put('var1', 'value1');
// Gets 'var1'
localStorage.get('var1');
// Gets 'var1'. If 'var1' is not set, return 'default value'
localStorage.get('var1', 'default value');
// Checks if 'var1' exists
localStorage.has('var1');
// Removes 'var1' from storage
localStorage.forget('var1');
// Writing to storage is slow if you need to save repetitive data in loops
for (let i = 0; i < 100; i++) {
storage.put('var' + i, i);
}
// Create data first
var data = {};
for (let i = 0; i < 100; i++) {
data['var' + i] = i;
}
// Write at once
storage.populate(data);
// or append to existing data
storage.append(data);
Kind: global class
Access: public
- Storage
- new Storage(namespace, storage)
- .namespace ⇒
string
- .type ⇒
string
- ._setData()
- ._getData()
- ._extend(obj, src)
- .get(key, defaultValue)
- .put(key, value)
- .pull(key, defaultValue) ⇒
string
- .has(key) ⇒
boolean
- .populate(data)
- .all() ⇒
Object
- .append(data)
- .forget(key)
- .flush()
Constructor
Param | Type | Description |
---|---|---|
namespace | string |
The namespace of storage |
storage | Object |
The storage type |
Get namespace
Kind: instance property of Storage
Returns: string
- - The namespace
Access: public
Get storage type
Kind: instance property of Storage
Returns: string
- - The storage type
Access: public
Save data to storage
Kind: instance method of Storage
Access: protected
Get data from storage
Kind: instance method of Storage
Access: protected
Merge two objects
Kind: instance method of Storage
Access: protected
Returns: Object
- Merged object
Param | Type | Description |
---|---|---|
obj | Object |
Destination object |
src | Object |
Source object |
Retrieve an item or return a default value
Kind: instance method of Storage
Access: public
Param | Type | Default | Description |
---|---|---|---|
key | string |
The data key | |
defaultValue | string |
null |
The default value |
string | null |
The data value |
Store an item
Kind: instance method of Storage
Access: public
Param | Type | Description |
---|---|---|
key | string |
The data key |
value | string |
The data value |
Retrieve an item and forget it
Kind: instance method of Storage
Returns: string
- - The data value
Access: public
Param | Type | Description |
---|---|---|
key | string |
The data key |
defaultValue | string |
The default value |
Whether or not an item exists
Kind: instance method of Storage
Returns: boolean
- - Whether or not item exists
Access: public
Param | Type | Description |
---|---|---|
key | string |
The data key |
Set all items
Kind: instance method of Storage
Access: public
Param | Type | Description |
---|---|---|
data | Object |
Data object |
Retrieve all items
Kind: instance method of Storage
Returns: Object
- - All data
Access: public
Append to current items
Kind: instance method of Storage
Acess: public
Param | Type | Description |
---|---|---|
data | Object |
Data to append |
Remove an item
Kind: instance method of Storage
Access: public
Param | Type | Description |
---|---|---|
key | string |
The data key |
Remove all items
Kind: instance method of Storage
Access: public
MIT - See included LICENSE.md