import type {Ref} from 'vue' import {getCurrentInstance} from 'vue' import { camelize, capitalize, extend, hasOwn, hyphenate, isArray, isObject, isString, looseEqual, toRawType } from '@vue/shared' import jServer from './judgeServer' import type {AnyFunction} from './types' import {warn} from './error' export const SCOPE = 'Util' export type PartialCSSStyleDeclaration = Partial> export function toObject(arr: Array): Record { const res = {} for (let i = 0; i < arr.length; i++) { if (arr[i]) { extend(res, arr[i]) } } return res } export const getValueByPath = (obj: any, paths = ''): unknown => { let ret: unknown = obj paths.split('.').map(path => { ret = ret?.[path] }) return ret } export function getPropByPath(obj: any, path: string, strict: boolean): { o: unknown, k: string, v: Nullable } { let tempObj = obj path = path.replace(/\[(\w+)\]/g, '.$1') path = path.replace(/^\./, '') const keyArr = path.split('.') let i = 0 for (i; i < keyArr.length - 1; i++) { if (!tempObj && !strict) break const key = keyArr[i] if (key in tempObj) { tempObj = tempObj[key] } else { if (strict) { throw new Error('[ROUI warn]:please transfer a valid prop path to form item!(请传递一个有效prop路径到form item中)') } break } } return { o: tempObj, k: keyArr[i], v: tempObj?.[keyArr[i]], } } export const generateId = (): number => Math.floor(Math.random() * 10000) export const escapeRegexpString = (value = ''): string => String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&') export const coerceTruthyValueToArray = arr => { if (!arr && arr !== 0) { return [] } return Array.isArray(arr) ? arr : [arr] } export const isIE = function (): boolean { return !jServer && !isNaN(Number(document.DOCUMENT_NODE)) } export const isEdge = function (): boolean { return !jServer && navigator.userAgent.indexOf('Edge') > -1 } export const isFirefox = function (): boolean { return !jServer && !!window.navigator.userAgent.match(/firefox/i) } export const autoprefixer = function ( style: PartialCSSStyleDeclaration, ): PartialCSSStyleDeclaration { const rules = ['transform', 'transition', 'animation'] const prefixes = ['ms-', 'webkit-'] rules.forEach(rule => { const value = style[rule] if (rule && value) { prefixes.forEach(prefix => { style[prefix + rule] = value }) } }) return style } export const kebabCase = hyphenate // reexport from lodash & vue shared export { hasOwn, // isEmpty, // isEqual, isObject, isArray, isString, capitalize, camelize, looseEqual, extend, } export const isBool = (val: unknown) => typeof val === 'boolean' export const isNumber = (val: unknown) => typeof val === 'number' export const isHTMLElement = (val: unknown) => toRawType(val).startsWith('HTML') export function rafThrottle>(fn: T): AnyFunction { let locked = false return function (...args: any[]) { if (locked) return locked = true window.requestAnimationFrame(() => { fn.apply(this, args) locked = false }) } } export const clearTimer = (timer: Ref) => { clearTimeout(timer.value) timer.value = null } /** * Generating a random int in range (0, max - 1) * @param max {number} */ export function getRandomInt(max: number) { return Math.floor(Math.random() * Math.floor(max)) } export function entries(obj: Hash): [string, T][] { return Object .keys(obj) .map((key: string) => ([key, obj[key]])) } export function isUndefined(val: any): val is undefined { return val === void 0 } export {isVNode} from 'vue' export function GlobalConfig() { const ci: any = getCurrentInstance() if ('$ELEMENT' in ci.proxy) { return ci.proxy.$ELEMENT } return {} } export const arrayFindIndex = function ( arr: Array, pred: (args: T) => boolean, ): number { return arr.findIndex(pred) } export const arrayFind = function ( arr: Array, pred: (args: T) => boolean, ): any { return arr.find(pred) } export function isEmpty(val: unknown) { if ( !val && val !== 0 || isArray(val) && !val.length || isObject(val) && !Object.keys(val).length ) return true return false } export function arrayFlat(arr: unknown[]) { return arr.reduce((acm: unknown[], item) => { const val = Array.isArray(item) ? arrayFlat(item) : item return acm.concat(val) }, []) } export function deduplicate(arr: T[]) { return Array.from(new Set(arr)) } /** * Unwraps refed value * @param ref Refed value */ export function $(ref: Ref) { return ref.value } export function addUnit(value: string | number) { if (isString(value)) { return value } else if (isNumber(value)) { return value + 'px' } if (process.env.NODE_ENV === 'development') { warn(SCOPE, 'binding value must be a string or number') } return '' } export function isDef(text: string): boolean { const reg = /([(\uAC00-\uD7AF)|(\u3130-\u318F)])+/gi return reg.test(text) } export function oneOf(val, valList) { for (let i = 0; i < valList.length; i++) { if (val === valList[i]) return true } return false } export function toRGBA(color) { let r = 0, g = 0, b = 0, a = 1 if (color.startsWith("#")) { if (color.length === 4) { let rr = color.substring(1, 2), gg = color.substring(2, 3), bb = color.substring(3, 4) r = parseInt(rr + rr, 16) g = parseInt(gg + gg, 16) b = parseInt(bb + bb, 16) } else { r = parseInt(color.substring(1, 3), 16) g = parseInt(color.substring(3, 5), 16) b = parseInt(color.substring(5, 7), 16) } } else if (color.startsWith('rgb(')) { let arr = color.substr(0, color.length - 1).split("rgb(")[1].split(",") r = arr[0] g = arr[1] b = arr[2] } else if (color.startsWith('rgba(')) { let arr = color.substr(0, color.length - 1).split("rgba(")[1].split(",") r = arr[0] g = arr[1] b = arr[2] a = arr[3] } return [r, g, b, a] } export function isLightColor(color) { if (!color) return true let rgba = toRGBA(color) if (rgba[3] == 0) return true let avg = (rgba[0] / rgba[3] + rgba[1] / rgba[3] + rgba[2] / rgba[3]) / 3 if (avg > 127) { return true } else { return false } }