vben
2021-01-01 0419a070413be34ea5455ed955fa51d8c522da86
提交 | 用户 | age
35d2bf 1 export const timestamp = () => +Date.now();
a305e5 2 import { unref } from 'vue';
498278 3 import { isObject } from '/@/utils/is';
35d2bf 4 export const clamp = (n: number, min: number, max: number) => Math.min(max, Math.max(min, n));
V 5 export const noop = () => {};
6 export const now = () => Date.now();
2f6253 7 /**
8  * @description:  Set ui mount node
9  */
10 export function getPopupContainer(node?: HTMLElement): HTMLElement {
116a1f 11   return (node?.parentNode as HTMLElement) ?? document.body;
2f6253 12 }
ba068b 13
2f6253 14 /**
15  * Add the object as a parameter to the URL
16  * @param baseUrl url
17  * @param obj
18  * @returns {string}
19  * eg:
20  *  let obj = {a: '3', b: '4'}
21  *  setObjToUrlParams('www.baidu.com', obj)
22  *  ==>www.baidu.com?a=3&b=4
23  */
24 export function setObjToUrlParams(baseUrl: string, obj: any): string {
25   let parameters = '';
26   let url = '';
27   for (const key in obj) {
28     parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
29   }
30   parameters = parameters.replace(/&$/, '');
31   if (/\?$/.test(baseUrl)) {
32     url = baseUrl + parameters;
33   } else {
34     url = baseUrl.replace(/\/?$/, '?') + parameters;
35   }
36   return url;
37 }
38
4ff1c4 39 export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
2f6253 40   let key: string;
41   for (key in target) {
498278 42     src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]);
2f6253 43   }
44   return src;
45 }
46
47 /**
48  * @description: 根据数组中某个对象值去重
49  */
50 export function unique<T = any>(arr: T[], key: string): T[] {
51   const map = new Map();
52   return arr.filter((item) => {
53     const _item = item as any;
54     return !map.has(_item[key]) && map.set(_item[key], 1);
55   });
56 }
57
58 /**
59  * @description: es6数组去重复
60  */
61 export function es6Unique<T>(arr: T[]): T[] {
62   return Array.from(new Set(arr));
63 }
ba068b 64
V 65 export function openWindow(
66   url: string,
67   opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }
68 ) {
69   const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
70   const feature: string[] = [];
71
72   noopener && feature.push('noopener=yes');
73   noreferrer && feature.push('noreferrer=yes');
74
75   window.open(url, target, feature.join(','));
76 }
a305e5 77
V 78 // dynamic use hook props
79 export function getDynamicProps<T, U>(props: T): Partial<U> {
80   const ret: Recordable = {};
81
82   Object.keys(props).map((key) => {
83     ret[key] = unref((props as Recordable)[key]);
84   });
85
86   return ret as Partial<U>;
87 }