makeStreamConfig.js
2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _getBorderCharacters = require('./getBorderCharacters');
var _getBorderCharacters2 = _interopRequireDefault(_getBorderCharacters);
var _validateConfig = require('./validateConfig');
var _validateConfig2 = _interopRequireDefault(_validateConfig);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Merges user provided border characters with the default border ("honeywell") characters.
*
* @param {Object} border
* @returns {Object}
*/
const makeBorder = function makeBorder() {
let border = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _lodash2.default.assign({}, (0, _getBorderCharacters2.default)('honeywell'), border);
};
/**
* Creates a configuration for every column using default
* values for the missing configuration properties.
*
* @param {number} columnCount
* @param {Object} columns
* @param {Object} columnDefault
* @returns {Object}
*/
const makeColumns = function makeColumns(columnCount) {
let columns = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
let columnDefault = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
_lodash2.default.times(columnCount, index => {
if (_lodash2.default.isUndefined(columns[index])) {
columns[index] = {};
}
columns[index] = _lodash2.default.assign({
alignment: 'left',
paddingLeft: 1,
paddingRight: 1,
truncate: Infinity,
wrapWord: false
}, columnDefault, columns[index]);
});
return columns;
};
/**
* @typedef {Object} columnConfig
* @property {string} alignment
* @property {number} width
* @property {number} truncate
* @property {number} paddingLeft
* @property {number} paddingRight
*/
/**
* @typedef {Object} streamConfig
* @property {columnConfig} columnDefault
* @property {Object} border
* @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
* @returns {Object}
*/
exports.default = function () {
let userConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
(0, _validateConfig2.default)('streamConfig.json', userConfig);
const config = _lodash2.default.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;
};
module.exports = exports['default'];