Monorepo containing core modules of Zeichen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
640 B

  1. export default class LocalStorage<T> {
  2. constructor(
  3. private readonly source: Storage,
  4. private readonly serializer: (t: T) => string = JSON.stringify,
  5. private readonly deserializer: (s: string) => T = JSON.parse,
  6. ) {}
  7. getCollection(collectionId: string) {
  8. const raw = this.source.getItem(collectionId)
  9. if (raw !== null) {
  10. return this.deserializer(raw)
  11. }
  12. return null
  13. }
  14. replaceCollection(collectionId: string, collectionData: T) {
  15. this.source.setItem(collectionId, this.serializer(collectionData))
  16. }
  17. removeCollection(collectionId: string) {
  18. this.source.removeItem(collectionId)
  19. }
  20. }