Skip to content

Instantly share code, notes, and snippets.

@mb8z
Created October 3, 2018 18:10
Show Gist options
  • Save mb8z/86d2c136a04c010811d295fb2fb2deab to your computer and use it in GitHub Desktop.
Save mb8z/86d2c136a04c010811d295fb2fb2deab to your computer and use it in GitHub Desktop.
import _ from 'lodash';
export default {
getItem: (key, defaultValue) => {
if (typeof localStorage === 'undefined') return null;
const value = localStorage.getItem(key);
return _.isNil(value) ? defaultValue : JSON.parse(value);
},
setItem: (key, data) => {
if (typeof localStorage === 'undefined') return null;
return localStorage.setItem(key, JSON.stringify(data));
},
removeItem: (key) => {
if (typeof localStorage === 'undefined') return null;
return localStorage.removeItem(key);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment