export function getLocal (name) {
  if (window.localStorage) {
    return JSON.parse(localStorage.getItem(name))
  }
}

export function setLocal (name, val) {
  if (window.localStorage) {
    localStorage.setItem(name, JSON.stringify(val))
  }
}

export function clearLocal (name) {
  if (window.localStorage) {
    localStorage.removeItem(name)
  }
}
export function addTop (id) {
  if (window.localStorage) {
    let arr = getLocal('topIds')
    if (!arr) arr = []
    if (arr.indexOf(id) !== -1) {
      arr.splice(arr.indexOf(id), 1)
    }
    arr.unshift(id)
    setLocal('topIds', arr)
  }
}
export function cancelTop (id) {
  if (window.localStorage) {
    let arr = getLocal('topIds')
    if (!arr) arr = []
    if (arr.indexOf(id) !== -1) {
      arr.splice(arr.indexOf(id), 1)
    }
    setLocal('topIds', arr)
  }
}
