index.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /**
  35. * 驼峰转横杠
  36. */
  37. export const humpToDash = (str: string): string => {
  38. return str.replace(/([A-Z])/g, '-$1').toLowerCase()
  39. }
  40. export const setCssVar = (prop: string, val: any, dom = document.documentElement) => {
  41. dom.style.setProperty(prop, val)
  42. }
  43. export const getCssVar = (prop: string, dom = document.documentElement) => {
  44. return getComputedStyle(dom).getPropertyValue(prop)
  45. }
  46. /**
  47. * 查找数组对象的某个下标
  48. * @param {Array} ary 查找的数组
  49. * @param {Functon} fn 判断的方法
  50. */
  51. export const findIndex = <T = Recordable>(ary: Array<T>, fn: Fn): number => {
  52. if (ary.findIndex) {
  53. return ary.findIndex(fn)
  54. }
  55. let index = -1
  56. ary.some((item: T, i: number, ary: Array<T>) => {
  57. const ret: T = fn(item, i, ary)
  58. if (ret) {
  59. index = i
  60. return ret
  61. }
  62. })
  63. return index
  64. }
  65. export const trim = (str: string) => {
  66. return str.replace(/(^\s*)|(\s*$)/g, '')
  67. }
  68. /**
  69. * @param {Date | number | string} time 需要转换的时间
  70. * @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
  71. */
  72. export function formatTime(time: Date | number | string, fmt: string) {
  73. if (!time) return ''
  74. else {
  75. const date = new Date(time)
  76. const o = {
  77. 'M+': date.getMonth() + 1,
  78. 'd+': date.getDate(),
  79. 'H+': date.getHours(),
  80. 'm+': date.getMinutes(),
  81. 's+': date.getSeconds(),
  82. 'q+': Math.floor((date.getMonth() + 3) / 3),
  83. S: date.getMilliseconds()
  84. }
  85. if (/(y+)/.test(fmt)) {
  86. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  87. }
  88. for (const k in o) {
  89. if (new RegExp('(' + k + ')').test(fmt)) {
  90. fmt = fmt.replace(
  91. RegExp.$1,
  92. RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
  93. )
  94. }
  95. }
  96. return fmt
  97. }
  98. }
  99. /**
  100. * 生成随机字符串
  101. */
  102. export function toAnyString() {
  103. const str: string = 'xxxxx-xxxxx-4xxxx-yxxxx-xxxxx'.replace(/[xy]/g, (c: string) => {
  104. const r: number = (Math.random() * 16) | 0
  105. const v: number = c === 'x' ? r : (r & 0x3) | 0x8
  106. return v.toString()
  107. })
  108. return str
  109. }
  110. /**
  111. * 首字母大写
  112. */
  113. export function firstUpperCase(str: string) {
  114. return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())
  115. }
  116. /**
  117. * 把对象转为formData
  118. */
  119. export function objToFormData(obj: Recordable) {
  120. const formData = new FormData()
  121. Object.keys(obj).forEach((key) => {
  122. formData.append(key, obj[key])
  123. })
  124. return formData
  125. }