/**
 * @param {string} str
 * @returns {string}
 */
export function strTrim(str) {
  return str.replace(/(^\s*)|(\s*$)/g, '')
}
/**
 * @param {Number} num
 * @returns {string}
 */
export function numFormat3 (num, dotNum = 2) {
  let numArr = Number(num).toFixed(dotNum).toString().split('.')
  let arr = numArr[0].split('').reverse()
  let res = []
  for (let i=0; i<arr.length; i++) {
    if ( i % 3 === 0 && i !== 0 ){
      res.push(',')
    }
    res.push(arr[i])
  }
  res.reverse()
  if (numArr[1]) {
    res = res.join('').concat('.' + numArr[1])
  } else {
    res = res.join('')
  }
  // let res = num.toString().replace(/\d+/, function(n){
  //   return n.replace(/(\d)(?=(\d{3})+$)/g,function($1){
  //     return $1 + ","
  //   })
  // })
  // toLocaleString()
  return res
}

/**
 * @param {Array} arr
 * @returns {Object}
 */
export function arrToObj (arr) {
  const obj = {}
  if (Object.prototype.toString.call(arr) === '[object Array]') {
    if (arr.length > 0) {
      arr.forEach(item => {
        obj[item.val] = item.key
      })
    }
  }
  return obj
}

/**
 * @param {any} val
 * @returns {Boolean}
 */
export function noEmpty(val) {
  if (val === null || val === undefined || val === '') {
    return false
  } else {
    return true
  }
}

/** 
 * @param {Number} date
 * @param {Boolean} onlyDate
 * @returns {String}
 * function time(time = +new Date()) {
    var date = new Date(time + 8 * 3600 * 1000); // 增加8小时
    return date.toJSON().substr(0, 19).replace('T', ' ');
  }
 */
export function comGetTime(date = new Date(), onlyDate = true) {
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  month = month > 9 ? month : '0' + month
  let day = date.getDate()
  day = day > 9 ? day : '0' + day
  let hour = date.getHours()
  hour = hour > 9 ? hour : '0' + hour
  let minute = date.getMinutes()
  minute = minute > 9 ? minute : '0' + minute
  let second = date.getSeconds()
  second = second > 9 ? second : '0' + second
  let milli = date.getMilliseconds()
  milli = milli > 100 ? milli : milli > 10 ? '0' + milli : '00' + milli
  return onlyDate ? `${year}-${month}-${day}` : `${year}${month}${day}${hour}${minute}${second}${milli}`
}
