2021-12-11 11:46:10 +08:00
|
|
|
import type { App, Plugin } from 'vue'
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param component 需要注册的组件
|
|
|
|
* @param alias 组件别名
|
|
|
|
* @returns any
|
|
|
|
*/
|
|
|
|
export const withInstall = <T>(component: T, alias?: string) => {
|
|
|
|
const comp = component as any
|
|
|
|
comp.install = (app: App) => {
|
|
|
|
app.component(comp.name || comp.displayName, component)
|
|
|
|
if (alias) {
|
|
|
|
app.config.globalProperties[alias] = component
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return component as T & Plugin
|
|
|
|
}
|
2022-01-05 17:02:25 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param str 需要转下划线的驼峰字符串
|
|
|
|
* @returns 字符串下划线
|
|
|
|
*/
|
2022-01-15 14:24:50 +08:00
|
|
|
export const humpToUnderline = (str: string): string => {
|
2022-01-05 17:02:25 +08:00
|
|
|
return str.replace(/([A-Z])/g, '-$1').toLowerCase()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param str 需要转驼峰的下划线字符串
|
|
|
|
* @returns 字符串驼峰
|
|
|
|
*/
|
2022-01-15 14:24:50 +08:00
|
|
|
export const underlineToHump = (str: string): string => {
|
|
|
|
return str.replace(/\-(\w)/g, (_, letter: string) => {
|
2022-01-05 17:02:25 +08:00
|
|
|
return letter.toUpperCase()
|
|
|
|
})
|
|
|
|
}
|
2022-01-13 17:26:06 +08:00
|
|
|
|
2022-01-15 14:24:50 +08:00
|
|
|
export const setCssVar = (prop: string, val: any, dom = document.documentElement) => {
|
2022-01-13 17:26:06 +08:00
|
|
|
dom.style.setProperty(prop, val)
|
|
|
|
}
|
2022-01-16 17:55:20 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 查找数组对象的某个下标
|
|
|
|
* @param {Array} ary 查找的数组
|
|
|
|
* @param {Functon} fn 判断的方法
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line
|
|
|
|
export const findIndex = <T = Recordable>(ary: Array<T>, fn: Fn): number => {
|
|
|
|
if (ary.findIndex) {
|
|
|
|
return ary.findIndex(fn)
|
|
|
|
}
|
|
|
|
let index = -1
|
|
|
|
ary.some((item: T, i: number, ary: Array<T>) => {
|
|
|
|
const ret: T = fn(item, i, ary)
|
|
|
|
if (ret) {
|
|
|
|
index = i
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return index
|
|
|
|
}
|
2022-01-18 16:22:47 +08:00
|
|
|
|
|
|
|
export const trim = (str: string) => {
|
|
|
|
return str.replace(/(^\s*)|(\s*$)/g, '')
|
|
|
|
}
|