Skip to content

工具函数

常用工具函数

ts
export const isDebug = wx.canIUse('getAppBaseInfo') ? wx.getAppBaseInfo().enableDebug : false

export const isDev = wx.getAccountInfoSync().miniProgram.envVersion !== 'release'

export function json2params(json: Record<string, any>) {
  const params = Object.keys(json)
    .map((key) => {
      let data = json[key]
      if (typeof data === 'object')
        data = JSON.stringify(data)

      return `${key}=${data}`
    })
  return `?${params.join('&')}`
}

export function bytesToSize(size: number) {
  let index = +(size >= 1024)
  let value = size
  const unit = ['B', 'KB', 'MB', 'GB']

  // eslint-disable-next-line no-cond-assign
  while ((value /= 1024) > 1024)
    index++

  return value.toFixed(2) + unit[index]
}
export const isDebug = wx.canIUse('getAppBaseInfo') ? wx.getAppBaseInfo().enableDebug : false

export const isDev = wx.getAccountInfoSync().miniProgram.envVersion !== 'release'

export function json2params(json: Record<string, any>) {
  const params = Object.keys(json)
    .map((key) => {
      let data = json[key]
      if (typeof data === 'object')
        data = JSON.stringify(data)

      return `${key}=${data}`
    })
  return `?${params.join('&')}`
}

export function bytesToSize(size: number) {
  let index = +(size >= 1024)
  let value = size
  const unit = ['B', 'KB', 'MB', 'GB']

  // eslint-disable-next-line no-cond-assign
  while ((value /= 1024) > 1024)
    index++

  return value.toFixed(2) + unit[index]
}