All files / src/utils CsvUtils.ts

100% Statements 15/15
100% Branches 4/4
100% Functions 8/8
100% Lines 15/15

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              66x           3x 9x 4x 3x 3x       5x   5x 12x 4x   3x 3x 3x 3x   5x    
import { ActionColumnKey, IdColumnKey, IdColumnKeyInCsv } from '@config/base';
import { isObjectType } from "@utils/ColumnsUtils";
import { TableMetaProps } from "../props/RecordProps";
import i18n from "@config/i18n";
import { capitalizeFirstLetter, typeWithPackageToSimpleType } from '@utils/StringUtils';
 
/** 下载 CSV 导入模板时候要隐藏的列 */
export const HideColumns = [
  "lastUpdated", "version", "lastUpdatedBy", "createdBy", "dateCreated", ActionColumnKey, IdColumnKey
];
 
export function getCsvHeader(columns: Array<TableMetaProps>): Array<string> {
  // ID 列单独处理放在第一列,所以后续循环中要排除 ID 列
  const headers = [IdColumnKeyInCsv].concat(columns
    .filter(c => !HideColumns.includes(c.key))
    .filter(c => c.type !== 'array')
    .map(c => isObjectType(c.type) ? `${c.key}.${c.labelField}` : `${c.key}`));
  return headers;
}
 
export function getCsvHeaderTranslated(domainName: string, columns: Array<TableMetaProps>): Array<string> {
  const simpleDomainName: string = capitalizeFirstLetter(typeWithPackageToSimpleType(domainName));
  // ID 列单独处理放在第一列,所以后续循环中要排除 ID 列
  const headers = ["; " + IdColumnKeyInCsv].concat(columns
    .filter(c => !HideColumns.includes(c.key))
    .filter(c => c.type !== 'array')
    .map(c => {
      const columnTitle = i18n.t(`field:${simpleDomainName}.${c.key}`);
      const labelTitle = i18n.t(`field:${simpleDomainName}.${c.labelField}`);
      const linkPhrase = i18n.t("objectFieldLinkPhrase");
      return isObjectType(c.type) ? `${columnTitle}${linkPhrase}${labelTitle}` : `${columnTitle}`;
    }));
  return headers;
}