Skip to content

Instantly share code, notes, and snippets.

@danywalls
Last active December 24, 2018 08:47
Show Gist options
  • Save danywalls/1c6ad3303321d99b38b5ce5e033cdf80 to your computer and use it in GitHub Desktop.
Save danywalls/1c6ad3303321d99b38b5ce5e033cdf80 to your computer and use it in GitHub Desktop.
Helper to manage the local and session storage.
var EdgarJs = {
Session: {
load(key, value) {
if (localStorage.getItem(key)) {
return localStorage.getItem(key);
}
return "not existe";
},
save(key, value) {
if (!localStorage.getItem(key)) {
localStorage.setItem(key, value)
return;
}
return "ya existe";
},
saveJSON(key, value) {
if (!sessionStorage.getItem(key)) {
sessionStorage.setItem(key, JSON.stringify(value));
return true;
}
return false;
},
loadJSON(key) {
if (sessionStorage.getItem(key)) {
return JSON.parse(sessionStorage.getItem(key));
}
return "no existe este key"
},
remove(key) {
if (sessionStorage.getItem(key)) {
sessionStorage.removeItem(key)
return true;
}
return false;
},
removeAll() {
sessionStorage.clear();
}
},
Local: {
load(key, value) {
if (localStorage.getItem(key)) {
return localStorage.getItem(key);
}
return "no existe";
},
save(key, value) {
if (!localStorage.getItem(key)) {
localStorage.setItem(key, value)
return;
}
return "ya existe"
},
saveJSON(key, value) {
if (!localStorage.getItem(key)) {
localStorage.setItem(key, JSON.stringify(value));
return true;
}
return false;
},
loadJSON(key) {
if (localStorage.getItem(key)) {
return JSON.parse(localStorage.getItem(key));
}
return "no existe este key"
},
remove(key) {
if (localStorage.getItem(key)) {
localStorage.removeItem(key)
return true;
}
return false;
},
removeAll() {
localStorage.clear();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment