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

93 lines
2.5 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _cloneDeep2 = _interopRequireDefault(require("lodash/cloneDeep"));
  7. var _isUndefined2 = _interopRequireDefault(require("lodash/isUndefined"));
  8. var _times2 = _interopRequireDefault(require("lodash/times"));
  9. var _getBorderCharacters = _interopRequireDefault(require("./getBorderCharacters"));
  10. var _validateConfig = _interopRequireDefault(require("./validateConfig"));
  11. var _calculateMaximumColumnWidthIndex = _interopRequireDefault(require("./calculateMaximumColumnWidthIndex"));
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /**
  14. * Merges user provided border characters with the default border ("honeywell") characters.
  15. *
  16. * @param {Object} border
  17. * @returns {Object}
  18. */
  19. const makeBorder = (border = {}) => {
  20. return Object.assign({}, (0, _getBorderCharacters.default)('honeywell'), border);
  21. };
  22. /**
  23. * Creates a configuration for every column using default
  24. * values for the missing configuration properties.
  25. *
  26. * @param {Array[]} rows
  27. * @param {Object} columns
  28. * @param {Object} columnDefault
  29. * @returns {Object}
  30. */
  31. const makeColumns = (rows, columns = {}, columnDefault = {}) => {
  32. const maximumColumnWidthIndex = (0, _calculateMaximumColumnWidthIndex.default)(rows);
  33. (0, _times2.default)(rows[0].length, index => {
  34. if ((0, _isUndefined2.default)(columns[index])) {
  35. columns[index] = {};
  36. }
  37. columns[index] = Object.assign({
  38. alignment: 'left',
  39. paddingLeft: 1,
  40. paddingRight: 1,
  41. truncate: Infinity,
  42. width: maximumColumnWidthIndex[index],
  43. wrapWord: false
  44. }, columnDefault, columns[index]);
  45. });
  46. return columns;
  47. };
  48. /**
  49. * Makes a new configuration object out of the userConfig object
  50. * using default values for the missing configuration properties.
  51. *
  52. * @param {Array[]} rows
  53. * @param {Object} userConfig
  54. * @returns {Object}
  55. */
  56. const makeConfig = (rows, userConfig = {}) => {
  57. (0, _validateConfig.default)('config.json', userConfig);
  58. const config = (0, _cloneDeep2.default)(userConfig);
  59. config.border = makeBorder(config.border);
  60. config.columns = makeColumns(rows, config.columns, config.columnDefault);
  61. if (!config.drawHorizontalLine) {
  62. /**
  63. * @returns {boolean}
  64. */
  65. config.drawHorizontalLine = () => {
  66. return true;
  67. };
  68. }
  69. if (config.singleLine === undefined) {
  70. config.singleLine = false;
  71. }
  72. return config;
  73. };
  74. var _default = makeConfig;
  75. exports.default = _default;
  76. //# sourceMappingURL=makeConfig.js.map