makeTableConfig.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.makeTableConfig = void 0;
  7. const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
  8. const calculateColumnWidths_1 = __importDefault(require("./calculateColumnWidths"));
  9. const utils_1 = require("./utils");
  10. const validateConfig_1 = require("./validateConfig");
  11. /**
  12. * Creates a configuration for every column using default
  13. * values for the missing configuration properties.
  14. */
  15. const makeColumnsConfig = (rows, columns, columnDefault) => {
  16. const columnWidths = calculateColumnWidths_1.default(rows);
  17. return rows[0].map((_, columnIndex) => {
  18. return {
  19. alignment: 'left',
  20. paddingLeft: 1,
  21. paddingRight: 1,
  22. truncate: Number.POSITIVE_INFINITY,
  23. verticalAlignment: 'top',
  24. width: columnWidths[columnIndex],
  25. wrapWord: false,
  26. ...columnDefault,
  27. ...columns === null || columns === void 0 ? void 0 : columns[columnIndex],
  28. };
  29. });
  30. };
  31. const makeHeaderConfig = (config) => {
  32. if (!config.header) {
  33. return undefined;
  34. }
  35. return {
  36. alignment: 'center',
  37. paddingLeft: 1,
  38. paddingRight: 1,
  39. truncate: Number.POSITIVE_INFINITY,
  40. wrapWord: false,
  41. ...config.header,
  42. };
  43. };
  44. /**
  45. * Makes a new configuration object out of the userConfig object
  46. * using default values for the missing configuration properties.
  47. */
  48. const makeTableConfig = (rows, userConfig = {}) => {
  49. var _a, _b, _c;
  50. validateConfig_1.validateConfig('config.json', userConfig);
  51. const config = lodash_clonedeep_1.default(userConfig);
  52. return {
  53. ...config,
  54. border: utils_1.makeBorderConfig(config.border),
  55. columns: makeColumnsConfig(rows, config.columns, config.columnDefault),
  56. drawHorizontalLine: (_a = config.drawHorizontalLine) !== null && _a !== void 0 ? _a : (() => {
  57. return true;
  58. }),
  59. drawVerticalLine: (_b = config.drawVerticalLine) !== null && _b !== void 0 ? _b : (() => {
  60. return true;
  61. }),
  62. header: makeHeaderConfig(config),
  63. singleLine: (_c = config.singleLine) !== null && _c !== void 0 ? _c : false,
  64. };
  65. };
  66. exports.makeTableConfig = makeTableConfig;