37 lines
723 B
JavaScript
37 lines
723 B
JavaScript
import { isArray, isObject, isJson } from "./dataType";
|
|
|
|
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 { setItem, getItem, removeItem, clear, key };
|