mirror of
https://github.com/sheptikhinv/timers.git
synced 2026-02-07 07:41:36 +05:00
add IStorageManager and it's implementation via localStorage in LocalStorageManager
This commit is contained in:
parent
8f69ed3b58
commit
8e8e07e2f5
2 changed files with 27 additions and 0 deletions
7
src/helpers/IStorageManager.ts
Normal file
7
src/helpers/IStorageManager.ts
Normal 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;
|
||||||
20
src/helpers/LocalStorageManager.ts
Normal file
20
src/helpers/LocalStorageManager.ts
Normal 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;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue