const ___TPOF___ = '___TP___'
const storage = window.sessionStorage

function setItem(k, v) {
  if (v === null || v === undefined) {
    removeItem(k)
    return
  }
  let cacheValue = typeof v + ___TPOF___
  switch (typeof v) {
    case 'object':cacheValue += JSON.stringify(v); break
    default:
      cacheValue += v
  }
  storage.setItem(k, cacheValue)
}
function getItem(k) {
  const cacheValue = storage.getItem(k)
  if (cacheValue) {
    const strings = cacheValue.split(___TPOF___)
    switch (strings[0]) {
      case 'object':return JSON.parse(strings[1])
      default:
        return strings[1]
    }
  }
  return null
}
function removeItem(k) {
  storage.removeItem(k)
}

export {
  setItem,
  getItem,
  removeItem
}
