kafe.storage

Class

Version 1.2.0

Easily access, sort and manipulate local and session storage values.

Source: dist/storage.js

Methods

cleanPersistentNamespaceItems

Removes all expired local storage values for a specific namespace.

cleanPersistentNamespaceItems
  • namespace

Parameters:

  • namespace String

Example:

kafe.storage.cleanPersistentNamespaceItems('history');

cleanSessionNamespaceItems

Removes all expired session storage values for a specific namespace.

cleanSessionNamespaceItems
  • namespace

Parameters:

  • namespace String

Example:

kafe.storage.cleanSessionNamespaceItems('user');

getAllPersistentItems

Returns all local storage key values.

getAllPersistentItems (  ) Object

Returns:

Object

An object containing all local key/value combinations.

Example:

kafe.storage.setPersistentItem('history:last-visit', '2013-07-21');
					kafe.storage.setPersistentItem('website:show-ads', 'true');
					
					kafe.storage.getAllPersistentItems();
					// returns { "history:last-visit": "2013-07-21", "settings:show-ads": "true" }

getAllPersistentKeys

Returns an array of all local storage keys.

getAllPersistentKeys (  ) Array(String)

Returns:

Array(String)

A list of keys.

Example:

kafe.storage.setPersistentItem('history:last-visit', '2013-07-21');
					kafe.storage.setPersistentItem('website:show-ads', 'true');
					
					kafe.storage.getAllPersistentKeys();
					// returns ["history:last-visit", "website:show-ads"]

getAllSessionItems

Returns all session storage key values.

getAllSessionItems (  ) Object

Returns:

Object

An object containing all session key/value combinations.

Example:

kafe.storage.setSessionItem('user:first-name', 'John');
					kafe.storage.setSessionItem('preferences:tutorials', 'false');
					
					kafe.storage.getAllSessionItems();
					// returns { "preferences:tutorials": "false", "user:first-name": "John" }

getAllSessionKeys

Returns an array of all session storage keys.

getAllSessionKeys (  ) Array(String)

Returns:

Array(String)

A list of keys.

Example:

kafe.storage.setSessionItem('user:first-name', 'John');
					kafe.storage.setSessionItem('preferences:tutorials', 'false');
					
					kafe.storage.getAllSessionKeys();
					// returns ["user:first-name", "preferences:tutorials"]

getJSON

Get the JSON response of a webservice and keep it in the session storage with or without an expiration flag. Use this shorthand method to prevent unnecessary communication with the server on ajax heavy websites. All session keys used with this method are part of the kafestorage-getJSON namespace.

getJSON
  • url
  • options
Promise

Parameters:

  • url String

    URL address of the webservice.

  • [options] Object Optional

    Other parameters

    • [expires] String Optional

      Sets a cookie of the specified key as the expiration flag. Changes to the cookie's value will force a new call to the webservice on the next use.

    • [expires] Number Optional

      Sets a time based expiration flag in seconds. After that time period, the next use will call the webservice instead of using the session storage.

Returns:

Promise

The response (or stored value) is returned when promise is completed.

Example:

kafe.storage.getJSON('/UserServices/GetUserInfos?username=john_doe', { expires: 3600 });
					// Using this same line will use the session stored value instead of calling the service unless one hour has passed.

getPersistentItem

Returns the local value for a specific key.

getPersistentItem
  • key
String

Parameters:

  • key String

Returns:

String

If not expiration flag was trigged (cookie or datetime), returns the local storage value. Otherwise, returns undefined.

Example:

kafe.storage.getPersistentItem('history:last-visit');

getPersistentNamespaceItems

Returns all local storage key values for a specific namespace.

getPersistentNamespaceItems
  • namespace
Object

Parameters:

  • namespace String

Returns:

Object

An object containing all local key/value combinations for the namespace.

Example:

kafe.storage.setPersistentItem('history:last-visit', '2013-07-21');
					kafe.storage.setPersistentItem('history:last-page', '/about-us');
					
					kafe.storage.getPersistentNamespaceItems('history');
					// returns { "history:last-page": "/about-us", "history:last-visit": "2013-07-21" }

getPersistentNamespaceKeys

Returns an array of local storage keys for a specific namespace.

getPersistentNamespaceKeys
  • namespace
Array(String)

Parameters:

  • namespace String

Returns:

Array(String)

A list of keys.

Example:

kafe.storage.setPersistentItem('history:last-visit', '2013-07-21');
					kafe.storage.setPersistentItem('history:last-page', '/about-us');
					
					kafe.storage.getPersistentNamespaceKeys('history');
					// returns ["history:last-page", "history:last-visit"]

getSessionItem

Returns the session value for a specific key.

getSessionItem
  • key
String

Parameters:

  • key String

Returns:

String

If not expiration flag was trigged (cookie or datetime), returns the session storage value. Otherwise, returns undefined.

Example:

kafe.storage.getSessionItem('user:first-name');

getSessionNamespaceItems

Returns all session storage key values for a specific namespace.

getSessionNamespaceItems
  • namespace
Object

Parameters:

  • namespace String

Returns:

Object

An object containing all session key/value combinations for the namespace.

Example:

kafe.storage.setSessionItem('user:first-name', 'John');
					kafe.storage.setSessionItem('user:last-name', 'Doe');
					
					kafe.storage.getSessionNamespaceItems('user');
					// returns { "user:first-name": "John", "user:last-name": "Doe" }

getSessionNamespaceKeys

Returns an array of session storage keys for a specific namespace.

getSessionNamespaceKeys
  • namespace
Array(String)

Parameters:

  • namespace String

Returns:

Array(String)

A list of keys.

Example:

kafe.storage.setSessionItem('user:first-name', 'John');
					kafe.storage.setSessionItem('user:last-name', 'Doe');
					
					kafe.storage.getSessionNamespaceKeys('user');
					// returns ["user:first-name", "user:last-name"]

removeAllPersistent

Removes all local storage keys.

removeAllPersistent (  )

Example:

kafe.storage.removeAllPersistent();

removeAllSession

Removes all session storage keys.

removeAllSession (  )

Example:

kafe.storage.removeAllSession();

removePersistentItem

Removes the local storage value for a specific key.

removePersistentItem
  • key

Parameters:

  • key String

Example:

kafe.storage.removePersistentItem('history:last-visit');

removePersistentNamespace

Removes all local storage keys of a specific namespace.

removePersistentNamespace
  • namespace

Parameters:

  • namespace String

Example:

kafe.storage.removePersistentNamespace('history');

removeSessionItem

Removes the session storage value for a specific key.

removeSessionItem
  • key

Parameters:

  • key String

Example:

kafe.storage.removeSessionItem('user:first-name');

removeSessionNamespace

Removes all session storage keys of a specific namespace.

removeSessionNamespace
  • namespace

Parameters:

  • namespace String

Example:

kafe.storage.removeSessionNamespace('user');

setPersistentItem

Sets the local value for a specific key with or without an expiration flag. Namespacing can be defined by using the ':' character.

setPersistentItem
  • key
  • value
  • options

Parameters:

  • key String
  • value String
  • [options] Object Optional

    Expiration parameters

    • [expires] String Optional

      Sets a cookie of the specified key as the expiration flag. Changes to the cookie's value will flag the local storage value for the provided key as expired.

    • [expires] Number Optional

      Sets a time based expiration flag in seconds. After that time period, the local storage value for the provided key will be flagged as expired.

Example:

kafe.storage.setPersistentItem('history:last-visit', '2013-07-21', { expires: 3600 });
					// The local storage value will return undefined in one hour.
kafe.storage.setPersistentItem('history:last-visit', '2013-07-21', { expires: 'last-visit-cookie' });
					// The local storage value will return undefined if the value of the cookie 'last-visit-cookie' is changed.

setSessionItem

Sets the session value for a specific key with or without an expiration flag. Namespacing can be defined by using the ':' character.

setSessionItem
  • key
  • value
  • options

Parameters:

  • key String
  • value String
  • [options] Object Optional

    Expiration parameters

    • [expires] String Optional

      Sets a cookie of the specified key as the expiration flag. Changes to the cookie's value will flag the session storage value for the provided key as expired.

    • [expires] Number Optional

      Sets a time based expiration flag in seconds. After that time period, the session storage value for the provided key will be flagged as expired.

Example:

kafe.storage.setSessionItem('user:first-name', 'John', { expires: 3600 });
					// The session storage value will return undefined in one hour.
kafe.storage.setSessionItem('user:first-name', 'John', { expires: 'logged-user' });
					// The session storage value will return undefined if the value of the cookie 'logged-user' is changed.