// 字符串转params对象
export const strToParams = (str: string) => {
  if (str[0] === '?') {
    str = str.substr(1)
  }

  const result: { [key: string]: string } = {}
  const splitRG = /([^=&]+)(?:=([^&]*))?&?/

  let rgRet: any
  while ((rgRet = str.match(splitRG))) {
    result[rgRet[1]] = rgRet[2] === undefined ? true : rgRet[2]
    str = str.substr(rgRet[0].length)
  }

  return result
}

// 对象转params
export const paramsToStr = (params: { [key: string]: string | boolean }) =>
  '?' +
  Object.keys(params)
    .filter(key => params[key] !== undefined)
    .map(key => `${key}${params[key] == false ? '' : `=${params[key]}`}`)
    .join('&')
