All files / src/utils ObjectUtils.ts

94.33% Statements 50/53
87.5% Branches 35/40
92.3% Functions 12/13
94.33% Lines 50/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 121 122 123                  68x                   20x 5x   15x 3x   12x 34x 10x     2x               68x 6x               68x 91x     68x   12x 1x   11x 11x 11x 11x 11x 11x 11x     68x 1253x 1253x 1253x   1253x     68x 60x     68x 2x     68x       68x 30469x     68x 1x             68x   68x 13x     13x     68x 9x 4x   5x 3x   5x     68x 8x         68x
import { SpecialFrontendToBackTypeMapping } from "@config/base";
import { ExecResultProps, RecordProps, SaveRecordProps } from "@props/RecordProps";
import _ from "lodash";
 
/**
 * 如果后台在元数据中没有返回 labelField 属性,
 * 那么前台会按照本数组中的元素,依次寻找对象中的属性
 * 以最先找到的属性为显示的 Label
 */
const DefaultLabelField = ['label', 'name', 'title', 'authority', 'username', 'serialNumber'];
 
/**
 * 找到对于某个对象,应该显示的 label
 * 如果 labelField 为空,则会依次使用 DefaultLabelField 中的属性
 * 以最先找到的属性为显示的 Label
 * @param record 对象信息
 * @param labelField 后台的 domain 中配置的 labelField 属性
 */
export function getLabelToDisplay(record: RecordProps | SaveRecordProps, labelField?: string): string {
  if (record == null || !isObject(record)) {
    return "";
  }
  if (labelField) {
    return record[labelField];
  }
  for (const lf of DefaultLabelField) {
    if (lf in record && record[lf] != null) {
      return record[lf];
    }
  }
  return `${record.id}`;
}
 
/**
 * 判断一个对象是否包含至少一个 key
 * @param obj 待判断的对象
 */
// eslint-disable-next-line  @typescript-eslint/no-explicit-any
export const pureObjectIsEmpty = (obj: any): boolean => {
  return (obj == null) ||
    (obj && obj.constructor === Object && Object.keys(obj).length === 0);
};
 
/**
 * 判断一个变量是不是 Javascript 的 Object
 * @param obj 待判断的 Object
 */
export const isObject = (obj: unknown): boolean => {
  return Object.prototype.toString.call(obj) === '[object Object]';
};
 
export const getNumberOfLineBreaks = (
  result: ExecResultProps, fineTuningResult?: string, noAdditionalLines?: boolean): number => {
  if (result == null) {
    return 1;
  }
  const { execLog, execResult } = result;
  const v1 = execLog?.split("\n")?.length ?? 1;
  const v2 = execResult?.split("\n")?.length ?? 1;
  const v3 = fineTuningResult?.split("\n")?.length ?? 1;
  const otherLines = (noAdditionalLines) ? 0 : 8;
  const value = otherLines + v1 + v2 + v3;
  return (isNaN(value) || value == null) ? 1 : value;
};
 
export const stopPropagationAndPreventDefault = (event: React.MouseEvent<unknown>): void => {
  event.stopPropagation();
  Eif (event.nativeEvent) {
    event.nativeEvent.stopImmediatePropagation();
  }
  event.preventDefault();
};
 
export const isArrayEqual = (x: Array<unknown>, y: Array<unknown>): boolean => {
  return _.isEqual(x, y);
};
 
export const isSetEqual = (x: Set<unknown>, y: Set<unknown>): boolean => {
  return _.isEqual(x, y);
};
 
export const isObjectEqual = (x: object, y: object): boolean => {
  return _.isEqual(x, y);
};
 
export const getRawDomainName = (domainName: string): string => {
  return SpecialFrontendToBackTypeMapping[domainName] || domainName;
};
 
export const errorToObject = (error: Error): { message: string, name: string, stack: string | undefined } => {
  return {
    message: error.message,
    name: error.name,
    stack: error.stack
  };
};
 
const labelCache: Record<string, Record<number, string>> = {};
 
export const getObjectLabelFromCache = (domainName: string, id: number): string | undefined => {
  Iif (!id) {
    return undefined;
  }
  return labelCache[domainName]?.[id];
};
 
export const setObjectLabelToCache = (domainName: string, id: number, label: string): void => {
  if (!id) {
    return;
  }
  if (!labelCache[domainName]) {
    labelCache[domainName] = {};
  }
  labelCache[domainName][id] = label;
};
 
export const deleteLabelFromCache = (domainName: string, id: number): void => {
  Iif (labelCache[domainName]) {
    delete labelCache[domainName][id];
  }
};
 
window.__labelCache = labelCache;