All files / src/core url.ts

100% Statements 20/20
100% Branches 8/8
100% Functions 5/5
100% Lines 17/17

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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56            10x 5x 5x 15x 11x     5x                 10x 4x 3x   9x 7x                                   10x 4x 4x   4x   5x   4x    
/**
 *
 * @ignore
 * @return {Array} 过滤出url的非空值
 *
 */
export function cleanArray(actual: any): Array<any> {
  const newArray: Array<any> = []
  for (let i = 0; i < actual.length; i++) {
    if (actual[i]) {
      newArray.push(actual[i])
    }
  }
  return newArray
}
 
/**
 *
 * @ignore
 * @return {string} 对象转成url的param
 *
 */
export function param(json: Record<string, any>): string {
  if (JSON.stringify(json) === '{}') return ''
  return cleanArray(
    Object.keys(json).map(key => {
      if (json[key] === undefined) return ''
      return encodeURIComponent(key) + '=' + encodeURIComponent(json[key])
    })
  ).join('&')
}
 
/**
 *
 * @ignore
 * @return {object}
 *
 */
 
/**
 *
 * @ignore
 * @return {object} 解析url中的参数
 *
 */
export function param2Obj(search: string): Record<string, any> {
  const str: string = search || window.location.search
  const objURL: object = {}
 
  str.replace(new RegExp('([^?=&]+)(=([^&]*))?', 'g'), ($0, $1, $2, $3): any => {
    // 明明没有返回为啥不能定义成void
    objURL[$1] = $3
  })
  return objURL
}