table 6.6.0 → 6.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  <a name="table"></a>
2
2
  # Table
3
3
 
4
+ > Produces a string that represents array data in a text table.
5
+
4
6
  [![Travis build status](http://img.shields.io/travis/gajus/table/master.svg?style=flat-square)](https://travis-ci.org/gajus/table)
5
7
  [![Coveralls](https://img.shields.io/coveralls/gajus/table.svg?style=flat-square)](https://coveralls.io/github/gajus/table)
6
8
  [![NPM version](http://img.shields.io/npm/v/table.svg?style=flat-square)](https://www.npmjs.org/package/table)
@@ -11,20 +13,11 @@
11
13
  * [Features](#table-features)
12
14
  * [Install](#table-install)
13
15
  * [Usage](#table-usage)
14
- * [Cell Content Alignment](#table-usage-cell-content-alignment)
15
- * [Column Width](#table-usage-column-width)
16
- * [Custom Border](#table-usage-custom-border)
17
- * [Draw Vertical Line](#table-usage-draw-vertical-line)
18
- * [Draw Horizontal Line](#table-usage-draw-horizontal-line)
19
- * [Single Line Mode](#table-usage-single-line-mode)
20
- * [Padding Cell Content](#table-usage-padding-cell-content)
21
- * [Predefined Border Templates](#table-usage-predefined-border-templates)
22
- * [Streaming](#table-usage-streaming)
23
- * [Text Truncation](#table-usage-text-truncation)
24
- * [Text Wrapping](#table-usage-text-wrapping)
25
-
16
+ * [API](#table-api)
17
+ * [table](#table-api-table-1)
18
+ * [createStream](#table-api-createstream)
19
+ * [getBorderCharacters](#table-api-getbordercharacters)
26
20
 
27
- Produces a string that represents array data in a text table.
28
21
 
29
22
  ![Demo of table displaying a list of missions to the Moon.](./.README/demo.png)
30
23
 
@@ -44,98 +37,28 @@ Produces a string that represents array data in a text table.
44
37
 
45
38
  ```bash
46
39
  npm install table
47
-
48
40
  ```
49
41
 
50
42
  [![Buy Me A Coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/gajus)
51
43
  [![Become a Patron](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/gajus)
52
44
 
45
+
53
46
  <a name="table-usage"></a>
54
47
  ## Usage
55
48
 
56
- Table data is described using an array (rows) of array (cells).
57
-
58
49
  ```js
59
- import {
60
- table
61
- } from 'table';
50
+ import { table } from 'table';
62
51
 
63
52
  // Using commonjs?
64
- // const {table} = require('table');
65
-
66
- let data,
67
- output;
53
+ // const { table } = require('table');
68
54
 
69
- data = [
55
+ const data = [
70
56
  ['0A', '0B', '0C'],
71
57
  ['1A', '1B', '1C'],
72
58
  ['2A', '2B', '2C']
73
59
  ];
74
60
 
75
- /**
76
- * @typedef {string} table~cell
77
- */
78
-
79
- /**
80
- * @typedef {table~cell[]} table~row
81
- */
82
-
83
- /**
84
- * @typedef {Object} table~columns
85
- * @property {string} alignment Cell content alignment (enum: left, center, right) (default: left).
86
- * @property {number} width Column width (default: auto).
87
- * @property {number} truncate Number of characters are which the content will be truncated (default: Infinity).
88
- * @property {number} paddingLeft Cell content padding width left (default: 1).
89
- * @property {number} paddingRight Cell content padding width right (default: 1).
90
- */
91
-
92
- /**
93
- * @typedef {Object} table~border
94
- * @property {string} topBody
95
- * @property {string} topJoin
96
- * @property {string} topLeft
97
- * @property {string} topRight
98
- * @property {string} bottomBody
99
- * @property {string} bottomJoin
100
- * @property {string} bottomLeft
101
- * @property {string} bottomRight
102
- * @property {string} bodyLeft
103
- * @property {string} bodyRight
104
- * @property {string} bodyJoin
105
- * @property {string} joinBody
106
- * @property {string} joinLeft
107
- * @property {string} joinRight
108
- * @property {string} joinJoin
109
- */
110
-
111
- /**
112
- * Used to dynamically tell table whether to draw a line separating rows or not.
113
- * The default behavior is to always return true.
114
- *
115
- * @typedef {function} drawHorizontalLine
116
- * @param {number} index
117
- * @param {number} size
118
- * @return {boolean}
119
- */
120
-
121
- /**
122
- * @typedef {Object} table~config
123
- * @property {table~border} border
124
- * @property {table~columns[]} columns Column specific configuration.
125
- * @property {table~columns} columnDefault Default values for all columns. Column specific settings overwrite the default values.
126
- * @property {table~drawHorizontalLine} drawHorizontalLine
127
- */
128
-
129
- /**
130
- * Generates a text table.
131
- *
132
- * @param {table~row[]} rows
133
- * @param {table~config} config
134
- * @return {String}
135
- */
136
- output = table(data);
137
-
138
- console.log(output);
61
+ console.log(table(data));
139
62
  ```
140
63
 
141
64
  ```
@@ -150,107 +73,44 @@ console.log(output);
150
73
  ```
151
74
 
152
75
 
153
- <a name="table-usage-cell-content-alignment"></a>
154
- ### Cell Content Alignment
155
-
156
- `{string} config.columns[{number}].alignment` property controls content horizontal alignment within a cell.
157
-
158
- Valid values are: "left", "right", "center" and "justify".
159
-
160
- ```js
161
- let config,
162
- data,
163
- output;
164
-
165
- data = [
166
- ['0A', '0B', '0C', '0D 0E 0F'],
167
- ['1A', '1B', '1C', '1D 1E 1F'],
168
- ['2A', '2B', '2C', '2D 2E 2F'],
169
- ];
170
-
171
- config = {
172
- columnDefault: {
173
- width: 10,
174
- },
175
- columns: [
176
- {alignment: 'left'},
177
- {alignment: 'center'},
178
- {alignment: 'right'},
179
- {alignment: 'justify'},
180
- ],
181
- };
182
-
183
- output = table(data, config);
184
-
185
- console.log(output);
186
- ```
187
-
188
- ```
189
- ╔════════════╤════════════╤════════════╤════════════╗
190
- ║ 0A │ 0B │ 0C │ 0D 0E 0F ║
191
- ╟────────────┼────────────┼────────────┼────────────╢
192
- ║ 1A │ 1B │ 1C │ 1D 1E 1F ║
193
- ╟────────────┼────────────┼────────────┼────────────╢
194
- ║ 2A │ 2B │ 2C │ 2D 2E 2F ║
195
- ╚════════════╧════════════╧════════════╧════════════╝
196
- ```
197
-
198
- <a name="table-usage-column-width"></a>
199
- ### Column Width
200
-
201
- `{number} config.columns[{number}].width` property restricts column width to a fixed width.
76
+ <a name="table-api"></a>
77
+ ## API
202
78
 
203
- ```js
204
- let data,
205
- output,
206
- options;
207
-
208
- data = [
209
- ['0A', '0B', '0C'],
210
- ['1A', '1B', '1C'],
211
- ['2A', '2B', '2C']
212
- ];
79
+ <a name="table-api-table-1"></a>
80
+ ### table
213
81
 
214
- options = {
215
- columns: {
216
- 1: {
217
- width: 10
218
- }
219
- }
220
- };
82
+ Returns the string in the table format
221
83
 
222
- output = table(data, options);
84
+ **Parameters:**
85
+ - **_data_:** The data to display
86
+ - Type: `any[][]`
87
+ - Required: `true`
223
88
 
224
- console.log(output);
225
- ```
89
+ - **_config_:** Table configuration
90
+ - Type: `object`
91
+ - Required: `false`
226
92
 
227
- ```
228
- ╔════╤════════════╤════╗
229
- ║ 0A │ 0B │ 0C ║
230
- ╟────┼────────────┼────╢
231
- ║ 1A │ 1B │ 1C ║
232
- ╟────┼────────────┼────╢
233
- ║ 2A │ 2B │ 2C ║
234
- ╚════╧════════════╧════╝
235
- ```
93
+ <a name="table-api-table-1-config-border"></a>
94
+ ##### config.border
236
95
 
237
- <a name="table-usage-custom-border"></a>
238
- ### Custom Border
96
+ Type: `{ [type: string]: string }`\
97
+ Default: `honeywell` [template](#getbordercharacters)
239
98
 
240
- `{object} config.border` property describes characters used to draw the table border.
99
+ Custom borders. The keys are any of:
100
+ - `topLeft`, `topRight`, `topBody`,`topJoin`
101
+ - `bottomLeft`, `bottomRight`, `bottomBody`, `bottomJoin`
102
+ - `joinLeft`, `joinRight`, `joinBody`, `joinJoin`
103
+ - `bodyLeft`, `bodyRight`, `bodyJoin`
104
+ - `headerJoin`
241
105
 
242
106
  ```js
243
- let config,
244
- data,
245
- output;
246
-
247
- data = [
107
+ const data = [
248
108
  ['0A', '0B', '0C'],
249
109
  ['1A', '1B', '1C'],
250
110
  ['2A', '2B', '2C']
251
111
  ];
252
112
 
253
- config = {
113
+ const config = {
254
114
  border: {
255
115
  topBody: `─`,
256
116
  topJoin: `┬`,
@@ -273,9 +133,7 @@ config = {
273
133
  }
274
134
  };
275
135
 
276
- output = table(data, config);
277
-
278
- console.log(output);
136
+ console.log(table(data, config));
279
137
  ```
280
138
 
281
139
  ```
@@ -288,17 +146,17 @@ console.log(output);
288
146
  └────┴────┴────┘
289
147
  ```
290
148
 
291
- <a name="table-usage-draw-vertical-line"></a>
292
- ### Draw Vertical Line
149
+ <a name="table-api-table-1-config-drawverticalline"></a>
150
+ ##### config.drawVerticalLine
293
151
 
294
- `{function} config.drawVerticalLine` property is a function that is called for every non-content column in the table. The result of the function `{boolean}` determines whether a border is drawn.
152
+ Type: `(lineIndex: number, columnCount: number) => boolean`\
153
+ Default: `() => true`
295
154
 
296
- ```js
297
- let data,
298
- output,
299
- options;
155
+ It is used to tell whether to draw a vertical line. This callback is called for each vertical border of the table.
156
+ If the table has `n` columns, then the `index` parameter is alternatively received all numbers in range `[0, n]` inclusively.
300
157
 
301
- data = [
158
+ ```js
159
+ const data = [
302
160
  ['0A', '0B', '0C'],
303
161
  ['1A', '1B', '1C'],
304
162
  ['2A', '2B', '2C'],
@@ -306,21 +164,13 @@ data = [
306
164
  ['4A', '4B', '4C']
307
165
  ];
308
166
 
309
- options = {
310
- /**
311
- * @typedef {function} drawVerticalLine
312
- * @param {number} index
313
- * @param {number} size
314
- * @return {boolean}
315
- */
316
- drawVerticalLine: (index, size) => {
317
- return index === 0 || index === size;
167
+ const config = {
168
+ drawVerticalLine: (lineIndex, columnCount) => {
169
+ return lineIndex === 0 || lineIndex === columnCount;
318
170
  }
319
171
  };
320
172
 
321
- output = table(data, options);
322
-
323
- console.log(output);
173
+ console.log(table(data, config));
324
174
 
325
175
  ```
326
176
 
@@ -339,17 +189,18 @@ console.log(output);
339
189
 
340
190
  ```
341
191
 
342
- <a name="table-usage-draw-horizontal-line"></a>
343
- ### Draw Horizontal Line
192
+ <a name="table-api-table-1-config-drawhorizontalline"></a>
193
+ ##### config.drawHorizontalLine
344
194
 
345
- `{function} config.drawHorizontalLine` property is a function that is called for every non-content row in the table. The result of the function `{boolean}` determines whether a row is drawn.
195
+ Type: `(lineIndex: number, rowCount: number) => boolean`\
196
+ Default: `() => true`
346
197
 
347
- ```js
348
- let data,
349
- output,
350
- options;
198
+ It is used to tell whether to draw a horizontal line. This callback is called for each horizontal border of the table.
199
+ If the table has `n` rows, then the `index` parameter is alternatively received all numbers in range `[0, n]` inclusively.
200
+ If the table has `n` rows and contains the header, then the range will be `[0, n+1]` inclusively.
351
201
 
352
- data = [
202
+ ```js
203
+ const data = [
353
204
  ['0A', '0B', '0C'],
354
205
  ['1A', '1B', '1C'],
355
206
  ['2A', '2B', '2C'],
@@ -357,21 +208,13 @@ data = [
357
208
  ['4A', '4B', '4C']
358
209
  ];
359
210
 
360
- options = {
361
- /**
362
- * @typedef {function} drawHorizontalLine
363
- * @param {number} index
364
- * @param {number} size
365
- * @return {boolean}
366
- */
367
- drawHorizontalLine: (index, size) => {
368
- return index === 0 || index === 1 || index === size - 1 || index === size;
211
+ const config = {
212
+ drawHorizontalLine: (lineIndex, rowCount) => {
213
+ return lineIndex === 0 || lineIndex === 1 || lineIndex === rowCount - 1 || lineIndex === rowCount;
369
214
  }
370
215
  };
371
216
 
372
- output = table(data, options);
373
-
374
- console.log(output);
217
+ console.log(table(data, config));
375
218
 
376
219
  ```
377
220
 
@@ -388,17 +231,15 @@ console.log(output);
388
231
 
389
232
  ```
390
233
 
391
- <a name="table-usage-single-line-mode"></a>
392
- ### Single Line Mode
234
+ <a name="table-api-table-1-config-singleline"></a>
235
+ ##### config.singleLine
393
236
 
394
- Horizontal lines inside the table are not drawn.
237
+ Type: `boolean`\
238
+ Default: `false`
395
239
 
396
- ```js
397
- import {
398
- table,
399
- getBorderCharacters
400
- } from 'table';
240
+ If `true`, horizontal lines inside the table are not drawn. This option also overrides the `config.drawHorizontalLine` if specified.
401
241
 
242
+ ```js
402
243
  const data = [
403
244
  ['-rw-r--r--', '1', 'pandorym', 'staff', '1529', 'May 23 11:25', 'LICENSE'],
404
245
  ['-rw-r--r--', '1', 'pandorym', 'staff', '16327', 'May 23 11:58', 'README.md'],
@@ -414,8 +255,7 @@ const config = {
414
255
  singleLine: true
415
256
  };
416
257
 
417
- const output = table(data, config);
418
- console.log(output);
258
+ console.log(table(data, config));
419
259
  ```
420
260
 
421
261
  ```
@@ -431,37 +271,163 @@ console.log(output);
431
271
  ╚═════════════╧═════╧══════════╧═══════╧════════╧══════════════╧═══════════════════╝
432
272
  ```
433
273
 
434
- <a name="table-usage-padding-cell-content"></a>
435
- ### Padding Cell Content
436
274
 
437
- `{number} config.columns[{number}].paddingLeft` and `{number} config.columns[{number}].paddingRight` properties control content padding within a cell. Property value represents a number of whitespaces used to pad the content.
275
+ <a name="table-api-table-1-config-columns"></a>
276
+ ##### config.columns
277
+
278
+ Type: `Column[] | { [columnIndex: number]: Column }`
279
+
280
+ Column specific configurations.
281
+
282
+ <a name="table-api-table-1-config-columns-config-columns-width"></a>
283
+ ###### config.columns[*].width
284
+
285
+ Type: `number`\
286
+ Default: the maximum cell widths of the column
287
+
288
+ Column width (excluding the paddings).
438
289
 
439
290
  ```js
440
- let config,
441
- data,
442
- output;
443
291
 
444
- data = [
445
- ['0A', 'AABBCC', '0C'],
292
+ const data = [
293
+ ['0A', '0B', '0C'],
446
294
  ['1A', '1B', '1C'],
447
295
  ['2A', '2B', '2C']
448
296
  ];
449
297
 
450
- config = {
298
+ const config = {
451
299
  columns: {
452
- 0: {
300
+ 1: { width: 10 }
301
+ }
302
+ };
303
+
304
+ console.log(table(data, config));
305
+ ```
306
+
307
+ ```
308
+ ╔════╤════════════╤════╗
309
+ ║ 0A │ 0B │ 0C ║
310
+ ╟────┼────────────┼────╢
311
+ ║ 1A │ 1B │ 1C ║
312
+ ╟────┼────────────┼────╢
313
+ ║ 2A │ 2B │ 2C ║
314
+ ╚════╧════════════╧════╝
315
+ ```
316
+
317
+ <a name="table-api-table-1-config-columns-config-columns-alignment"></a>
318
+ ###### config.columns[*].alignment
319
+
320
+ Type: `'center' | 'justify' | 'left' | 'right'`\
321
+ Default: `'left'`
322
+
323
+ Cell content horizontal alignment
324
+
325
+ ```js
326
+ const data = [
327
+ ['0A', '0B', '0C', '0D 0E 0F'],
328
+ ['1A', '1B', '1C', '1D 1E 1F'],
329
+ ['2A', '2B', '2C', '2D 2E 2F'],
330
+ ];
331
+
332
+ const config = {
333
+ columnDefault: {
334
+ width: 10,
335
+ },
336
+ columns: [
337
+ { alignment: 'left' },
338
+ { alignment: 'center' },
339
+ { alignment: 'right' },
340
+ { alignment: 'justify' }
341
+ ],
342
+ };
343
+
344
+ console.log(table(data, config));
345
+ ```
346
+
347
+ ```
348
+ ╔════════════╤════════════╤════════════╤════════════╗
349
+ ║ 0A │ 0B │ 0C │ 0D 0E 0F ║
350
+ ╟────────────┼────────────┼────────────┼────────────╢
351
+ ║ 1A │ 1B │ 1C │ 1D 1E 1F ║
352
+ ╟────────────┼────────────┼────────────┼────────────╢
353
+ ║ 2A │ 2B │ 2C │ 2D 2E 2F ║
354
+ ╚════════════╧════════════╧════════════╧════════════╝
355
+ ```
356
+
357
+ <a name="table-api-table-1-config-columns-config-columns-verticalalignment"></a>
358
+ ###### config.columns[*].verticalAlignment
359
+
360
+ Type: `'top' | 'middle' | 'bottom'`\
361
+ Default: `'top'`
362
+
363
+ Cell content vertical alignment
364
+
365
+ ```js
366
+ const data = [
367
+ ['A', 'B', 'C', 'DEF'],
368
+ ];
369
+
370
+ const config = {
371
+ columnDefault: {
372
+ width: 1,
373
+ },
374
+ columns: [
375
+ { verticalAlignment: 'top' },
376
+ { verticalAlignment: 'middle' },
377
+ { verticalAlignment: 'bottom' },
378
+ ],
379
+ };
380
+
381
+ console.log(table(data, config));
382
+ ```
383
+
384
+ ```
385
+ ╔═══╤═══╤═══╤═══╗
386
+ ║ A │ │ │ D ║
387
+ ║ │ B │ │ E ║
388
+ ║ │ │ C │ F ║
389
+ ╚═══╧═══╧═══╧═══╝
390
+ ```
391
+
392
+ <a name="table-api-table-1-config-columns-config-columns-paddingleft"></a>
393
+ ###### config.columns[*].paddingLeft
394
+
395
+ Type: `number`\
396
+ Default: `1`
397
+
398
+ The number of whitespaces used to pad the content on the left.
399
+
400
+ <a name="table-api-table-1-config-columns-config-columns-paddingright"></a>
401
+ ###### config.columns[*].paddingRight
402
+
403
+ Type: `number`\
404
+ Default: `1`
405
+
406
+ The number of whitespaces used to pad the content on the right.
407
+
408
+ The `paddingLeft` and `paddingRight` options do not count on the column width. So the column has `width = 5`, `paddingLeft = 2` and `paddingRight = 2` will have the total width is `9`.
409
+
410
+
411
+ ```js
412
+ const data = [
413
+ ['0A', 'AABBCC', '0C'],
414
+ ['1A', '1B', '1C'],
415
+ ['2A', '2B', '2C']
416
+ ];
417
+
418
+ const config = {
419
+ columns: [
420
+ {
453
421
  paddingLeft: 3
454
422
  },
455
- 1: {
423
+ {
456
424
  width: 2,
457
425
  paddingRight: 3
458
426
  }
459
- }
427
+ ]
460
428
  };
461
429
 
462
- output = table(data, config);
463
-
464
- console.log(output);
430
+ console.log(table(data, config));
465
431
  ```
466
432
 
467
433
  ```
@@ -476,136 +442,180 @@ console.log(output);
476
442
  ╚══════╧══════╧════╝
477
443
  ```
478
444
 
479
- <a name="table-usage-predefined-border-templates"></a>
480
- ### Predefined Border Templates
445
+ <a name="table-api-table-1-config-columns-config-columns-truncate"></a>
446
+ ###### config.columns[*].truncate
481
447
 
482
- You can load one of the predefined border templates using `getBorderCharacters` function.
483
-
484
- ```js
485
- import {
486
- table,
487
- getBorderCharacters
488
- } from 'table';
448
+ Type: `number`\
449
+ Default: `Infinity`
489
450
 
490
- let config,
491
- data;
451
+ The number of characters is which the content will be truncated.
452
+ To handle a content that overflows the container width, `table` package implements [text wrapping](#config.columns[*].wrapWord). However, sometimes you may want to truncate content that is too long to be displayed in the table.
492
453
 
493
- data = [
494
- ['0A', '0B', '0C'],
495
- ['1A', '1B', '1C'],
496
- ['2A', '2B', '2C']
454
+ ```js
455
+ const data = [
456
+ ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pulvinar nibh sed mauris convallis dapibus. Nunc venenatis tempus nulla sit amet viverra.']
497
457
  ];
498
458
 
499
- config = {
500
- border: getBorderCharacters(`name of the template`)
459
+ const config = {
460
+ columns: [
461
+ {
462
+ width: 20,
463
+ truncate: 100
464
+ }
465
+ ]
501
466
  };
502
467
 
503
- table(data, config);
468
+ console.log(table(data, config));
504
469
  ```
505
470
 
506
471
  ```
507
- # honeywell
472
+ ╔══════════════════════╗
473
+ ║ Lorem ipsum dolor si ║
474
+ ║ t amet, consectetur ║
475
+ ║ adipiscing elit. Pha ║
476
+ ║ sellus pulvinar nibh ║
477
+ ║ sed mauris convall… ║
478
+ ╚══════════════════════╝
479
+ ```
508
480
 
509
- ╔════╤════╤════╗
510
- ║ 0A │ 0B │ 0C ║
511
- ╟────┼────┼────╢
512
- ║ 1A │ 1B │ 1C ║
513
- ╟────┼────┼────╢
514
- ║ 2A │ 2B │ 2C ║
515
- ╚════╧════╧════╝
481
+ <a name="table-api-table-1-config-columns-config-columns-wrapword"></a>
482
+ ###### config.columns[*].wrapWord
516
483
 
517
- # norc
484
+ Type: `boolean`\
485
+ Default: `false`
518
486
 
519
- ┌────┬────┬────┐
520
- │ 0A │ 0B │ 0C │
521
- ├────┼────┼────┤
522
- │ 1A │ 1B │ 1C │
523
- ├────┼────┼────┤
524
- │ 2A │ 2B │ 2C │
525
- └────┴────┴────┘
487
+ The `table` package implements auto text wrapping, i.e., text that has the width greater than the container width will be separated into multiple lines at the nearest space or one of the special characters: `\|/_.,;-`.
526
488
 
527
- # ramac (ASCII; for use in terminals that do not support Unicode characters)
489
+ When `wrapWord` is `false`:
528
490
 
529
- +----+----+----+
530
- | 0A | 0B | 0C |
531
- |----|----|----|
532
- | 1A | 1B | 1C |
533
- |----|----|----|
534
- | 2A | 2B | 2C |
535
- +----+----+----+
491
+ ```js
492
+ const data = [
493
+ ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pulvinar nibh sed mauris convallis dapibus. Nunc venenatis tempus nulla sit amet viverra.']
494
+ ];
536
495
 
537
- # void (no borders; see "bordless table" section of the documentation)
496
+ const config = {
497
+ columns: [ { width: 20 } ]
498
+ };
538
499
 
539
- 0A 0B 0C
500
+ console.log(table(data, config));
501
+ ```
540
502
 
541
- 1A 1B 1C
503
+ ```
504
+ ╔══════════════════════╗
505
+ ║ Lorem ipsum dolor si ║
506
+ ║ t amet, consectetur ║
507
+ ║ adipiscing elit. Pha ║
508
+ ║ sellus pulvinar nibh ║
509
+ ║ sed mauris convallis ║
510
+ ║ dapibus. Nunc venena ║
511
+ ║ tis tempus nulla sit ║
512
+ ║ amet viverra. ║
513
+ ╚══════════════════════╝
514
+ ```
542
515
 
543
- 2A 2B 2C
516
+ When `wrapWord` is `true`:
517
+
518
+ ```
519
+ ╔══════════════════════╗
520
+ ║ Lorem ipsum dolor ║
521
+ ║ sit amet, ║
522
+ ║ consectetur ║
523
+ ║ adipiscing elit. ║
524
+ ║ Phasellus pulvinar ║
525
+ ║ nibh sed mauris ║
526
+ ║ convallis dapibus. ║
527
+ ║ Nunc venenatis ║
528
+ ║ tempus nulla sit ║
529
+ ║ amet viverra. ║
530
+ ╚══════════════════════╝
544
531
 
545
532
  ```
546
533
 
547
- Raise [an issue](https://github.com/gajus/table/issues) if you'd like to contribute a new border template.
548
534
 
549
- <a name="table-usage-predefined-border-templates-borderless-table"></a>
550
- #### Borderless Table
535
+ <a name="table-api-table-1-config-columndefault"></a>
536
+ ##### config.columnDefault
537
+
538
+ Type: `Column`\
539
+ Default: `{}`
540
+
541
+ The default configuration for all columns. Column-specific settings will overwrite the default values.
551
542
 
552
- Simply using "void" border character template creates a table with a lot of unnecessary spacing.
553
543
 
554
- To create a more plesant to the eye table, reset the padding and remove the joining rows, e.g.
544
+ <a name="table-api-table-1-config-header"></a>
545
+ ##### config.header
546
+
547
+ Type: `object`
548
+
549
+ Header configuration.
550
+
551
+ The header configuration inherits the most of the column's, except:
552
+ - `content` **{string}**: the header content.
553
+ - `width:` calculate based on the content width automatically.
554
+ - `alignment:` `center` be default.
555
+ - `verticalAlignment:` is not supported.
556
+ - `config.border.topJoin` will be `config.border.topBody` for prettier.
555
557
 
556
558
  ```js
557
- let output;
559
+ const data = [
560
+ ['0A', '0B', '0C'],
561
+ ['1A', '1B', '1C'],
562
+ ['2A', '2B', '2C'],
563
+ ];
558
564
 
559
- output = table(data, {
560
- border: getBorderCharacters(`void`),
561
- columnDefault: {
562
- paddingLeft: 0,
563
- paddingRight: 1
564
- },
565
- drawHorizontalLine: () => {
566
- return false
567
- }
568
- });
565
+ const config = {
566
+ columnDefault: {
567
+ width: 10,
568
+ },
569
+ header: {
570
+ alignment: 'center',
571
+ content: 'THE HEADER\nThis is the table about something',
572
+ },
573
+ }
569
574
 
570
- console.log(output);
575
+ console.log(table(data, config));
571
576
  ```
572
577
 
573
578
  ```
574
- 0A 0B 0C
575
- 1A 1B 1C
576
- 2A 2B 2C
579
+ ╔══════════════════════════════════════╗
580
+ ║ THE HEADER ║
581
+ ║ This is the table about something ║
582
+ ╟────────────┬────────────┬────────────╢
583
+ ║ 0A │ 0B │ 0C ║
584
+ ╟────────────┼────────────┼────────────╢
585
+ ║ 1A │ 1B │ 1C ║
586
+ ╟────────────┼────────────┼────────────╢
587
+ ║ 2A │ 2B │ 2C ║
588
+ ╚════════════╧════════════╧════════════╝
577
589
  ```
578
590
 
579
- <a name="table-usage-streaming"></a>
580
- ### Streaming
591
+
592
+ <a name="table-api-createstream"></a>
593
+ ### createStream
581
594
 
582
595
  `table` package exports `createStream` function used to draw a table and append rows.
583
596
 
584
- `createStream` requires `{number} columnDefault.width` and `{number} columnCount` configuration properties.
597
+ **Parameter:**
598
+ - _**config:**_ the same as `table`'s, except `config.columnDefault.width` and `config.columnCount` must be provided.
585
599
 
586
- ```js
587
- import {
588
- createStream
589
- } from 'table';
590
600
 
591
- let config,
592
- stream;
601
+ ```js
602
+ import { createStream } from 'table';
593
603
 
594
- config = {
604
+ const config = {
595
605
  columnDefault: {
596
606
  width: 50
597
607
  },
598
608
  columnCount: 1
599
609
  };
600
610
 
601
- stream = createStream(config);
611
+ const stream = createStream(config);
602
612
 
603
613
  setInterval(() => {
604
614
  stream.write([new Date()]);
605
615
  }, 500);
606
616
  ```
607
617
 
608
- ![Streaming current date.](./.README/streaming.gif)
618
+ ![Streaming current date.](./.README/api/stream/streaming.gif)
609
619
 
610
620
  `table` package uses ANSI escape codes to overwrite the output of the last line when a new row is printed.
611
621
 
@@ -614,38 +624,29 @@ The underlying implementation is explained in this [Stack Overflow answer](http:
614
624
  Streaming supports all of the configuration properties and functionality of a static table (such as auto text wrapping, alignment and padding), e.g.
615
625
 
616
626
  ```js
617
- import {
618
- createStream
619
- } from 'table';
627
+ import { createStream } from 'table';
620
628
 
621
629
  import _ from 'lodash';
622
630
 
623
- let config,
624
- stream,
625
- i;
626
-
627
- config = {
631
+ const config = {
628
632
  columnDefault: {
629
633
  width: 50
630
634
  },
631
635
  columnCount: 3,
632
- columns: {
633
- 0: {
636
+ columns: [
637
+ {
634
638
  width: 10,
635
639
  alignment: 'right'
636
640
  },
637
- 1: {
638
- alignment: 'center',
639
- },
640
- 2: {
641
- width: 10
642
- }
643
- }
641
+ { alignment: 'center' },
642
+ { width: 10 }
643
+
644
+ ]
644
645
  };
645
646
 
646
- stream = createStream(config);
647
+ const stream = createStream(config);
647
648
 
648
- i = 0;
649
+ let i = 0;
649
650
 
650
651
  setInterval(() => {
651
652
  let random;
@@ -656,126 +657,103 @@ setInterval(() => {
656
657
  }, 500);
657
658
  ```
658
659
 
659
- ![Streaming random data.](./.README/streaming-random.gif)
660
+ ![Streaming random data.](./.README/api/stream/streaming-random.gif)
661
+
660
662
 
661
- <a name="table-usage-text-truncation"></a>
662
- ### Text Truncation
663
+ <a name="table-api-getbordercharacters"></a>
664
+ ### getBorderCharacters
663
665
 
664
- To handle a content that overflows the container width, `table` package implements [text wrapping](#table-usage-text-wrapping). However, sometimes you may want to truncate content that is too long to be displayed in the table.
666
+ **Parameter:**
667
+ - **_template_**
668
+ - Type: `'honeywell' | 'norc' | 'ramac' | 'void'`
669
+ - Required: `true`
665
670
 
666
- `{number} config.columns[{number}].truncate` property (default: `Infinity`) truncates the text at the specified length.
671
+ You can load one of the predefined border templates using `getBorderCharacters` function.
667
672
 
668
673
  ```js
669
- let config,
670
- data,
671
- output;
674
+ import { table, getBorderCharacters } from 'table';
672
675
 
673
- data = [
674
- ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pulvinar nibh sed mauris convallis dapibus. Nunc venenatis tempus nulla sit amet viverra.']
676
+ const data = [
677
+ ['0A', '0B', '0C'],
678
+ ['1A', '1B', '1C'],
679
+ ['2A', '2B', '2C']
675
680
  ];
676
681
 
677
- config = {
678
- columns: {
679
- 0: {
680
- width: 20,
681
- truncate: 100
682
- }
683
- }
682
+ const config = {
683
+ border: getBorderCharacters(`name of the template`)
684
684
  };
685
685
 
686
- output = table(data, config);
687
-
688
- console.log(output);
686
+ console.log(table(data, config));
689
687
  ```
690
688
 
691
689
  ```
692
- ╔══════════════════════╗
693
- ║ Lorem ipsum dolor si ║
694
- ║ t amet, consectetur ║
695
- ║ adipiscing elit. Pha ║
696
- ║ sellus pulvinar nibh ║
697
- ║ sed mauris convall… ║
698
- ╚══════════════════════╝
699
- ```
690
+ # honeywell
700
691
 
701
- <a name="table-usage-text-wrapping"></a>
702
- ### Text Wrapping
692
+ ╔════╤════╤════╗
693
+ ║ 0A │ 0B │ 0C ║
694
+ ╟────┼────┼────╢
695
+ ║ 1A │ 1B │ 1C ║
696
+ ╟────┼────┼────╢
697
+ ║ 2A │ 2B │ 2C ║
698
+ ╚════╧════╧════╝
703
699
 
704
- `table` package implements auto text wrapping, i.e. text that has width greater than the container width will be separated into multiple lines, e.g.
700
+ # norc
705
701
 
706
- ```js
707
- let config,
708
- data,
709
- output;
702
+ ┌────┬────┬────┐
703
+ │ 0A │ 0B │ 0C │
704
+ ├────┼────┼────┤
705
+ │ 1A │ 1B │ 1C │
706
+ ├────┼────┼────┤
707
+ │ 2A │ 2B │ 2C │
708
+ └────┴────┴────┘
710
709
 
711
- data = [
712
- ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pulvinar nibh sed mauris convallis dapibus. Nunc venenatis tempus nulla sit amet viverra.']
713
- ];
710
+ # ramac (ASCII; for use in terminals that do not support Unicode characters)
714
711
 
715
- config = {
716
- columns: {
717
- 0: {
718
- width: 20
719
- }
720
- }
721
- };
712
+ +----+----+----+
713
+ | 0A | 0B | 0C |
714
+ |----|----|----|
715
+ | 1A | 1B | 1C |
716
+ |----|----|----|
717
+ | 2A | 2B | 2C |
718
+ +----+----+----+
722
719
 
723
- output = table(data, config);
720
+ # void (no borders; see "borderless table" section of the documentation)
724
721
 
725
- console.log(output);
726
- ```
722
+ 0A 0B 0C
723
+
724
+ 1A 1B 1C
725
+
726
+ 2A 2B 2C
727
727
 
728
- ```
729
- ╔══════════════════════╗
730
- ║ Lorem ipsum dolor si ║
731
- ║ t amet, consectetur ║
732
- ║ adipiscing elit. Pha ║
733
- ║ sellus pulvinar nibh ║
734
- ║ sed mauris convallis ║
735
- ║ dapibus. Nunc venena ║
736
- ║ tis tempus nulla sit ║
737
- ║ amet viverra. ║
738
- ╚══════════════════════╝
739
728
  ```
740
729
 
741
- When `wrapWord` is `true` the text is broken at the nearest space or one of the special characters ("-", "_", "\", "/", ".", ",", ";"), e.g.
730
+ Raise [an issue](https://github.com/gajus/table/issues) if you'd like to contribute a new border template.
742
731
 
743
- ```js
744
- let config,
745
- data,
746
- output;
732
+ <a name="table-api-getbordercharacters-borderless-table"></a>
733
+ #### Borderless Table
747
734
 
748
- data = [
749
- ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pulvinar nibh sed mauris convallis dapibus. Nunc venenatis tempus nulla sit amet viverra.']
750
- ];
735
+ Simply using `void` border character template creates a table with a lot of unnecessary spacing.
751
736
 
752
- config = {
753
- columns: {
754
- 0: {
755
- width: 20,
756
- wrapWord: true
757
- }
758
- }
759
- };
737
+ To create a more pleasant to the eye table, reset the padding and remove the joining rows, e.g.
738
+
739
+ ```js
760
740
 
761
- output = table(data, config);
741
+ const output = table(data, {
742
+ border: getBorderCharacters('void'),
743
+ columnDefault: {
744
+ paddingLeft: 0,
745
+ paddingRight: 1
746
+ },
747
+ drawHorizontalLine: () => false
748
+ }
749
+ );
762
750
 
763
751
  console.log(output);
764
752
  ```
765
753
 
766
754
  ```
767
- ╔══════════════════════╗
768
- ║ Lorem ipsum dolor ║
769
- ║ sit amet, ║
770
- ║ consectetur ║
771
- ║ adipiscing elit. ║
772
- ║ Phasellus pulvinar ║
773
- ║ nibh sed mauris ║
774
- ║ convallis dapibus. ║
775
- ║ Nunc venenatis ║
776
- ║ tempus nulla sit ║
777
- ║ amet viverra. ║
778
- ╚══════════════════════╝
779
-
755
+ 0A 0B 0C
756
+ 1A 1B 1C
757
+ 2A 2B 2C
780
758
  ```
781
759