Module: rgjs/storage

RGJS6 Storage module.

Methods


<static> clear( [opts])

Clear the storage.

Parameters:
Name Type Argument Description
opts object <optional>

Options are optional.

Properties
Name Type Argument Description
storageType string <optional>

The type of storage (localStorage or sessionStorage). Default: localStorage.

Returns:

False if local storage is not supported.

Type
boolean
Example
rgjs.storage.store('data_key', 'value');
rgjs.storage.clear();

<static> get(key [, fallback] [, opts])

Get a value.

Parameters:
Name Type Argument Default Description
key string

The key for the value.

fallback string <optional>
null

Fallback value, defaults to null.

opts object <optional>

Options are optional.

Properties
Name Type Argument Description
storageType string <optional>

The type of storage (localStorage or sessionStorage). Default: localStorage.

Returns:

The value.

Type
string
Example
rgjs.storage.get('data_key'); // Returns the value or null.
rgjs.storage.get('data_key', {}); // Returns `{}` if the value is undefined or null.

<static> isSet(key [, opts])

Check if the key has been set.

Parameters:
Name Type Argument Description
key string

The key to test.

opts object <optional>

Options are optional.

Properties
Name Type Argument Description
storageType string <optional>

The type of storage (localStorage or sessionStorage). Default: localStorage.

Returns:

True if the value has been set, false if not.

Type
boolean
Example
rgjs.storage.store('data_key', 'value');
rgjs.storage.isSet('data_key'); // true
rgjs.storage.store('data_key', null);
rgjs.storage.isSet('data_key'); // false

<static> isSupported( [storageType])

Check support for Web Storage.

Parameters:
Name Type Argument Description
storageType string <optional>

The type of storage to check (localStorage or sessionStorage). Default: localStorage.

Returns:

True or false.

Type
boolean
Example
rgjs.storage.isSupported()

<static> store(key, val [, opts])

Store a value.
If the value evaluates as falsey, the key will be unset.
If the value is an array or object, it is automatically piped through JSON.stringify().

Parameters:
Name Type Argument Description
key string

The key for the value.

val string | *

The value to be stored.

opts object <optional>

Options are optional.

Properties
Name Type Argument Description
storageType string <optional>

The type of storage (localStorage or sessionStorage). Default: localStorage.

Returns:

Returns false if something went wrong.

Type
boolean
Example
rgjs.storage.store('data_key', 'value'); // Sets 'data_key' to 'value'
rgjs.storage.store('data_key', false); // Removes 'data_key'
rgjs.storage.store('data_key', {x: 1, y: 2, z: 3}); // Sets 'data_key' to stringified '{"x":1,"y":2,"z":3}'