util.js 2.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
const _toString = Object.prototype.toString
const hasOwnProperty = Object.prototype.hasOwnProperty

4 5 6 7
const _completeValue = value => {
  return value > 9 ? value : ('0' + value)
}

fxy060608's avatar
fxy060608 已提交
8 9 10
export function isFn (fn) {
  return typeof fn === 'function'
}
fxy060608's avatar
fxy060608 已提交
11 12 13 14 15

export function isStr (str) {
  return typeof str === 'string'
}

16 17 18 19
export function isObject (obj) {
  return obj !== null && typeof obj === 'object'
}

fxy060608's avatar
fxy060608 已提交
20 21 22 23 24 25 26 27
export function isPlainObject (obj) {
  return _toString.call(obj) === '[object Object]'
}

export function hasOwn (obj, key) {
  return hasOwnProperty.call(obj, key)
}

28
export function noop () {}
fxy060608's avatar
fxy060608 已提交
29 30 31 32 33

export function toRawType (val) {
  return _toString.call(val).slice(8, -1)
}

fxy060608's avatar
fxy060608 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/**
 * Create a cached version of a pure function.
 */
export function cached (fn) {
  const cache = Object.create(null)
  return function cachedFn (str) {
    const hit = cache[str]
    return hit || (cache[str] = fn(str))
  }
}

/**
 * Camelize a hyphen-delimited string.
 */
const camelizeRE = /-(\w)/g
export const camelize = cached((str) => {
  return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
})

fxy060608's avatar
fxy060608 已提交
53 54 55 56 57 58 59 60
export function setProperties (item, props, propsData) {
  props.forEach(function (name) {
    if (hasOwn(propsData, name)) {
      item[name] = propsData[name]
    }
  })
}

fxy060608's avatar
fxy060608 已提交
61
export function getLen (str = '') {
fxy060608's avatar
fxy060608 已提交
62 63
  /* eslint-disable no-control-regex */
  return ('' + str).replace(/[^\x00-\xff]/g, '**').length
64 65 66 67 68 69 70 71 72 73 74
}

export function formatDateTime ({
  date = new Date(),
  mode = 'date'
}) {
  if (mode === 'time') {
    return _completeValue(date.getHours()) + ':' + _completeValue(date.getMinutes())
  } else {
    return date.getFullYear() + '-' + _completeValue(date.getMonth() + 1) + '-' + _completeValue(date.getDate())
  }
fxy060608's avatar
fxy060608 已提交
75
}
76 77 78 79 80

export function updateElementStyle (element, styles) {
  for (let attrName in styles) {
    element.style[attrName] = styles[attrName]
  }
fxy060608's avatar
init v3  
fxy060608 已提交
81 82 83 84
}

export function guid () {
  return Math.floor(4294967296 * (1 + Math.random())).toString(16).slice(1)
fxy060608's avatar
fxy060608 已提交
85 86 87 88 89 90 91 92 93
}

export function debounce (fn, delay) {
  let timeout
  return function () {
    clearTimeout(timeout)
    const timerFn = () => fn.apply(this, arguments)
    timeout = setTimeout(timerFn, delay)
  }
Q
qiang 已提交
94 95 96 97
}

export function kebabCase (string) {
  return string.replace(/[A-Z]/g, str => '-' + str.toLowerCase())
98
}