34 lines
690 B
TypeScript
34 lines
690 B
TypeScript
const NameSpace = 'ihealth-kit-registration_';
|
|
|
|
function getData<T>(key: string, defaultValue?: T): T | null {
|
|
const rawData = window.localStorage.getItem(NameSpace + key);
|
|
try {
|
|
if (rawData) {
|
|
return JSON.parse(rawData);
|
|
} else {
|
|
return defaultValue || null
|
|
}
|
|
} catch (e) {
|
|
return defaultValue || null;
|
|
}
|
|
}
|
|
|
|
function setData<T>(key: string, data: T): void {
|
|
window.localStorage.setItem(NameSpace + key, JSON.stringify(data));
|
|
}
|
|
|
|
function clearData(key?: string): void {
|
|
if (key) {
|
|
window.localStorage.removeItem(NameSpace + key);
|
|
} else {
|
|
window.localStorage.clear()
|
|
}
|
|
}
|
|
|
|
const storage = {
|
|
get: getData,
|
|
set: setData,
|
|
clear: clearData
|
|
}
|
|
export default storage
|