2020-08-26 01:57:08 +02:00
|
|
|
import _ from 'lodash';
|
|
|
|
import getBorderCharacters from './getBorderCharacters';
|
|
|
|
import validateConfig from './validateConfig';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges user provided border characters with the default border ("honeywell") characters.
|
|
|
|
*
|
2021-02-26 04:58:33 +01:00
|
|
|
* @param {object} border
|
|
|
|
* @returns {object}
|
2020-08-26 01:57:08 +02:00
|
|
|
*/
|
|
|
|
const makeBorder = (border = {}) => {
|
|
|
|
return Object.assign({}, getBorderCharacters('honeywell'), border);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a configuration for every column using default
|
|
|
|
* values for the missing configuration properties.
|
|
|
|
*
|
|
|
|
* @param {number} columnCount
|
2021-02-26 04:58:33 +01:00
|
|
|
* @param {object} columns
|
|
|
|
* @param {object} columnDefault
|
|
|
|
* @returns {object}
|
2020-08-26 01:57:08 +02:00
|
|
|
*/
|
|
|
|
const makeColumns = (columnCount, columns = {}, columnDefault = {}) => {
|
|
|
|
_.times(columnCount, (index) => {
|
|
|
|
if (_.isUndefined(columns[index])) {
|
|
|
|
columns[index] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
columns[index] = Object.assign({
|
|
|
|
alignment: 'left',
|
|
|
|
paddingLeft: 1,
|
|
|
|
paddingRight: 1,
|
2021-02-26 04:58:33 +01:00
|
|
|
truncate: Number.POSITIVE_INFINITY,
|
|
|
|
wrapWord: false,
|
2020-08-26 01:57:08 +02:00
|
|
|
}, columnDefault, columns[index]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return columns;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-02-26 04:58:33 +01:00
|
|
|
* @typedef {object} columnConfig
|
2020-08-26 01:57:08 +02:00
|
|
|
* @property {string} alignment
|
|
|
|
* @property {number} width
|
|
|
|
* @property {number} truncate
|
|
|
|
* @property {number} paddingLeft
|
|
|
|
* @property {number} paddingRight
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-02-26 04:58:33 +01:00
|
|
|
* @typedef {object} streamConfig
|
2020-08-26 01:57:08 +02:00
|
|
|
* @property {columnConfig} columnDefault
|
2021-02-26 04:58:33 +01:00
|
|
|
* @property {object} border
|
2020-08-26 01:57:08 +02:00
|
|
|
* @property {columnConfig[]}
|
|
|
|
* @property {number} columnCount Number of columns in the table (required).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a new configuration object out of the userConfig object
|
|
|
|
* using default values for the missing configuration properties.
|
|
|
|
*
|
|
|
|
* @param {streamConfig} userConfig
|
2021-02-26 04:58:33 +01:00
|
|
|
* @returns {object}
|
2020-08-26 01:57:08 +02:00
|
|
|
*/
|
|
|
|
export default (userConfig = {}) => {
|
|
|
|
validateConfig('streamConfig.json', userConfig);
|
|
|
|
|
|
|
|
const config = _.cloneDeep(userConfig);
|
|
|
|
|
|
|
|
if (!config.columnDefault || !config.columnDefault.width) {
|
|
|
|
throw new Error('Must provide config.columnDefault.width when creating a stream.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config.columnCount) {
|
|
|
|
throw new Error('Must provide config.columnCount.');
|
|
|
|
}
|
|
|
|
|
|
|
|
config.border = makeBorder(config.border);
|
|
|
|
config.columns = makeColumns(config.columnCount, config.columns, config.columnDefault);
|
|
|
|
|
|
|
|
return config;
|
|
|
|
};
|