Local Storage Module
/**
* @author DaveDevor <davedevor@gmail.com>
*/
/**
* Shows if local storage is supported.
* @type {Boolean}
*/
const hasStorage = checkLocalStorage();
/**
* Check to see if local storage is supported
*
* @returns {Boolean}
*/
function checkLocalStorage() {
try {
window.localStorage.setItem('foo', 'bar');
window.localStorage.removeItem('foo');
return true;
}
catch (e) {
return false;
}
}
/**
* Object to set and get data from local Storage;
* @type {Obj}
*/
const localStorage = {
/**
* Retrieve data from local Storage
* @param {String} key
*
* @returns {String | Number | Object | Array} value
*/
get: (key) => {
if (hasStorage && window.localStorage.getItem(key)) {
return JSON.parse(window.localStorage.getItem(key));
}
return false;
},
/**
* Store data in local storage
* @param {Str} key
* @param {Str | Num | Obj | Arr} val
*/
set: (key, val) => {
if (hasStorage && typeof val !== 'undefined') {
window.localStorage.setItem(key, JSON.stringify(val) );
}
},
/**
* Remove data from local storage
* @param {String} key
*
* @returns {Boolean} success
*/
del: (key) => {
if (hasStorage && window.localStorage.getItem(key)) {
window.localStorage.removeItem(key);
return true;
}
return false;
}
};
export {localStorage as default};