Last active
July 6, 2018 03:00
-
-
Save daveols/666eec165f5b2acd6f049d8bd44dfd5a to your computer and use it in GitHub Desktop.
React Loads - local storage cache provider
This file contains 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
const Global = typeof window !== 'undefined' ? window : global | |
const serialize = data => JSON.stringify(data) | |
const deserialize = stringValue => { | |
if (!stringValue) return | |
let value = '' | |
try { | |
value = JSON.parse(stringValue) | |
} catch (e) { | |
value = stringValue | |
} | |
return value | |
} | |
const get = key => { | |
const data = Global.localStorage.getItem(`bacon-app.${key}`) | |
return deserialize(data) | |
} | |
const set = (key, data) => { | |
if (!data && data === undefined) { | |
Global.localStorage.removeItem(`bacon-app.${key}`) | |
} else { | |
Global.localStorage.setItem(`bacon-app.${key}`, serialize(data)) | |
} | |
return data | |
} | |
export default { | |
get, | |
set, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment