
export function isFunction(fn) {
  return typeof fn === 'function'
}

export function setStorage(k, v) {
  if (typeof v !== 'object') return

  window.localStorage.setItem(k, window.JSON.stringify(v))
}

export function getStorage(k) {
  const str = window.localStorage.getItem(k);
  if (!str) return;

  return window.JSON.parse(str);
}

export function getStorageBuilder(key, initData) {
  if (initData) {
    setStorage(key, initData)  
  }

  return {
    set: (k, v) => {
      const data = getStorage(key) || {}

      setStorage(key, { ...data, [`${k}`]: v })
    },

    get: (k) => {
      const data = getStorage(key) || {}
      return data[k]
    }
  }
}
