All files / getUrlParams getUrlParams.ts

100% Statements 6/6
0% Branches 0/1
100% Functions 2/2
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22                      1x     1x 1x 2x 2x   1x    
/**
 * @description 获取url参数,解析为一个对象
 * @function getUrlParams
 * @param {string} [url=window.location.href] - 合法的url
 * @returns {Object} 解析url的参数,结果为对象
 * @author liukun <919590347@qq.com>
 * @example
 * import { getUrlParams } from '@wont/utils'
 * getUrlParams('https://wont-org.github.io?a=1&b=2')  // returns {a:'1',b:'2'}
 */
 
export function getUrlParams(
  url: string = window.location.href,
): Record<string, string> {
  const result = {}
  url.replace(/([^?&=]+)=([^&]+)/g, (_, k, v) => {
    result[k] = v
    return v
  })
  return result
}