index.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. *
  3. * @param component 需要注册的组件
  4. * @param alias 组件别名
  5. * @returns any
  6. */
  7. export const withInstall = <T>(component: T, alias?: string) => {
  8. const comp = component as any
  9. comp.install = (app: any) => {
  10. app.component(comp.name || comp.displayName, component)
  11. if (alias) {
  12. app.config.globalProperties[alias] = component
  13. }
  14. }
  15. return component as T & Plugin
  16. }
  17. /**
  18. * @param str 需要转下划线的驼峰字符串
  19. * @returns 字符串下划线
  20. */
  21. export const humpToUnderline = (str: string): string => {
  22. return str.replace(/([A-Z])/g, '-$1').toLowerCase()
  23. }
  24. /**
  25. * @param str 需要转驼峰的下划线字符串
  26. * @returns 字符串驼峰
  27. */
  28. export const underlineToHump = (str: string): string => {
  29. if (!str) return ''
  30. return str.replace(/\-(\w)/g, (_, letter: string) => {
  31. return letter.toUpperCase()
  32. })
  33. }
  34. export const setCssVar = (prop: string, val: any, dom = document.documentElement) => {
  35. dom.style.setProperty(prop, val)
  36. }
  37. /**
  38. * 查找数组对象的某个下标
  39. * @param {Array} ary 查找的数组
  40. * @param {Functon} fn 判断的方法
  41. */
  42. // eslint-disable-next-line
  43. export const findIndex = <T = Recordable>(ary: Array<T>, fn: Fn): number => {
  44. if (ary.findIndex) {
  45. return ary.findIndex(fn)
  46. }
  47. let index = -1
  48. ary.some((item: T, i: number, ary: Array<T>) => {
  49. const ret: T = fn(item, i, ary)
  50. if (ret) {
  51. index = i
  52. return ret
  53. }
  54. })
  55. return index
  56. }
  57. export const trim = (str: string) => {
  58. return str.replace(/(^\s*)|(\s*$)/g, '')
  59. }
  60. /**
  61. * @param {Date | number | string} time 需要转换的时间
  62. * @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
  63. */
  64. export const formatTime = (time: Date | number | string, fmt: string) => {
  65. if (!time) return ''
  66. else {
  67. const date = new Date(time)
  68. const o = {
  69. 'M+': date.getMonth() + 1,
  70. 'd+': date.getDate(),
  71. 'H+': date.getHours(),
  72. 'm+': date.getMinutes(),
  73. 's+': date.getSeconds(),
  74. 'q+': Math.floor((date.getMonth() + 3) / 3),
  75. S: date.getMilliseconds()
  76. }
  77. if (/(y+)/.test(fmt)) {
  78. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  79. }
  80. for (const k in o) {
  81. if (new RegExp('(' + k + ')').test(fmt)) {
  82. fmt = fmt.replace(
  83. RegExp.$1,
  84. RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
  85. )
  86. }
  87. }
  88. return fmt
  89. }
  90. }
  91. /**
  92. * 生成随机字符串
  93. */
  94. export const toAnyString = () => {
  95. const str: string = 'xxxxx-xxxxx-4xxxx-yxxxx-xxxxx'.replace(/[xy]/g, (c: string) => {
  96. const r: number = (Math.random() * 16) | 0
  97. const v: number = c === 'x' ? r : (r & 0x3) | 0x8
  98. return v.toString()
  99. })
  100. return str
  101. }
  102. export const generateUUID = () => {
  103. if (typeof crypto === 'object') {
  104. if (typeof crypto.randomUUID === 'function') {
  105. return crypto.randomUUID()
  106. }
  107. if (typeof crypto.getRandomValues === 'function' && typeof Uint8Array === 'function') {
  108. const callback = (c: any) => {
  109. const num = Number(c)
  110. return (num ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (num / 4)))).toString(
  111. 16
  112. )
  113. }
  114. return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, callback)
  115. }
  116. }
  117. let timestamp = new Date().getTime()
  118. let performanceNow =
  119. (typeof performance !== 'undefined' && performance.now && performance.now() * 1000) || 0
  120. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
  121. let random = Math.random() * 16
  122. if (timestamp > 0) {
  123. random = (timestamp + random) % 16 | 0
  124. timestamp = Math.floor(timestamp / 16)
  125. } else {
  126. random = (performanceNow + random) % 16 | 0
  127. performanceNow = Math.floor(performanceNow / 16)
  128. }
  129. return (c === 'x' ? random : (random & 0x3) | 0x8).toString(16)
  130. })
  131. }