minimal adjustments
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / table / dist / makeStreamConfig.js.flow
1 import cloneDeep from 'lodash.clonedeep';
2 import getBorderCharacters from './getBorderCharacters';
3 import validateConfig from './validateConfig';
4
5 /**
6  * Merges user provided border characters with the default border ("honeywell") characters.
7  *
8  * @param {object} border
9  * @returns {object}
10  */
11 const makeBorder = (border = {}) => {
12   return Object.assign({}, getBorderCharacters('honeywell'), border);
13 };
14
15 /**
16  * Creates a configuration for every column using default
17  * values for the missing configuration properties.
18  *
19  * @param {number} columnCount
20  * @param {object} columns
21  * @param {object} columnDefault
22  * @returns {object}
23  */
24 const makeColumns = (columnCount, columns = {}, columnDefault = {}) => {
25   for (let index = 0; index < columnCount; index++) {
26     if (typeof columns[index] === 'undefined') {
27       columns[index] = {};
28     }
29
30     columns[index] = Object.assign({
31       alignment: 'left',
32       paddingLeft: 1,
33       paddingRight: 1,
34       truncate: Number.POSITIVE_INFINITY,
35       wrapWord: false,
36     }, columnDefault, columns[index]);
37   }
38
39   return columns;
40 };
41
42 /**
43  * @typedef {object} columnConfig
44  * @property {string} alignment
45  * @property {number} width
46  * @property {number} truncate
47  * @property {number} paddingLeft
48  * @property {number} paddingRight
49  */
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 /**
60  * Makes a new configuration object out of the userConfig object
61  * using default values for the missing configuration properties.
62  *
63  * @param {streamConfig} userConfig
64  * @returns {object}
65  */
66 export default (userConfig = {}) => {
67   validateConfig('streamConfig.json', userConfig);
68
69   const config = cloneDeep(userConfig);
70
71   if (!config.columnDefault || !config.columnDefault.width) {
72     throw new Error('Must provide config.columnDefault.width when creating a stream.');
73   }
74
75   if (!config.columnCount) {
76     throw new Error('Must provide config.columnCount.');
77   }
78
79   config.border = makeBorder(config.border);
80   config.columns = makeColumns(config.columnCount, config.columns, config.columnDefault);
81
82   return config;
83 };