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
531 B

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