2020-10-31 15:20:38 +08:00
|
|
|
import { getCurrentInstance } from 'vue'
|
2021-09-04 19:29:28 +08:00
|
|
|
import {
|
|
|
|
camelize,
|
|
|
|
capitalize,
|
|
|
|
extend,
|
|
|
|
hasOwn,
|
|
|
|
hyphenate,
|
|
|
|
isArray,
|
|
|
|
isObject,
|
|
|
|
isString,
|
|
|
|
isFunction,
|
|
|
|
looseEqual,
|
|
|
|
toRawType,
|
|
|
|
} from '@vue/shared'
|
2021-06-25 16:08:00 +08:00
|
|
|
import isEqualWith from 'lodash/isEqualWith'
|
2020-09-14 10:03:33 +08:00
|
|
|
import isServer from './isServer'
|
2021-10-18 16:30:45 +08:00
|
|
|
import { debugWarn, throwError } from './error'
|
2021-01-28 22:10:45 +08:00
|
|
|
|
2021-08-24 13:36:48 +08:00
|
|
|
import type { ComponentPublicInstance, CSSProperties, Ref } from 'vue'
|
2021-10-18 16:24:52 +08:00
|
|
|
import type { TimeoutHandle, Nullable } from './types'
|
2021-04-09 10:00:21 +08:00
|
|
|
|
2021-01-28 22:10:45 +08:00
|
|
|
export const SCOPE = 'Util'
|
2020-07-28 00:32:04 +08:00
|
|
|
|
|
|
|
export function toObject<T>(arr: Array<T>): Record<string, T> {
|
|
|
|
const res = {}
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
|
|
if (arr[i]) {
|
|
|
|
extend(res, arr[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:55:04 +08:00
|
|
|
export const getValueByPath = (obj, paths = ''): unknown => {
|
2020-07-28 00:32:04 +08:00
|
|
|
let ret: unknown = obj
|
2021-09-04 19:29:28 +08:00
|
|
|
paths.split('.').map((path) => {
|
2020-07-28 00:32:04 +08:00
|
|
|
ret = ret?.[path]
|
|
|
|
})
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
export function getPropByPath(
|
|
|
|
obj: any,
|
|
|
|
path: string,
|
|
|
|
strict: boolean
|
|
|
|
): {
|
2020-08-02 21:29:35 +08:00
|
|
|
o: unknown
|
|
|
|
k: string
|
|
|
|
v: Nullable<unknown>
|
2020-07-28 00:32:04 +08:00
|
|
|
} {
|
2020-08-24 15:38:30 +08:00
|
|
|
let tempObj = obj
|
2021-09-13 03:14:47 +08:00
|
|
|
let key, value
|
2020-07-28 00:32:04 +08:00
|
|
|
|
2021-09-13 03:14:47 +08:00
|
|
|
if (obj && hasOwn(obj, path)) {
|
|
|
|
key = path
|
|
|
|
value = tempObj?.[path]
|
|
|
|
} else {
|
|
|
|
path = path.replace(/\[(\w+)\]/g, '.$1')
|
|
|
|
path = path.replace(/^\./, '')
|
2020-12-08 21:04:04 +08:00
|
|
|
|
2021-09-13 03:14:47 +08:00
|
|
|
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) {
|
2021-10-18 16:30:45 +08:00
|
|
|
throwError(SCOPE, 'Please transfer a valid prop path to form item!')
|
2021-09-13 03:14:47 +08:00
|
|
|
}
|
|
|
|
break
|
2020-12-08 21:04:04 +08:00
|
|
|
}
|
2020-07-28 00:32:04 +08:00
|
|
|
}
|
2021-09-13 03:14:47 +08:00
|
|
|
key = keyArr[i]
|
|
|
|
value = tempObj?.[keyArr[i]]
|
2020-07-28 00:32:04 +08:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
o: tempObj,
|
2021-09-13 03:14:47 +08:00
|
|
|
k: key,
|
|
|
|
v: value,
|
2020-07-28 00:32:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate random number in range [0, 1000]
|
|
|
|
* Maybe replace with [uuid](https://www.npmjs.com/package/uuid)
|
|
|
|
*/
|
|
|
|
export const generateId = (): number => Math.floor(Math.random() * 10000)
|
|
|
|
|
|
|
|
// use isEqual instead
|
|
|
|
// export const valueEquals
|
|
|
|
|
2020-08-24 15:38:30 +08:00
|
|
|
export const escapeRegexpString = (value = ''): string =>
|
2020-07-28 00:32:04 +08:00
|
|
|
String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
|
|
|
|
|
|
|
|
// Use native Array.find, Array.findIndex instead
|
|
|
|
|
|
|
|
// coerce truthy value to array
|
2021-09-04 19:29:28 +08:00
|
|
|
export const coerceTruthyValueToArray = (arr) => {
|
2021-01-28 22:10:45 +08:00
|
|
|
if (!arr && arr !== 0) {
|
|
|
|
return []
|
|
|
|
}
|
2020-11-10 14:24:09 +08:00
|
|
|
return Array.isArray(arr) ? arr : [arr]
|
2020-09-16 14:49:21 +08:00
|
|
|
}
|
2020-07-28 00:32:04 +08:00
|
|
|
|
2021-09-10 15:00:39 +08:00
|
|
|
// drop IE and (Edge < 79) support
|
|
|
|
// export const isIE
|
|
|
|
// export const isEdge
|
2020-07-28 00:32:04 +08:00
|
|
|
|
2021-01-28 22:10:45 +08:00
|
|
|
export const isFirefox = function (): boolean {
|
2020-09-15 10:50:32 +08:00
|
|
|
return !isServer && !!window.navigator.userAgent.match(/firefox/i)
|
2020-07-28 00:32:04 +08:00
|
|
|
}
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
export const autoprefixer = function (style: CSSProperties): CSSProperties {
|
2020-07-28 00:32:04 +08:00
|
|
|
const rules = ['transform', 'transition', 'animation']
|
|
|
|
const prefixes = ['ms-', 'webkit-']
|
2021-09-04 19:29:28 +08:00
|
|
|
rules.forEach((rule) => {
|
2020-07-28 00:32:04 +08:00
|
|
|
const value = style[rule]
|
|
|
|
if (rule && value) {
|
2021-09-04 19:29:28 +08:00
|
|
|
prefixes.forEach((prefix) => {
|
2020-07-28 00:32:04 +08:00
|
|
|
style[prefix + rule] = value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return style
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:38:30 +08:00
|
|
|
export const kebabCase = hyphenate
|
2020-07-28 00:32:04 +08:00
|
|
|
|
2020-09-14 10:03:33 +08:00
|
|
|
// reexport from lodash & vue shared
|
2021-09-09 11:49:48 +08:00
|
|
|
export { isVNode } from 'vue'
|
2020-07-28 00:32:04 +08:00
|
|
|
export {
|
2020-09-14 10:03:33 +08:00
|
|
|
hasOwn,
|
2020-09-30 18:21:47 +08:00
|
|
|
// isEmpty,
|
|
|
|
// isEqual,
|
2020-08-24 15:38:30 +08:00
|
|
|
isObject,
|
2020-09-14 10:03:33 +08:00
|
|
|
isArray,
|
2020-09-22 13:02:05 +08:00
|
|
|
isString,
|
2020-07-28 00:32:04 +08:00
|
|
|
capitalize,
|
2020-08-25 22:25:46 +08:00
|
|
|
camelize,
|
2020-08-24 15:38:30 +08:00
|
|
|
looseEqual,
|
|
|
|
extend,
|
2020-07-28 00:32:04 +08:00
|
|
|
}
|
|
|
|
|
2021-10-06 19:34:32 +08:00
|
|
|
export const isBool = (val: unknown): val is boolean => typeof val === 'boolean'
|
|
|
|
export const isNumber = (val: unknown): val is number => typeof val === 'number'
|
2020-09-14 10:03:33 +08:00
|
|
|
export const isHTMLElement = (val: unknown) => toRawType(val).startsWith('HTML')
|
|
|
|
|
2021-10-18 16:24:52 +08:00
|
|
|
export function rafThrottle<T extends (...args: any) => any>(fn: T): T {
|
2020-07-28 00:32:04 +08:00
|
|
|
let locked = false
|
2021-10-18 16:24:52 +08:00
|
|
|
return function (this: ThisParameterType<T>, ...args: any[]) {
|
2020-07-28 00:32:04 +08:00
|
|
|
if (locked) return
|
|
|
|
locked = true
|
2021-10-18 16:24:52 +08:00
|
|
|
|
2020-07-28 00:32:04 +08:00
|
|
|
window.requestAnimationFrame(() => {
|
2021-10-18 16:24:52 +08:00
|
|
|
Reflect.apply(fn, this, args)
|
2020-07-28 00:32:04 +08:00
|
|
|
locked = false
|
|
|
|
})
|
2021-10-18 16:24:52 +08:00
|
|
|
} as T
|
2020-07-28 00:32:04 +08:00
|
|
|
}
|
|
|
|
|
2020-09-09 21:18:08 +08:00
|
|
|
export const clearTimer = (timer: Ref<TimeoutHandle>) => {
|
|
|
|
clearTimeout(timer.value)
|
|
|
|
timer.value = null
|
|
|
|
}
|
|
|
|
|
2020-08-24 00:00:20 +08:00
|
|
|
/**
|
|
|
|
* 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))
|
|
|
|
}
|
|
|
|
|
2020-10-30 23:26:33 +08:00
|
|
|
export function isUndefined(val: any): val is undefined {
|
2021-09-04 19:29:28 +08:00
|
|
|
return val === undefined
|
2020-09-19 20:44:07 +08:00
|
|
|
}
|
|
|
|
|
2020-10-03 16:02:53 +08:00
|
|
|
export function useGlobalConfig() {
|
|
|
|
const vm: any = getCurrentInstance()
|
|
|
|
if ('$ELEMENT' in vm.proxy) {
|
|
|
|
return vm.proxy.$ELEMENT
|
|
|
|
}
|
|
|
|
return {}
|
|
|
|
}
|
2021-01-28 22:10:45 +08:00
|
|
|
|
2020-10-30 23:26:33 +08:00
|
|
|
export function isEmpty(val: unknown) {
|
|
|
|
if (
|
2021-09-04 19:29:28 +08:00
|
|
|
(!val && val !== 0) ||
|
|
|
|
(isArray(val) && !val.length) ||
|
|
|
|
(isObject(val) && !Object.keys(val).length)
|
|
|
|
)
|
|
|
|
return true
|
2020-10-30 23:26:33 +08:00
|
|
|
|
|
|
|
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<T>(arr: T[]) {
|
2020-11-24 01:03:30 +08:00
|
|
|
return Array.from(new Set(arr))
|
2020-10-30 23:26:33 +08:00
|
|
|
}
|
2020-11-10 14:55:03 +08:00
|
|
|
|
2021-01-28 22:10:45 +08:00
|
|
|
export function addUnit(value: string | number) {
|
|
|
|
if (isString(value)) {
|
|
|
|
return value
|
|
|
|
} else if (isNumber(value)) {
|
2021-09-17 09:18:24 +08:00
|
|
|
return `${value}px`
|
2021-01-28 22:10:45 +08:00
|
|
|
}
|
2021-09-09 11:49:48 +08:00
|
|
|
debugWarn(SCOPE, 'binding value must be a string or number')
|
2021-01-28 22:10:45 +08:00
|
|
|
return ''
|
|
|
|
}
|
2021-06-25 16:08:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enhance `lodash.isEqual` for it always return false even two functions have completely same statements.
|
|
|
|
* @param obj The value to compare
|
|
|
|
* @param other The other value to compare
|
|
|
|
* @returns Returns `true` if the values are equivalent, else `false`.
|
|
|
|
* @example
|
|
|
|
* lodash.isEqual(() => 1, () => 1) // false
|
|
|
|
* isEqualWith(() => 1, () => 1) // true
|
|
|
|
*/
|
2021-09-04 19:29:28 +08:00
|
|
|
export function isEqualWithFunction(obj: any, other: any) {
|
2021-06-25 16:08:00 +08:00
|
|
|
return isEqualWith(obj, other, (objVal, otherVal) => {
|
2021-09-04 19:29:28 +08:00
|
|
|
return isFunction(objVal) && isFunction(otherVal)
|
|
|
|
? `${objVal}` === `${otherVal}`
|
|
|
|
: undefined
|
2021-06-25 16:08:00 +08:00
|
|
|
})
|
|
|
|
}
|
2021-07-13 17:30:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate function for attach ref for the h renderer
|
|
|
|
* @param ref Ref<HTMLElement | ComponentPublicInstance>
|
|
|
|
* @returns (val: T) => void
|
|
|
|
*/
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
export const refAttacher = <T extends HTMLElement | ComponentPublicInstance>(
|
|
|
|
ref: Ref<T>
|
|
|
|
) => {
|
|
|
|
return (val: T) => {
|
|
|
|
ref.value = val
|
2021-07-13 17:30:41 +08:00
|
|
|
}
|
2021-09-04 19:29:28 +08:00
|
|
|
}
|