2020-07-28 00:32:04 +08:00
|
|
|
import isServer from './isServer'
|
2020-09-09 21:18:08 +08:00
|
|
|
|
2020-08-25 22:25:46 +08:00
|
|
|
import { isObject, capitalize, hyphenate, looseEqual, extend, camelize } from '@vue/shared'
|
2020-08-18 20:23:39 +08:00
|
|
|
import { isEmpty, castArray, isEqual } from 'lodash'
|
2020-07-28 00:32:04 +08:00
|
|
|
|
2020-08-24 15:38:30 +08:00
|
|
|
import type { AnyFunction } from './types'
|
2020-09-09 21:18:08 +08:00
|
|
|
import type { Ref } from 'vue'
|
2020-07-28 00:32:04 +08:00
|
|
|
|
2020-09-15 10:50:32 +08:00
|
|
|
export type PartialCSSStyleDeclaration = Partial<
|
|
|
|
Pick<CSSStyleDeclaration, 'transform' | 'transition' | 'animation'>
|
|
|
|
>
|
2020-08-24 15:38:30 +08:00
|
|
|
const { hasOwnProperty } = Object.prototype
|
2020-07-28 00:32:04 +08:00
|
|
|
|
2020-08-24 15:38:30 +08:00
|
|
|
export function hasOwn(obj: any, key: string): boolean {
|
2020-07-28 00:32:04 +08:00
|
|
|
return hasOwnProperty.call(obj, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:38:30 +08:00
|
|
|
export const getValueByPath = (obj: any, paths = ''): unknown => {
|
2020-07-28 00:32:04 +08:00
|
|
|
let ret: unknown = obj
|
|
|
|
paths.split('.').map(path => {
|
|
|
|
ret = ret?.[path]
|
|
|
|
})
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:38:30 +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
|
2020-07-28 00:32:04 +08:00
|
|
|
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]
|
|
|
|
tempObj = tempObj?.[key]
|
|
|
|
if (!tempObj && strict) {
|
|
|
|
throw new Error('please transfer a valid prop path to form item!')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
o: tempObj,
|
|
|
|
k: keyArr[i],
|
|
|
|
v: tempObj?.[keyArr[i]],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
export const coerceTruthyValueToArray = castArray
|
|
|
|
|
|
|
|
export const isIE = function(): boolean {
|
|
|
|
return !isServer && !isNaN(Number(document.DOCUMENT_NODE))
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isEdge = function(): boolean {
|
|
|
|
return !isServer && navigator.userAgent.indexOf('Edge') > -1
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-15 10:50:32 +08:00
|
|
|
export const autoprefixer = function(
|
|
|
|
style: PartialCSSStyleDeclaration,
|
|
|
|
): PartialCSSStyleDeclaration {
|
2020-07-28 00:32:04 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:38:30 +08:00
|
|
|
export const kebabCase = hyphenate
|
2020-07-28 00:32:04 +08:00
|
|
|
|
|
|
|
// reexport from lodash
|
|
|
|
export {
|
|
|
|
isEmpty,
|
|
|
|
isEqual,
|
2020-08-24 15:38:30 +08:00
|
|
|
isObject,
|
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
|
|
|
}
|
|
|
|
|
2020-08-24 15:38:30 +08:00
|
|
|
export function rafThrottle<T extends AnyFunction<any>>(fn: T): AnyFunction<void> {
|
2020-07-28 00:32:04 +08:00
|
|
|
let locked = false
|
2020-08-24 15:38:30 +08:00
|
|
|
return function(...args: any[]) {
|
2020-07-28 00:32:04 +08:00
|
|
|
if (locked) return
|
|
|
|
locked = true
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
fn.apply(this, args)
|
|
|
|
locked = false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const objToArray = castArray
|
2020-07-29 15:09:29 +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-09-11 15:17:40 +08:00
|
|
|
export function entries<T>(obj: Hash<T>): [string, T][] {
|
|
|
|
return Object
|
|
|
|
.keys(obj)
|
|
|
|
.map((key: string) => ([key, obj[key]]))
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:09:29 +08:00
|
|
|
export { isVNode } from 'vue'
|