index.js 2.83 KB
Newer Older
Nature's avatar
Nature committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**
 * 一些帮助函数
 */

/**
 * setTimeout 的 promise 封装
 * @param {Number} time
 * @returns
 */
export function timeout (time) {
  return new Promise(resolve => {
    setTimeout(resolve, time)
  })
}

/**
 * 断言
 * @param {any} condition 条件
 * @param {any} msg 信息
 */
export function assert (condition, msg) {
Nature's avatar
Nature committed
22
  if (!condition) throw new Error(`[hls-ui] ${msg}`)
Nature's avatar
Nature committed
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
}

/**
 * 改变 case 格式为 param
 * @param {String} str 字符串
 */
export function case2Param (str) {
  assert(typeof str === 'string', 'case2Param 传入数据类型错误:应为 String')

  str = str.replace(/^[A-Z]/g, $0 => $0.toLowerCase())
  return str.replace(/[A-Z]/g, $0 => `-${$0.toLowerCase()}`)
}

/**
 * 判断平台
 * @return {String} 平台
 */
export function detectOS () {
  const ua = navigator.userAgent.toLowerCase()

  if (/MicroMessenger/i.test(ua)) {
    return 'weixin'
  } else if (/iPhone|iPad|iPod|iOS/i.test(ua)) {
    return 'ios'
  } else if (/Android/i.test(ua)) {
    return 'android'
  } else {
    return 'other'
  }
}
JingChao's avatar
JingChao committed
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

export function range (num, min, max) {
  return Math.min(Math.max(num, min), max)
}

function trimExtraChar (value, char, regExp) {
  const index = value.indexOf(char)

  if (index === -1) {
    return value
  }

  if (char === '-' && index !== 0) {
    return value.slice(0, index)
  }

  return value.slice(0, index + 1) + value.slice(index).replace(regExp, '')
}

export function formatNumber (value, allowDot) {
  if (value) {
    if (allowDot) {
      value = trimExtraChar(value, '.', /\./g)
    } else {
      value = value.split('.')[0]
    }

    value = trimExtraChar(value, '-', /-/g)

    const regExp = allowDot ? /[^-0-9.]/g : /[^-0-9]/g

    return value.replace(regExp, '')
  }
}

export function isDef (val) {
  return val !== undefined && val !== null && val !== ''
}

export function isUndefined (value) {
  return Object.prototype.toString.call(value) === '[object Undefined]'
}

export function isString (value) {
  return Object.prototype.toString.call(value) === '[object String]'
}
export function isNumber (value) {
  return Object.prototype.toString.call(value) === '[object Number]'
}
export function isBoolean (value) {
  return Object.prototype.toString.call(value) === '[object Boolean]'
}
export function isNull (value) {
  return Object.prototype.toString.call(value) === '[object Null]'
}
export function isObject (value) {
  return Object.prototype.toString.call(value) === '[object Object]'
}
export function isFunction (value) {
  return Object.prototype.toString.call(value) === '[object Function]'
}
export function isArray (value) {
  return Object.prototype.toString.call(value) === '[object Array]'
}
export function isDate (value) {
  return Object.prototype.toString.call(value) === '[object Date]'
}
export function isRegExp (value) {
  return Object.prototype.toString.call(value) === '[object RegExp]'
}