Skip to content

Instantly share code, notes, and snippets.

@Purexo
Created December 2, 2015 17:58
Show Gist options
  • Save Purexo/51903271006a2b552c62 to your computer and use it in GitHub Desktop.
Save Purexo/51903271006a2b552c62 to your computer and use it in GitHub Desktop.
Binding localStorage API to extend the key -> value functionnality with JSON api
/**
* @author : Purexo
* @description : Binding localStorage API to extend the key -> value functionnality with JSON api
* when setItem, data is stringify
* when getItem, data is parsed
* @usage :
* ls.setItem('key', data)
* ls.getItem('key', data)
* ls.removeItem('key')
* ls.key('index')
* ls.clear()
*/
let ls;
(function() {
function LS () { }
LS.prototype.setItem = (key, data) => {
return localStorage.setItem(key, JSON.stringify(data));
}
LS.prototype.getItem = (key) => {
return JSON.parse(localStorage.getItem(key));
}
LS.prototype.removeItem = (key) => {
return localStorage.removeItem(key);
}
LS.prototype.key = (index) => {
return localStorage.key(index);
}
LS.prototype.clear = () => {
return localStorage.clear();
}
ls = new LS();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment