Skip to content

Instantly share code, notes, and snippets.

@stefano-bortolotti
Last active August 29, 2015 13:57
Show Gist options
  • Save stefano-bortolotti/9618506 to your computer and use it in GitHub Desktop.
Save stefano-bortolotti/9618506 to your computer and use it in GitHub Desktop.
Wrap local storage api
if(!window['UTIL']) window['UTIL'] = {};
UTIL.LocalStorage = function() {
var context = {};
return $.extend(context, {
get : function(key) {
try {
return JSON.parse(localStorage.getItem(key))
}
catch(e) {
return localStorage.getItem(key)
}
},
set : function(key, value) { // key[String], value[Object or String]
//console.log('val is type: ' + typeof value);
if (typeof value === 'function')
throw new TypeError('Functions can not be saved');
localStorage[key] = typeof value === 'object' ? JSON.stringify(value) : value;
},
remove : function(key) {
localStorage.removeItem(key);
}
});
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment