utils.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import _ from 'lodash';
  2. // import { BillboardType, HeaderConfig, ListConfig, TableConfig, TitleConfig } from './billboard.data';
  3. /**
  4. * 功能类似 lodash.get 但是当取值为 null 时也直接返回默认值
  5. */
  6. export function get(object?: Object, path?: Array<string> | string, defaultValue: any = '/') {
  7. if (!object || path === undefined) return defaultValue;
  8. const d = _.get(object, path, defaultValue);
  9. return d === null ? defaultValue : d;
  10. }
  11. // /** 依据配置获取适用于 CommonTitle 的数据 */
  12. // export function parseTitleConfig(data: BillboardType, config: TitleConfig) {
  13. // const val = get(data, config.prop);
  14. // return {
  15. // label: config.label,
  16. // value: config.translation ? get(config.translation, val) : val,
  17. // };
  18. // }
  19. // /** 依据配置获取适用于 LargeBoard 的数据 */
  20. // export function parseHeaderConfigs(data: BillboardType, configs: HeaderConfig[]) {
  21. // const types = ['to-top-right', 'to-bottom-right'];
  22. // return configs.map((cfg, i) => {
  23. // const val = get(data, cfg.prop);
  24. // return {
  25. // label: cfg.label,
  26. // value: cfg.translation ? get(cfg.translation, val) : val,
  27. // type: types[i % configs.length],
  28. // };
  29. // });
  30. // }
  31. // /** 依据配置获取适用于 CommonTable、CollapseTable 的数据 */
  32. // export function parseTableConfig(data: BillboardType, config: TableConfig) {
  33. // const arr = get(data, config.prop, []);
  34. // if (config.translation) {
  35. // const finalRes = arr.reduce((res: any[], ele) => {
  36. // // 对于表格里的每条数据,如果需要翻译则翻译之
  37. // config.columns.forEach((col) => {
  38. // if (col._t) {
  39. // const v = get(ele, col.prop);
  40. // _.set(ele, col.prop, get(config.translation, v));
  41. // }
  42. // });
  43. // config.collapses.forEach((col) => {
  44. // if (col._t) {
  45. // const v = get(ele, col.prop);
  46. // _.set(ele, col.prop, get(config.translation, v));
  47. // }
  48. // });
  49. // res.push(ele);
  50. // return res;
  51. // }, []);
  52. // return {
  53. // data: finalRes,
  54. // collapses: config.collapses,
  55. // columns: config.columns,
  56. // };
  57. // } else {
  58. // return {
  59. // data,
  60. // collapses: config.collapses,
  61. // columns: config.columns,
  62. // };
  63. // }
  64. // }
  65. // /** 依据配置获取适用于 ListItem 的数据 */
  66. // export function parseListConfigs(data: BillboardType, configs: ListConfig[]) {
  67. // const types = ['blue', 'green'];
  68. // return configs.map((cfg, i) => {
  69. // const val = get(data, cfg.prop);
  70. // return {
  71. // icon: cfg.icon,
  72. // label: cfg.label,
  73. // value: cfg.translation ? get(cfg.translation, val) : val,
  74. // type: types[i % configs.length],
  75. // collapses: cfg.collapses.map((col) => {
  76. // return {
  77. // label: col.label,
  78. // };
  79. // }),
  80. // };
  81. // });
  82. // }