
/**
 * queryString 转 object
 * @param {*} queryStr 
 * @returns 
 */
export function parseQueryString(queryStr) {
  if (!queryStr) {
    return;
  }

  const params = queryStr.split('&').reduce((acc, str) => {
    if (!str) return acc;

    const kv = str.split('=')
    if (kv.length !== 2) return acc;

    const key = decodeURIComponent(kv[0])
    const val = decodeURIComponent(kv[1])

    return {
      ...acc,
      [key]: val,
    }
  }, {});
  return params;
}

/**
 * object 转 queryStr
 * @param {*} params 
 * @returns 
 */
export function getQueryString(params) {
  if (!params) return ;

  const keys = Object.keys(params)
  const arr = keys.map((key) => {
    const val = params[key]
    return `${key}=${encodeURIComponent(val)}`
  })

  return arr.join('&')
}
