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.flow 2.1KB

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