You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

171 lines
5.2 KiB

import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router'
import type { App, Component } from 'vue'
2 years ago
import { intersectionWith, isEqual, mergeWith, unionWith } from 'lodash-es'
2 years ago
import { unref } from 'vue'
import { isArray, isObject } from '@/utils/is'
2 years ago
export const noop = () => {}
/**
* @description: Set ui mount node
*/
export function getPopupContainer(node?: HTMLElement): HTMLElement {
return (node?.parentNode as HTMLElement) ?? document.body
}
/**
* Add the object as a parameter to the URL
* @param baseUrl url
* @param obj
* @returns {string}
* eg:
* let obj = {a: '3', b: '4'}
* setObjToUrlParams('www.baidu.com', obj)
* ==>www.baidu.com?a=3&b=4
*/
export function setObjToUrlParams(baseUrl: string, obj: any): string {
let parameters = ''
for (const key in obj) {
parameters += key + '=' + encodeURIComponent(obj[key]) + '&'
}
parameters = parameters.replace(/&$/, '')
return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters
}
/**
* Recursively merge two objects.
*
*
* @param source The source object to merge from.
* @param target The target object to merge into.
* @param mergeArrays How to merge arrays. Default is "replace".
* replace
* - "union": Union the arrays.
* - "intersection": Intersect the arrays.
* - "concat": Concatenate the arrays.
* - "replace": Replace the source array with the target array.
* @returns The merged object.
*/
export function deepMerge<T extends object | null | undefined, U extends object | null | undefined>(
source: T,
target: U,
mergeArrays: 'union' | 'intersection' | 'concat' | 'replace' = 'replace'
): T & U {
if (!target) {
return source as T & U
}
if (!source) {
return target as T & U
}
if (isArray(target) && isArray(source)) {
switch (mergeArrays) {
case 'union':
return unionWith(target, source, isEqual) as T & U
case 'intersection':
return intersectionWith(target, source, isEqual) as T & U
case 'concat':
return target.concat(source) as T & U
case 'replace':
return source as T & U
default:
throw new Error(`Unknown merge array strategy: ${mergeArrays}`)
}
}
if (isObject(target) && isObject(source)) {
return mergeWith({}, target, source, (targetValue, sourceValue) => {
return deepMerge(targetValue, sourceValue, mergeArrays)
}) as T & U
}
return source as T & U
2 years ago
}
export function openWindow(url: string, opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }) {
const { target = '__blank', noopener = true, noreferrer = true } = opt || {}
const feature: string[] = []
noopener && feature.push('noopener=yes')
noreferrer && feature.push('noreferrer=yes')
window.open(url, target, feature.join(','))
}
// dynamic use hook props
export function getDynamicProps<T extends Record<string, unknown>, U>(props: T): Partial<U> {
const ret: Recordable = {}
Object.keys(props).map((key) => {
ret[key] = unref((props as Recordable)[key])
})
return ret as Partial<U>
}
export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
if (!route) return route
const { matched, ...opt } = route
return {
...opt,
matched: (matched
? matched.map((item) => ({
meta: item.meta,
name: item.name,
path: item.path
}))
: undefined) as RouteRecordNormalized[]
}
}
// https://github.com/vant-ui/vant/issues/8302
type EventShim = {
new (...args: any[]): {
$props: {
onClick?: (...args: any[]) => void
}
}
}
export type WithInstall<T> = T & {
install(app: App): void
} & EventShim
export type CustomComponent = Component & { displayName?: string }
export const withInstall = <T extends CustomComponent>(component: T, alias?: string) => {
;(component as Record<string, unknown>).install = (app: App) => {
const compName = component.name || component.displayName
if (!compName) return
app.component(compName, component)
if (alias) {
app.config.globalProperties[alias] = component
}
}
return component as WithInstall<T>
}
2 years ago
/**
*
*
* (debounce)(delay)100ms
* 100ms内再次执行函数
*
*
* @param fn
* @param delay
* @returns {Function}
*/
export function simpleDebounce(fn, delay = 100) {
let timer: any | null = null
return function () {
// eslint-disable-next-line prefer-rest-params
const args = arguments
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
// @ts-ignore
fn.apply(this, args)
}, delay)
}
}