added local storage utils

master
Tudor Stanciu 2022-05-04 02:37:24 +03:00
parent 1414009f40
commit 1c7334e108
1 changed files with 36 additions and 0 deletions

View File

@ -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 };