All files / src/core format.ts

100% Statements 50/50
100% Branches 30/30
100% Functions 8/8
100% Lines 44/44

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 14410x             10x 100x 100x 99x   1x   100x                 10x 1x                 35x 18x                 10x 2x 2x 2x 2x                 2x                 10x 2x 1x                 10x 5x   3x       3x   3x   3x 9x 7x 7x   9x     3x                 10x 4x   3x   3x   3x 7x 2x     5x 1x   4x       3x             10x 5x                        
import { isObject, isUndefined, isNull, isPlainObject } from './is'
/**
 *
 * @ignore
 * @return {string} 不满两位前位补0
 *
 */
export function formatAddZero(value: number | string): string {
  let str = ''
  if (typeof value === 'number') {
    str = value.toString()
  } else {
    str = value
  }
  return str[1] ? str : `0${str}`
}
 
/**
 *
 * @ignore
 * @return {string} 手机号脱敏处理
 *
 */
export function dstMobile(mobile: string): string {
  return mobile.replace(/(\d{3})\d{4}(\d{0,4})/, '$1****$2')
}
 
/**
 *
 * @ignore
 * @return {number} 最大最小值之间取随机数
 *
 */
export function random(max: number, min = 0): number {
  return Math.floor(Math.random() * (max - min) + min)
}
 
/**
 *
 * @ignore
 * @return {string} 生产随机手机号
 *
 */
export function creatMobile(dst = false): string {
  let mobile = '1'
  const middle: number[] = [3, 5, 6, 7, 8, 9]
  const len = dst ? '****' : '' + random(9) + random(9) + random(9) + random(9)
  mobile =
    mobile +
    middle[random(middle.length)] +
    random(9) +
    len +
    random(9) +
    random(9) +
    random(9) +
    random(9)
  return mobile
}
 
/**
 *
 * @ignore
 * @return {number} 格式化手机号格式 132 5555 6666
 *
 */
export function formatPhone(phone: string): string {
  if (phone.length !== 11) return ''
  return phone.substring(0, 3) + ' ' + phone.substring(3, 7) + ' ' + phone.substring(7)
}
 
/**
 *
 * @ignore
 * @return {object} 解析url拼接参数 返回一个params 对象
 *
 */
export function parseParams(value: string): object {
  if (!value || value.indexOf('&') === -1) return {}
 
  const arr: string[] = value.split('&')
 
  const params: {
    [prop: string]: any
  } = {}
 
  let i = 0
 
  while (i < arr.length) {
    if (arr[i].indexOf('=') !== -1) {
      const cur = arr[i].split('=')
      params[cur[0]] = cur[1]
    }
    i++
  }
 
  return params
}
 
/**
 *
 * @ignore
 * @return {object} 解析params对象返回一个url拼接参数
 *
 */
export function formatParams(params: any): string {
  if (!isPlainObject(params)) return ''
 
  let str = ''
 
  const keys: string[] = Object.keys(params)
 
  for (let i = 0; i < keys.length; i++) {
    if (isNull(params[keys[i]]) || isUndefined(params[keys[i]])) {
      continue
    }
 
    if (isObject(params[keys[i]])) {
      str += `&${keys[i]}=${JSON.stringify(params[keys[i]])}`
    } else {
      str += `&${keys[i]}=${params[keys[i]]}`
    }
  }
 
  return str.length ? '?' + str.substr(1) : str
}
 
/**
 * @ignore
 * @return {object} 十六进制颜色转rgba
 */
export function hexToRgba(hex: string, opacity?: number | string): string {
  return (
    'rgba(' +
    parseInt('0x' + hex.slice(1, 3), 16) +
    ', ' +
    parseInt('0x' + hex.slice(3, 5), 16) +
    ', ' +
    parseInt('0x' + hex.slice(5, 7), 16) +
    ', ' +
    (opacity || 1) +
    ')'
  )
}