All files / getUrlParam getUrlParam.ts

100% Statements 5/5
60% Branches 3/5
100% Functions 1/1
100% Lines 5/5

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                      1x 1x 1x 1x 1x    
/**
 * @description 获取url参数
 * @function getUrlParam
 * @returns {string} url参数name对应的值
 * @param {string} name - url参数
 * @param {string} [url=window.location.href] 可选,url
 * @author liukun <919590347@qq.com>
 * @example
 * import { getUrlParam } from '@wont/utils'
 * getUrlParam('id', 'http://localhost:8088/#/index?type=hash&id=8080&index=0')  // returns '8080'
 */
export function getUrlParam(name: string, url?: string): string {
  const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`)
  const href = url || window.location.href
  const result = href.match(reg)
  return (result && decodeURI(result[2])) || ''
}