Skip to content

Instantly share code, notes, and snippets.

@faisalmuhammad
Last active October 15, 2020 13:12
Show Gist options
  • Save faisalmuhammad/674c902c1f094077c44edbe0095b6e64 to your computer and use it in GitHub Desktop.
Save faisalmuhammad/674c902c1f094077c44edbe0095b6e64 to your computer and use it in GitHub Desktop.
Generic Local Storage Class
/**
* Customized HTML5 local storage class with additional functionality.
*/
export class LocalStorage {
/**
* Checks if the browser supports local storage.
* @returns true if the local storage is supported; false otherwise.
*/
public static isSupported(): boolean {
try {
// Try to use basic methods of localStorage.
localStorage.setItem('__localstorage_support_test__', '');
localStorage.getItem('__localstorage_support_test__');
localStorage.removeItem('__localstorage_support_test__');
} catch (e) {
// If exception is thrown, then there is problem in local storage support.
console.warn('LocalStorage Not supported for this browser.');
return false;
}
// local storage is suuported.
return true;
};
/**
* Sets an encoded string item in the local storage.
* @param key Key of the item to be stored.
* @param value Value of the item to be stored.
*/
public static setItem(key: string, value: string): void {
localStorage.setItem(key, value);
};
/**
* Sets an encoded object item in the local storage.
* @param key Key of the item to be stored.
* @param value Value of the item to be stored.
*/
public static setItemObject<T>(key: string, value: T): void {
const json = JSON.stringify(value);
this.setItem(key, json);
};
/**
* Gets the decoded string item from the local storage.
* @param key Key of the item to be retrieved.
* @returns String value against the given key.
*/
public static getItem(key: string): string {
// Check if the value for the key exists in the storage.
if (localStorage.getItem(key) === undefined || localStorage.getItem(key) === null) {
return null;
}
// Get and return the value.
const value = localStorage.getItem(key);
return value;
};
/**
* Gets the the decoded object item from the local storage.
* @param key Key of the item to be retrieved.
* @returns Object of type T against the given key.
*/
public static getItemObject<T>(key: string): T {
// Check if the value for given key is null.
if (this.getItem(key) === null) {
return null;
}
// Parse and return the JSON object.
const value = this.getItem(key);
return JSON.parse(value) as T;
};
public static removeItem(key: string): void {
localStorage.removeItem(key);
}
}; // End of class: LocalStorage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment