Last active
August 29, 2015 13:57
-
-
Save stefano-bortolotti/9618506 to your computer and use it in GitHub Desktop.
Wrap local storage api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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