Skip to content

Instantly share code, notes, and snippets.

@charliecalvert
Last active May 25, 2019 15:53
Show Gist options
  • Save charliecalvert/d8404b826ee22702c501368335624622 to your computer and use it in GitHub Desktop.
Save charliecalvert/d8404b826ee22702c501368335624622 to your computer and use it in GitHub Desktop.
Handle Local Storage
/**
* Created by Charlie on 5/8/17.
*
* Use it like this:
* import { getByIndex } from '../assets/elf-local-storage';
*/
const ELF_TAG = 'elf';
const padNumber = function(numberToPad, width, padValue) {
padValue = padValue || '0';
numberToPad += '';
if (numberToPad.length >= width) {
return numberToPad;
} else {
return new Array(width - numberToPad.length + 1).join(padValue) + numberToPad;
}
};
function saveByIndex(item, index) {
if (typeof item === 'object') {
item = JSON.stringify(item, null, 4);
}
const key = ELF_TAG + padNumber(index, 4, 0);
localStorage.setItem(key, item);
}
function getByIndex(index) {
const key = ELF_TAG + padNumber(index, 4, 0);
return JSON.parse(localStorage.getItem(key));
}
function removeElfKeys() {
for (var key in localStorage) {
if (key.startsWith(ELF_TAG)) {
localStorage.removeItem(key);
}
}
}
function clearLocalStorage() {
localStorage.clear();
}
function getCount () {
return localStorage.getItem('elven-count');
}
export {
saveByIndex,
getByIndex,
removeElfKeys,
clearLocalStorage,
getCount
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment