webstoragejs
A simple, JavaScript API for handling localStorage/sessionStorage with automatic JSON serialization and namespace supported.
Installation
- Install the latest version of webstoragejs:
npm install --save webstoragejs
- At this point you can import
webstoragejs
and its styles in your application as follows:
;
Usage
The API is identical to the standard Web Storage API. The only difference is that the items we put/get are automatically serialized/unserialized with JSON.stringify() and JSON.parse().
API
Constructer
Build a new webStorage object for operator under specific namespace and storage.
;const storage = ;/** * @ options.namespace * Type: string * Required: false * Default: 'default' * Description: The namespace for set/get item into/from target storage. */ /** * @ options.sessionStorage * Type: bool * Required: false * Default: false * Description: Apply the target storage (One of [window.localStorage, window.sessionStorage]) for set/get item. ** false: Will take window.localStorage ** true: Will take window.sessionStorage ** If target storage not avlible, then in-memory object will be used. For example sofari do not support localStorage/sessionStorage in private browsing mode. */
setItem
Set item into storage under specific namespace.
const storage = ;storage;/** * @ key * Type: string * Required: true * Description: The key for set item into target storage. */ /** * @ value * Type: Any valid type. Such as number, string, object, array * Required: true * Description: The value ned to set into target storage. */
getItem
Get item from storage under specific namespace.
const storage = ;storage;/** * @ key * Type: string * Required: true * Description: The key for get item from target storage. */ /** * @ defaultValue * Type: Any valid type. Such as number, string, object, array * Required: false * Default: undefined * Description: The default value returned when try to get value failed. */
removeItem
Remove item from storage under specific namespace.
const storage = ;storage;/** * @ key * Type: string * Required: true * Description: The key for remove item from target storage. */
size
Get count of items from storage under specific namespace.
const storage = ;const otherStorage = ; const size = storagesize;const otherSize = otherStoragesize;console // => 0console // => 0 storage;storage; const newSize = storagesize;const newOtherSize = otherSizesize;console; // => 2console; // => 0
clear
Clears all stored keys from storage under specific namespace.
const storage = ;const otherStorage = ; storage;storage; otherStorage;otherStorage; console; // => 2console; // => 2 // Clears all stored keys from storagestorageclear; console; // => 0console; // => 2
Example
Basic
;const storage = ; // Set numberstorage;const num = storage;console; // => numberconsole; // => 123 // set stringstorage;const str = storage;console; // => stringconsole; // => example // Set Objectstorage;const obj = storage;console; // => objectconsole; // => example
Set value
;const storage = ;storage;
Get value
;const storage = ;const obj = storage; // Try to get value, return defalut value if cannot get value correctlyconst value = storage;
Apply customize namespace
;const storage = ;storage;
Apply sessionStorage
;const storage = ;storage;