add IStorageManager and it's implementation via localStorage in LocalStorageManager

This commit is contained in:
Вячеслав 2025-06-14 18:35:24 +05:00
parent 8f69ed3b58
commit 8e8e07e2f5
2 changed files with 27 additions and 0 deletions

View file

@ -0,0 +1,7 @@
interface IStorageManager<T> {
getItem(key: string): T | null;
setItem(key: string, value: T): void;
removeItem(key: string): void;
}
export default IStorageManager;

View file

@ -0,0 +1,20 @@
import IStorageManager from "./IStorageManager.ts";
class LocalStorageManager<T> implements IStorageManager<T> {
setItem = (key: string, value: T): void => {
localStorage.setItem(key, JSON.stringify(value));
}
getItem = (key: string): T | null => {
const value = localStorage.getItem(key);
if (value === null)
return value;
return JSON.parse(value);
}
removeItem = (key: string): void => {
localStorage.removeItem(key);
}
}
export default LocalStorageManager;