Created
December 2, 2015 17:58
-
-
Save Purexo/51903271006a2b552c62 to your computer and use it in GitHub Desktop.
Binding localStorage API to extend the key -> value functionnality with JSON api
This file contains hidden or 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
/** | |
* @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