Ohm-Management - Projektarbeit B-ME
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.

makeStreamConfig.js 2.6KB

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