-
Notifications
You must be signed in to change notification settings - Fork 1
StorageManagement
Yousif Al-Raheem edited this page Jan 20, 2020
·
1 revision
This utility helps to manage storage. To use it, construct a new StorageManagement with one of the StorageManagementType LOCAL | SESSION | COOKIE.
This utility consists of 7 methods:
To set desired item to storage
// to set item to storage
const storageManagement: StorageManagement= new StorageManagement();
storageManagement.setItem("key", "value")
console.log(storageManagement.getItem("key")); // "value"| Param | Type | Description |
|---|---|---|
| key | string | key of item to be set |
| value | any | value to be set to storage |
To get item with specific key from storage. If key is invalid or not found in storage, null will be returned.
// to get item from storage
const storageManagement: StorageManagement= new StorageManagement();
console.log(storageManagement.getItem("key")); // "value"| Param | Type | Description |
|---|---|---|
| key | string | key of item to be retrieved |
To remove item with specific key from storage. If key is invalid or not found in cookie, false will be returned.
// to remove item from storage
const storageManagement: StorageManagement= new StorageManagement();
console.log(storageManagement.removeItem("key")); // true| Param | Type | Description |
|---|---|---|
| key | string | key of item to be retrieved |
To clear storage.
// to clear storage
const storageManagement: StorageManagement= new StorageManagement("COOKIE");
storageManagement.clear();
console.log(document.cookie); // "key=; max-age=-1"To retrieve list of keys in storage.
// to retrieve list of keys from storage
const storageManagement: StorageManagement= new StorageManagement("SESSION");
console.log(storageManagement.keys()); // ["sessionKey1", "sessionKey2"]To retrieve key at certain index from storage. Method will return undefined if invalid index passed.
// to retrieve specific key from storage
const storageManagement: StorageManagement= new StorageManagement();
console.log(storageManagement.key(0)); // "key"| Param | Type | Description |
|---|---|---|
| index | number | index of key in storage |
To length of list of keys in storage.
// to retrieve list of keys from storage
const storageManagement: StorageManagement= new StorageManagement();
console.log(storageManagement.length); // 1