Monorepo containing core modules of Zeichen.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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