|
1234567891011121314151617181920212223 |
- export default class LocalStorage<T> {
- constructor(
- private readonly source: Storage,
- private readonly serializer: (t: T) => string = JSON.stringify,
- private readonly deserializer: (s: string) => T = JSON.parse,
- ) {}
-
- getCollection(collectionId: string) {
- const raw = this.source.getItem(collectionId)
- if (raw !== null) {
- return this.deserializer(raw)
- }
- return null
- }
-
- replaceCollection(collectionId: string, collectionData: T) {
- this.source.setItem(collectionId, this.serializer(collectionData))
- }
-
- removeCollection(collectionId: string) {
- this.source.removeItem(collectionId)
- }
- }
|