UNPKG

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