Save and load notes in Zeichen using the browser's local storage.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

24 linhas
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. }