util.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const util = {}
  2. // const { formatDateTime } = require('../../../../util/util')
  3. Date.prototype.Format = function (fmt) {
  4. const o = {
  5. 'M+': this.getMonth() + 1, // 月份
  6. 'd+': this.getDate(), // 日
  7. 'h+': this.getHours(), // 小时
  8. 'm+': this.getMinutes(), // 分
  9. 's+': this.getSeconds(), // 秒
  10. 'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
  11. S: this.getMilliseconds() // 毫秒
  12. }
  13. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
  14. for (const k in o) {
  15. if (new RegExp('(' + k + ')').test(fmt)) {
  16. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  17. }
  18. }
  19. return fmt
  20. }
  21. util.renderName = (name) => {
  22. switch (name) {
  23. case 'appLaunch':
  24. return '小程序启动'
  25. break
  26. case 'firstRender':
  27. return '页面首次渲染'
  28. break
  29. case 'route':
  30. return '路由性能'
  31. break
  32. case 'evaluateScript':
  33. return '注入脚本'
  34. break
  35. }
  36. }
  37. util.renderEntryType = (entryType) => {
  38. switch (entryType) {
  39. case 'navigation':
  40. return '路由'
  41. break
  42. case 'render':
  43. return '渲染'
  44. break
  45. case 'script':
  46. return '脚本'
  47. break
  48. }
  49. }
  50. util.renderDuration = (duration) => (duration ? duration + 'ms' : '')
  51. util.renderStartTime = (startTime) => {
  52. if (!startTime) return ''
  53. const date = new Date(startTime)
  54. return date.Format('yyyy-MM-dd hh:mm:ss')
  55. }
  56. module.exports = util