// 可以在不同的地方共享数据、传参
const get = <T>(key: string, defaultValue?: T): T => {
  const val = localStorage.getItem(key)
  return (val === undefined ? defaultValue : val) as T
}

// 设值
const set = (key: string, value: any) => {
  localStorage.setItem(key, value)
}

// 弹出值
const pop = <T>(key: string, defaultValue?: T): T => {
  const value = get(key, defaultValue)
  localStorage.removeItem(key)
  return value
}

// 清空
const clear = () => {
  localStorage.clear()
}

export default {
  get,
  set,
  pop,
  clear,
}
