index.ts 2.7 KB

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