makeStreamConfig.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * Merges user provided border characters with the default border ("honeywell") characters.
  14. *
  15. * @param {Object} border
  16. * @returns {Object}
  17. */
  18. const makeBorder = (border = {}) => {
  19. return Object.assign({}, (0, _getBorderCharacters.default)('honeywell'), border);
  20. };
  21. /**
  22. * Creates a configuration for every column using default
  23. * values for the missing configuration properties.
  24. *
  25. * @param {number} columnCount
  26. * @param {Object} columns
  27. * @param {Object} columnDefault
  28. * @returns {Object}
  29. */
  30. const makeColumns = (columnCount, columns = {}, columnDefault = {}) => {
  31. (0, _times2.default)(columnCount, index => {
  32. if ((0, _isUndefined2.default)(columns[index])) {
  33. columns[index] = {};
  34. }
  35. columns[index] = Object.assign({
  36. alignment: 'left',
  37. paddingLeft: 1,
  38. paddingRight: 1,
  39. truncate: Infinity,
  40. wrapWord: false
  41. }, columnDefault, columns[index]);
  42. });
  43. return columns;
  44. };
  45. /**
  46. * @typedef {Object} columnConfig
  47. * @property {string} alignment
  48. * @property {number} width
  49. * @property {number} truncate
  50. * @property {number} paddingLeft
  51. * @property {number} paddingRight
  52. */
  53. /**
  54. * @typedef {Object} streamConfig
  55. * @property {columnConfig} columnDefault
  56. * @property {Object} border
  57. * @property {columnConfig[]}
  58. * @property {number} columnCount Number of columns in the table (required).
  59. */
  60. /**
  61. * Makes a new configuration object out of the userConfig object
  62. * using default values for the missing configuration properties.
  63. *
  64. * @param {streamConfig} userConfig
  65. * @returns {Object}
  66. */
  67. const makeStreamConfig = (userConfig = {}) => {
  68. (0, _validateConfig.default)('streamConfig.json', userConfig);
  69. const config = (0, _cloneDeep2.default)(userConfig);
  70. if (!config.columnDefault || !config.columnDefault.width) {
  71. throw new Error('Must provide config.columnDefault.width when creating a stream.');
  72. }
  73. if (!config.columnCount) {
  74. throw new Error('Must provide config.columnCount.');
  75. }
  76. config.border = makeBorder(config.border);
  77. config.columns = makeColumns(config.columnCount, config.columns, config.columnDefault);
  78. return config;
  79. };
  80. var _default = makeStreamConfig;
  81. exports.default = _default;
  82. //# sourceMappingURL=makeStreamConfig.js.map