all files / src/utils/ index.js

37.5% Statements 18/48
12.12% Branches 4/33
33.33% Functions 3/9
40.48% Lines 17/42
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    1× 1×                           1× 1× 1× 2× 2× 2× 2×         1×                   1× 1× 1× 1× 5×         1×         1×                                                            
import * as colorsObj from './colors'
 
const colors = Object.keys(colorsObj)
export const getColor = function (color) {
  if (!color) return ''
  return colors.indexOf(color) !== -1 ? colorsObj[color] : color
}
 
export function isNotNull (val) {
  return val !== undefined && val !== null
}
 
export function isNull (val) {
  return val === undefined || val === null
}
 
export function merge (target) {
  for (var i = 1, j = arguments.length; i < j; i++) {
    var source = arguments[i]
    for (var prop in source) {
      Eif (source.hasOwnProperty(prop)) {
        var value = source[prop]
        Eif (value !== undefined) {
          target[prop] = value
        }
      }
    }
  }
  return target
}
 
export function getWidth (w) {
  let width = String(w)
  if (width && width.indexOf('%') === -1 && width.indexOf('px') === -1) width += 'px'
  return width
}
 
export function isPc () {
  var uaInfo = navigator.userAgent
  var agents = ['Android', 'iPhone', 'Windows Phone', 'iPad', 'iPod']
  var flag = true
  for (var i = 0; i < agents.length; i++) {
    Iif (uaInfo.indexOf(agents[i]) > 0) {
      flag = false
      break
    }
  }
  return flag
}
 
export function retina () {
  // 处理retina屏幕显示效果
  Eif (isPc()) return
  var classNames = []
  let pixelRatio = window.devicePixelRatio || 1
  classNames.push('pixel-ratio-' + Math.floor(pixelRatio))
  if (pixelRatio >= 2) {
    classNames.push('retina')
  }
 
  let html = document.getElementsByTagName('html')[0]
 
  classNames.forEach((className) => html.classList.add(className))
}
 
/**
 * 将 String, Object, Array 转成 Array
 */
export function convertClass (classes) {
  let newClasses = []
  if (!classes) return newClasses
  if (classes instanceof Array) {
    newClasses = newClasses.concat(classes)
  } else if (classes instanceof Object) {
    for (const name in classes) {
      if (classes[name]) newClasses.push(name)
    }
  } else {
    newClasses = newClasses.concat(classes.split(' '))
  }
  return newClasses
}