UNPKG

@dailyshot/hooks

Version:

A set of hooks used in Dailyshot packages

42 lines (40 loc) 1.26 kB
const useStorage = (defaultType) => { const getStorageType = (type) => `${!type ? "session" : type}Storage`; const isBrowser = (() => typeof window !== "undefined")(); const isEmptyObject = (value) => Object.keys(value).length === 0; const getItem = (key, type) => { const storageType = !defaultType ? type : defaultType; return isBrowser ? window[getStorageType(storageType)][key] : ""; }; const setItem = (key, value, type) => { const storageType = !defaultType ? type : defaultType; if (isBrowser) { window[getStorageType(storageType)].setItem(key, value); return true; } return false; }; const removeItem = (key, type) => { const storageType = !defaultType ? type : defaultType; window[getStorageType(storageType)].removeItem(key); }; const parseFromString = (valueString) => { if (!valueString) return null; return JSON.parse(valueString); }; const parseToString = (value) => { if (value === void 0 || typeof value === "object" && isEmptyObject(value)) return ""; return JSON.stringify(value); }; return { getItem, setItem, removeItem, parseFromString, parseToString }; }; export { useStorage }; //# sourceMappingURL=use-storage.js.map