index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @param {string} str
  3. * @returns {string}
  4. */
  5. export function strTrim(str) {
  6. return str.replace(/(^\s*)|(\s*$)/g, '')
  7. }
  8. /**
  9. * @param {Number} num
  10. * @returns {string}
  11. */
  12. export function numFormat3 (num, dotNum = 2) {
  13. let numArr = Number(num).toFixed(dotNum).toString().split('.')
  14. let arr = numArr[0].split('').reverse()
  15. let res = []
  16. for (let i=0; i<arr.length; i++) {
  17. if ( i % 3 === 0 && i !== 0 ){
  18. res.push(',')
  19. }
  20. res.push(arr[i])
  21. }
  22. res.reverse()
  23. if (numArr[1]) {
  24. res = res.join('').concat('.' + numArr[1])
  25. } else {
  26. res = res.join('')
  27. }
  28. // let res = num.toString().replace(/\d+/, function(n){
  29. // return n.replace(/(\d)(?=(\d{3})+$)/g,function($1){
  30. // return $1 + ","
  31. // })
  32. // })
  33. // toLocaleString()
  34. return res
  35. }
  36. /**
  37. * @param {Array} arr
  38. * @returns {Object}
  39. */
  40. export function arrToObj (arr) {
  41. const obj = {}
  42. if (Object.prototype.toString.call(arr) === '[object Array]') {
  43. if (arr.length > 0) {
  44. arr.forEach(item => {
  45. obj[item.val] = item.key
  46. })
  47. }
  48. }
  49. return obj
  50. }
  51. /**
  52. * @param {any} val
  53. * @returns {Boolean}
  54. */
  55. export function noEmpty(val) {
  56. if (val === null || val === undefined || val === '') {
  57. return false
  58. } else {
  59. return true
  60. }
  61. }
  62. /**
  63. * @param {Number} date
  64. * @param {Boolean} onlyDate
  65. * @returns {String}
  66. * function time(time = +new Date()) {
  67. var date = new Date(time + 8 * 3600 * 1000); // 增加8小时
  68. return date.toJSON().substr(0, 19).replace('T', ' ');
  69. }
  70. */
  71. export function comGetTime(date = new Date(), onlyDate = true) {
  72. let year = date.getFullYear()
  73. let month = date.getMonth() + 1
  74. month = month > 9 ? month : '0' + month
  75. let day = date.getDate()
  76. day = day > 9 ? day : '0' + day
  77. let hour = date.getHours()
  78. hour = hour > 9 ? hour : '0' + hour
  79. let minute = date.getMinutes()
  80. minute = minute > 9 ? minute : '0' + minute
  81. let second = date.getSeconds()
  82. second = second > 9 ? second : '0' + second
  83. let milli = date.getMilliseconds()
  84. milli = milli > 100 ? milli : milli > 10 ? '0' + milli : '00' + milli
  85. return onlyDate ? `${year}-${month}-${day}` : `${year}${month}${day}${hour}${minute}${second}${milli}`
  86. }