const camelizeReg = /-(\w)/g
const kebabCaseReg = /([A-Z])/g
/**
* kebab转小驼峰
*/
export function camelize(str: string): string {
return str.replace(camelizeReg, (_, c) => c.toUpperCase())
}
/**
* 小驼峰字符串转kebab
*/
export function kebabCase(str: string): string {
return str.replace(kebabCaseReg, '-$1').toLocaleLowerCase().replace(/^-/, '')
}
/**
* 判断某个字符串是否为正确的数字格式
*/
export function isNumeric(val: string): boolean {
return /^\d+(\.\d+)?$/.test(val)
}
/**
* 判断是否为开发环境
*/
export const isDev = process.env.NODE_ENV === 'development'
/**
* 判断是否在浏览器里
*/
export const inBrowser = typeof window !== 'undefined'
/**
* 判断是否为Android系统
*/
export function isAndroid(): boolean {
return inBrowser ? /android/.test(navigator.userAgent.toLowerCase()) : false
}
/**
* 判断是否为iOS系统
*/
export function isIOS(): boolean {
return inBrowser ? /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase()) : false
}
/**
* 判断是否定义了: 非null并且非undefined
*/
export function isDef<T>(val: T): val is NonNullable<T> {
return val !== undefined && val !== null
}
/**
* 判断是否为函数
*/
export function isFunction(val: unknown): val is Function {
return typeof val === 'function'
}
/**
* 判断是否为非null的object
* @param val
* @returns
*/
export function isObject(val: unknown): val is Record<any, any> {
return val !== null && typeof val === 'object'
}
/**
* 判断是否为promise
*/
export function isPromise<T = any>(val: unknown): val is Promise<T> {
return isObject(val) && isFunction(val.then) && isFunction(val.catch)
}
网友评论