Save and load notes in Zeichen using the browser's local storage.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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