You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

format.ts 1.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * @Author: 赵伟
  3. * @Date: 2024-11-29 09:27:19
  4. * @Description: 用于 BasicInfo 和 BasicTableInfo 组件的常用转化格式
  5. */
  6. // 格式化日期
  7. export { formatDate } from '@/utils/date';
  8. /**
  9. * 格式化字符串数组
  10. * @param value - 字符串数组
  11. * @returns 逗号分隔的字符串
  12. */
  13. export const formatList = (value: string[] | null | undefined): string => {
  14. if (
  15. value === undefined ||
  16. value === null ||
  17. Array.isArray(value) === false ||
  18. value.length === 0
  19. ) {
  20. return '--';
  21. }
  22. return value.join(',');
  23. };
  24. /**
  25. * 格式化布尔值
  26. * @param value - 布尔值
  27. * @returns "是" 或 "否"
  28. */
  29. export const formatBoolean = (value: boolean): string => {
  30. return value ? '是' : '否';
  31. };
  32. type FormatEnum = (value: string | number) => string;
  33. /**
  34. * 格式化枚举
  35. * @param options - 枚举选项
  36. * @returns 格式化枚举函数
  37. */
  38. export const formatEnum = (options: { value: string | number; label: string }[]): FormatEnum => {
  39. return (value: string | number) => {
  40. const option = options.find((item) => item.value === value);
  41. return option ? option.label : '--';
  42. };
  43. };