index.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. }
  132. /**
  133. * element plus 的文件大小 Formatter 实现
  134. *
  135. * @param row 行数据
  136. * @param column 字段
  137. * @param cellValue 字段值
  138. */
  139. // @ts-ignore
  140. export const fileSizeFormatter = (row, column, cellValue) => {
  141. const fileSize = cellValue
  142. const unitArr = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
  143. const srcSize = parseFloat(fileSize)
  144. const index = Math.floor(Math.log(srcSize) / Math.log(1024))
  145. const size = srcSize / Math.pow(1024, index)
  146. const sizeStr = size.toFixed(2) //保留的小数位数
  147. return sizeStr + ' ' + unitArr[index]
  148. }
  149. /**
  150. * 将值复制到目标对象,且以目标对象属性为准,例:target: {a:1} source:{a:2,b:3} 结果为:{a:2}
  151. * @param target 目标对象
  152. * @param source 源对象
  153. */
  154. export const copyValueToTarget = (target, source) => {
  155. const newObj = Object.assign({}, target, source)
  156. // 删除多余属性
  157. Object.keys(newObj).forEach((key) => {
  158. // 如果不是target中的属性则删除
  159. if (Object.keys(target).indexOf(key) === -1) {
  160. delete newObj[key]
  161. }
  162. })
  163. // 更新目标对象值
  164. Object.assign(target, newObj)
  165. }
  166. /**
  167. * 将一个整数转换为分数保留两位小数
  168. * @param num
  169. */
  170. export const formatToFraction = (num: number | string | undefined): number => {
  171. if (typeof num === 'undefined') return 0
  172. const parsedNumber = typeof num === 'string' ? parseFloat(num) : num
  173. return parseFloat((parsedNumber / 100).toFixed(2))
  174. }
  175. /**
  176. * 将一个分数转换为整数
  177. * @param num
  178. */
  179. export const convertToInteger = (num: number | string | undefined): number => {
  180. if (typeof num === 'undefined') return 0
  181. const parsedNumber = typeof num === 'string' ? parseFloat(num) : num
  182. // TODO 分转元后还有小数则四舍五入
  183. return Math.round(parsedNumber * 100)
  184. }
  185. /**
  186. * 元转分
  187. */
  188. export const yuanToFen = (amount: string | number): number => {
  189. return Math.round(Number(amount) * 100)
  190. }
  191. /**
  192. * 分转元
  193. */
  194. export const fenToYuan = (amount: string | number): number => {
  195. return Number((Number(amount) / 100).toFixed(2))
  196. }