| | |
| | | export const timestamp = () => +Date.now(); |
| | | import { isObject } from '/@/utils/is'; |
| | | export const clamp = (n: number, min: number, max: number) => Math.min(max, Math.max(min, n)); |
| | | import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router'; |
| | | import type { App, Component } from 'vue'; |
| | | |
| | | import { intersectionWith, isEqual, mergeWith, unionWith } from 'lodash-es'; |
| | | import { unref } from 'vue'; |
| | | import { isArray, isObject } from '@/utils/is'; |
| | | |
| | | export const noop = () => {}; |
| | | export const now = () => Date.now(); |
| | | |
| | | /** |
| | | * @description: Set ui mount node |
| | | */ |
| | | export function getPopupContainer(node?: HTMLElement): HTMLElement { |
| | | if (node) { |
| | | return node.parentNode as HTMLElement; |
| | | } |
| | | return document.body; |
| | | return (node?.parentNode as HTMLElement) ?? document.body; |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | export function setObjToUrlParams(baseUrl: string, obj: any): string { |
| | | let parameters = ''; |
| | | let url = ''; |
| | | for (const key in obj) { |
| | | parameters += key + '=' + encodeURIComponent(obj[key]) + '&'; |
| | | } |
| | | parameters = parameters.replace(/&$/, ''); |
| | | if (/\?$/.test(baseUrl)) { |
| | | url = baseUrl + parameters; |
| | | } else { |
| | | url = baseUrl.replace(/\/?$/, '?') + parameters; |
| | | } |
| | | return url; |
| | | } |
| | | |
| | | export function deepMerge<T = any>(src: any = {}, target: any = {}): T { |
| | | let key: string; |
| | | for (key in target) { |
| | | src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]); |
| | | } |
| | | return src; |
| | | return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters; |
| | | } |
| | | |
| | | /** |
| | | * @description: 根据数组中某个对象值去重 |
| | | * 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 unique<T = any>(arr: T[], key: string): T[] { |
| | | const map = new Map(); |
| | | return arr.filter((item) => { |
| | | const _item = item as any; |
| | | return !map.has(_item[key]) && map.set(_item[key], 1); |
| | | 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; |
| | | } |
| | | return mergeWith({}, source, target, (sourceValue, targetValue) => { |
| | | if (isArray(targetValue) && isArray(sourceValue)) { |
| | | switch (mergeArrays) { |
| | | case 'union': |
| | | return unionWith(sourceValue, targetValue, isEqual); |
| | | case 'intersection': |
| | | return intersectionWith(sourceValue, targetValue, isEqual); |
| | | case 'concat': |
| | | return sourceValue.concat(targetValue); |
| | | case 'replace': |
| | | return targetValue; |
| | | default: |
| | | throw new Error(`Unknown merge array strategy: ${mergeArrays as string}`); |
| | | } |
| | | } |
| | | if (isObject(targetValue) && isObject(sourceValue)) { |
| | | return deepMerge(sourceValue, targetValue, mergeArrays); |
| | | } |
| | | return undefined; |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * @description: es6数组去重复 |
| | | */ |
| | | export function es6Unique<T>(arr: T[]): T[] { |
| | | return Array.from(new Set(arr)); |
| | | } |
| | | |
| | | export function openWindow( |
| | | url: string, |
| | | opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean } |
| | | opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }, |
| | | ) { |
| | | const { target = '__blank', noopener = true, noreferrer = true } = opt || {}; |
| | | const feature: string[] = []; |
| | |
| | | |
| | | 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).forEach((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>; |
| | | }; |