Skip to content

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:

Method: setItem

To set desired item to storage

Example

// to set item to storage
const storageManagement: StorageManagement= new StorageManagement();
storageManagement.setItem("key", "value")
console.log(storageManagement.getItem("key")); // "value"

Properties

Param Type Description
key string key of item to be set
value any value to be set to storage

Method: getItem

To get item with specific key from storage. If key is invalid or not found in storage, null will be returned.

Example

// to get item from storage
const storageManagement: StorageManagement= new StorageManagement();
console.log(storageManagement.getItem("key")); // "value"

Properties

Param Type Description
key string key of item to be retrieved

Method: removeItem

To remove item with specific key from storage. If key is invalid or not found in cookie, false will be returned.

Example

// to remove item from storage
const storageManagement: StorageManagement= new StorageManagement();
console.log(storageManagement.removeItem("key")); // true

Properties

Param Type Description
key string key of item to be retrieved

Method: clear

To clear storage.

Example

// to clear storage
const storageManagement: StorageManagement= new StorageManagement("COOKIE");
storageManagement.clear();
console.log(document.cookie); // "key=; max-age=-1"

Method: keys

To retrieve list of keys in storage.

Example

// to retrieve list of keys from storage
const storageManagement: StorageManagement= new StorageManagement("SESSION");
console.log(storageManagement.keys()); // ["sessionKey1", "sessionKey2"]

Method: key

To retrieve key at certain index from storage. Method will return undefined if invalid index passed.

Example

// to retrieve specific key from storage
const storageManagement: StorageManagement= new StorageManagement();
console.log(storageManagement.key(0)); // "key"

Properties

Param Type Description
index number index of key in storage

Getter Method: length

To length of list of keys in storage.

Example

// to retrieve list of keys from storage
const storageManagement: StorageManagement= new StorageManagement();
console.log(storageManagement.length); // 1
Clone this wiki locally