Skip to content

Instantly share code, notes, and snippets.

@brito
Created March 22, 2017 21:29
Show Gist options
  • Save brito/bf77ccff19b8f24932433d635fd88241 to your computer and use it in GitHub Desktop.
Save brito/bf77ccff19b8f24932433d635fd88241 to your computer and use it in GitHub Desktop.
Use and test localStorage
N = 'Use and test localStorage'
if ('test')
console.clear(),
localStorage.length ?
GET(N).ok ||
console.warn(N, 'not empty'):
SET(N, { ok: true });
_LIST();
if ('mop')
console.assert(GET(N).ok),
localStorage.removeItem(N),
console.assert(!GET(N)),
console.info('Tests ok');
function SET(key, value){
return localStorage.setItem(key, JSON.stringify(value) ), GET(key) }
function GET(key, reviver){
return JSON.parse(localStorage.getItem(key), reviver) }
function _LIST(){
Object.keys(localStorage)
.forEach(key => console.log(key, GET(key))) }
//
new Date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment