RGJS6 Storage module.
Methods
-
<static> clear( [opts])
-
Clear the storage.
Parameters:
Name Type Argument Description optsobject <optional>
Options are optional.
Properties
Name Type Argument Description storageTypestring <optional>
The type of storage (
localStorageorsessionStorage). 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 keystring The key for the value.
fallbackstring <optional>
null Fallback value, defaults to null.
optsobject <optional>
Options are optional.
Properties
Name Type Argument Description storageTypestring <optional>
The type of storage (
localStorageorsessionStorage). 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 keystring The key to test.
optsobject <optional>
Options are optional.
Properties
Name Type Argument Description storageTypestring <optional>
The type of storage (
localStorageorsessionStorage). 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 storageTypestring <optional>
The type of storage to check (
localStorageorsessionStorage). Default:localStorage.Returns:
True or false.
- Type
- boolean
Example
rgjs.storage.isSupported()
-
<static> store(key, val [, opts])
-
Store a value.
If the value evaluates asfalsey, thekeywill be unset.
If the value is an array or object, it is automatically piped throughJSON.stringify().Parameters:
Name Type Argument Description keystring The key for the value.
valstring | * The value to be stored.
optsobject <optional>
Options are optional.
Properties
Name Type Argument Description storageTypestring <optional>
The type of storage (
localStorageorsessionStorage). 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}'