对象方法

/**
 * @description: 处理数据(格式转换)
 * @param {*} obj { x0: [11, 12, 13],x1: [21, 22, 23] }
 * @return {*} data 表格正常数据格式
 */
export function dealData(obj) {
  const keys = Object.keys(obj);
  const data = [];
  for (let i = 0; i < obj[keys[0]].length; i++) {
    const myObj = {};
    for (const key of keys) {
      myObj[key] = obj[key][i];
    }
    data.push(myObj as never);
  }
  return data;
}
 
let obj =  {
        id: [1", "7", "44", "48", "56", "58", "66", "67", "71", "72", "74"],
        x0: [1.805927, 0.163763, -0.008116, -0.519609, 2.044072, -0.422281, -1.213336, -0.815737 ],
        x1: [1.805927, 0.163763, -0.008116, -0.519609, 2.044072, -0.422281, -1.213336, -0.815737 ]
      }
dealData(obj)
 
/**
 * @description: 根据任务类型获取 direction
 * @param {string} type 任务类型
 * @return {*}
 */
export function getDirectionByTaskType(type: string) {
  let direction = 0;
  switch (type) {
    case '纵向':
      direction = 0;
      break;
    case '横向':
      direction = 1;
      break;
    case '查询':
      direction = 2;
      break;
    case '预测':
      direction = 3;
      break;
    default:
      0;
  }

  return direction;
}
 
export function lighten(color: string, amount: number) {
  color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color;
  amount = Math.trunc((255 * amount) / 100);
  return `#${addLight(color.substring(0, 2), amount)}${addLight(
    color.substring(2, 4),
    amount,
  )}${addLight(color.substring(4, 6), amount)}`;
}

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(','));
}

/**
 * 处理css单位
 * */
export function cssUnit(value: string | number, unit = 'px') {
  return isNumber(value) || (isString(value) && value.indexOf(unit as string) === -1)
    ? `${value}${unit}`
    : value;
}

/**
 * 判断是否 url
 * */
export function isUrl(url: string) {
  return /^(http|https):\/\//g.test(url);
}

/**
 * 判断是否为数值
 * */
export function isNumberCustom(num: any) {
  return !isNaN(parseFloat(num)) && isFinite(num);
}

/**
 * 判断是否为日期
 * */
export function isDateCustom(date: any) {
  return isNaN(date as any) && !isNaN(Date.parse(date));
}

/**
 * @description: h函数渲染阿里图标
 * @param {string} name 图标名称
 * @return {*}
 */
export function renderALIcon(name: string) {
  return () =>
    h(Icon, {
      name,
    });
}

/**
 * @description: 生成选项数组结构
 * @param {*} arr 枚举值数组
 * @param {*} data 数据源
 * @param {*} key
 * @return {*}
 */
export function createOptions(arr: [], data: any[], key: string) {
  const filterOptions = [];

  arr.forEach((v) => {
    filterOptions.push({
      label: `${v} (${data.filter((item) => item[key] == v).length})`,
      value: v,
    });
  });
  return filterOptions || [];
}
 
posted @ 2022-11-28 15:29  xiaoxiao95  阅读(30)  评论(0编辑  收藏  举报