diff --git a/src/storage/localStorage.js b/src/storage/localStorage.js new file mode 100644 index 0000000..b75a66d --- /dev/null +++ b/src/storage/localStorage.js @@ -0,0 +1,36 @@ +import { isArray, isObject, isJson } from "../data/typeValidator"; + +const setItem = (key, value) => { + let valueToStore = value; + if (isArray(value) || isObject(value)) { + valueToStore = JSON.stringify(value); + } + + window.localStorage.setItem(key, valueToStore); +}; + +const getItem = key => { + var value = window.localStorage.getItem(key); + var { data, success } = isJson(value); + + if (success) { + return data; + } else { + return value; + } +}; + +const removeItem = key => { + window.localStorage.removeItem(key); +}; + +const clear = () => { + window.localStorage.clear(); +}; + +const key = index => { + var keyName = window.localStorage.key(index); + return keyName; +}; + +export default { setItem, getItem, removeItem, clear, key };