web-storage-es6

1.2.0 • Public • Published

Web Storage ES6 Build Status Coveralls Status

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.

Installation

npm install --save web-storage-es6

Usage Summary

Overview

  • 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

Code

// 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');

Don'ts

// 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);
}

Dos

// 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);

API

Storage

Kind: global class
Access: public

new Storage(namespace, storage)

Constructor

Param Type Description
namespace string The namespace of storage
storage Object The storage type

storage.namespace ⇒ string

Get namespace

Kind: instance property of Storage
Returns: string - - The namespace
Access: public

storage.type ⇒ string

Get storage type

Kind: instance property of Storage
Returns: string - - The storage type
Access: public

storage._setData()

Save data to storage

Kind: instance method of Storage
Access: protected

storage._getData()

Get data from storage

Kind: instance method of Storage
Access: protected

storage._extend(obj, src)

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

storage.get(key, defaultValue)

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

storage.put(key, value)

Store an item

Kind: instance method of Storage
Access: public

Param Type Description
key string The data key
value string The data value

storage.pull(key, defaultValue) ⇒ string

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

storage.has(key) ⇒ boolean

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

storage.populate(data)

Set all items

Kind: instance method of Storage
Access: public

Param Type Description
data Object Data object

storage.all() ⇒ Object

Retrieve all items

Kind: instance method of Storage
Returns: Object - - All data
Access: public

storage.append(data)

Append to current items

Kind: instance method of Storage
Acess: public

Param Type Description
data Object Data to append

storage.forget(key)

Remove an item

Kind: instance method of Storage
Access: public

Param Type Description
key string The data key

storage.flush()

Remove all items

Kind: instance method of Storage
Access: public

License

MIT - See included LICENSE.md

Package Sidebar

Install

npm i web-storage-es6

Weekly Downloads

1

Version

1.2.0

License

MIT

Unpacked Size

1.08 MB

Total Files

56

Last publish

Collaborators

  • alanzhaonys