index.ts 3.2 KB
Newer Older
V
vben 已提交
1
export const timestamp = () => +Date.now();
V
vben 已提交
2
import { unref } from 'vue';
V
vben 已提交
3
import { isObject } from '/@/utils/is';
V
vben 已提交
4 5 6
export const clamp = (n: number, min: number, max: number) => Math.min(max, Math.max(min, n));
export const noop = () => {};
export const now = () => Date.now();
陈文彬 已提交
7 8 9 10
/**
 * @description:  Set ui mount node
 */
export function getPopupContainer(node?: HTMLElement): HTMLElement {
V
vben 已提交
11
  return (node?.parentNode as HTMLElement) ?? document.body;
陈文彬 已提交
12
}
V
vben 已提交
13

陈文彬 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/**
 * 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(/&$/, '');
30
  return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
陈文彬 已提交
31 32
}

V
vben 已提交
33
export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
陈文彬 已提交
34 35
  let key: string;
  for (key in target) {
V
vben 已提交
36
    src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]);
陈文彬 已提交
37 38 39 40 41
  }
  return src;
}

/**
42
 * @description: Deduplication according to the value of an object in the array
陈文彬 已提交
43 44 45 46 47 48 49 50 51 52
 */
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);
  });
}

/**
53
 * @description: es6 array to repeat
陈文彬 已提交
54 55 56 57
 */
export function es6Unique<T>(arr: T[]): T[] {
  return Array.from(new Set(arr));
}
V
vben 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70

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(','));
}
V
vben 已提交
71 72 73 74 75 76 77 78 79 80 81

// dynamic use hook props
export function getDynamicProps<T, 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>;
}
V
vben 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95

export function getLastItem<T extends any>(list: T) {
  if (Array.isArray(list)) {
    return list.slice(-1)[0];
  }

  if (list instanceof Set) {
    return Array.from(list).slice(-1)[0];
  }

  if (list instanceof Map) {
    return Array.from(list.values()).slice(-1)[0];
  }
}
V
vben 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

/**
 * set page Title
 * @param {*} title  :page Title
 */
function setDocumentTitle(title: string) {
  document.title = title;
  const ua = navigator.userAgent;
  const regex = /\bMicroMessenger\/([\d.]+)/;
  // 兼容
  if (regex.test(ua) && /ip(hone|od|ad)/i.test(ua)) {
    const i = document.createElement('iframe');
    i.src = '/favicon.ico';
    i.style.display = 'none';
    i.onload = function () {
      setTimeout(function () {
        i.remove();
      }, 9);
    };
    document.body.appendChild(i);
  }
}

export function setTitle(title: string, appTitle?: string) {
  if (title) {
V
vben 已提交
121
    const _title = title ? ` ${title} - ${appTitle} ` : `${appTitle}`;
V
vben 已提交
122 123 124
    setDocumentTitle(_title);
  }
}