store.ts 694 字节
enum StoreType {
  local = 'local',
  session = 'session'
}
class Store {
  storage: Storage
  constructor(type: StoreType) {
    this.storage = type === StoreType.local ? localStorage : sessionStorage
  }
  set(key: string, value: any) {
    if (value) {
      this.storage.setItem(key, JSON.stringify(value))
    }
  }

  get(key: string) {
    const value = this.storage.getItem(key)
    if (value) {
      // return JSON.parse(value) || value
      return value
    }
  }

  del(key: string) {
    this.storage.removeItem(key)
  }

  clear() {
    this.storage.clear()
  }
}

const LStore = new Store(StoreType.local)
const SStore = new Store(StoreType.session)

export { LStore, SStore }