Skip to content

Commit 0627005

Browse files
Pandorymgajus
authored andcommitted
feat: single line mode (compact) (#94)
1 parent 64364f8 commit 0627005

File tree

7 files changed

+137
-3
lines changed

7 files changed

+137
-3
lines changed

.README/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Produces a string that represents array data in a text table.
2929
{"gitdown": "include", "file": "./usage/column_width.md"}
3030
{"gitdown": "include", "file": "./usage/custom_border.md"}
3131
{"gitdown": "include", "file": "./usage/draw_horizontal_line.md"}
32+
{"gitdown": "include", "file": "./usage/single_line_mode.md"}
3233
{"gitdown": "include", "file": "./usage/padding_cell_content.md"}
3334
{"gitdown": "include", "file": "./usage/predefined_border_templates.md"}
3435
{"gitdown": "include", "file": "./usage/streaming.md"}

.README/usage/single_line_mode.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
### Single Line Mode
2+
3+
Horizontal lines inside the table are not drawn.
4+
5+
```js
6+
import {
7+
table,
8+
getBorderCharacters
9+
} from 'table';
10+
11+
const data = [
12+
['-rw-r--r--', '1', 'pandorym', 'staff', '1529', 'May 23 11:25', 'LICENSE'],
13+
['-rw-r--r--', '1', 'pandorym', 'staff', '16327', 'May 23 11:58', 'README.md'],
14+
['drwxr-xr-x', '76', 'pandorym', 'staff', '2432', 'May 23 12:02', 'dist'],
15+
['drwxr-xr-x', '634', 'pandorym', 'staff', '20288', 'May 23 11:54', 'node_modules'],
16+
['-rw-r--r--', '1,', 'pandorym', 'staff', '525688', 'May 23 11:52', 'package-lock.json'],
17+
['-rw-r--r--@', '1', 'pandorym', 'staff', '2440', 'May 23 11:25', 'package.json'],
18+
['drwxr-xr-x', '27', 'pandorym', 'staff', '864', 'May 23 11:25', 'src'],
19+
['drwxr-xr-x', '20', 'pandorym', 'staff', '640', 'May 23 11:25', 'test'],
20+
];
21+
22+
const config = {
23+
singleLine: true
24+
};
25+
26+
const output = table(data, config);
27+
console.log(output);
28+
```
29+
30+
```
31+
╔═════════════╤═════╤══════════╤═══════╤════════╤══════════════╤═══════════════════╗
32+
║ -rw-r--r-- │ 1 │ pandorym │ staff │ 1529 │ May 23 11:25 │ LICENSE ║
33+
║ -rw-r--r-- │ 1 │ pandorym │ staff │ 16327 │ May 23 11:58 │ README.md ║
34+
║ drwxr-xr-x │ 76 │ pandorym │ staff │ 2432 │ May 23 12:02 │ dist ║
35+
║ drwxr-xr-x │ 634 │ pandorym │ staff │ 20288 │ May 23 11:54 │ node_modules ║
36+
║ -rw-r--r-- │ 1, │ pandorym │ staff │ 525688 │ May 23 11:52 │ package-lock.json ║
37+
║ -rw-r--r--@ │ 1 │ pandorym │ staff │ 2440 │ May 23 11:25 │ package.json ║
38+
║ drwxr-xr-x │ 27 │ pandorym │ staff │ 864 │ May 23 11:25 │ src ║
39+
║ drwxr-xr-x │ 20 │ pandorym │ staff │ 640 │ May 23 11:25 │ test ║
40+
╚═════════════╧═════╧══════════╧═══════╧════════╧══════════════╧═══════════════════╝
41+
```

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* [Column Width](#table-usage-column-width)
1717
* [Custom Border](#table-usage-custom-border)
1818
* [Draw Horizontal Line](#table-usage-draw-horizontal-line)
19+
* [Single Line Mode](#table-usage-single-line-mode)
1920
* [Padding Cell Content](#table-usage-padding-cell-content)
2021
* [Predefined Border Templates](#table-usage-predefined-border-templates)
2122
* [Streaming](#table-usage-streaming)
@@ -341,6 +342,49 @@ console.log(output);
341342
342343
```
343344

345+
<a name="table-usage-single-line-mode"></a>
346+
### Single Line Mode
347+
348+
Horizontal lines inside the table are not drawn.
349+
350+
```js
351+
import {
352+
table,
353+
getBorderCharacters
354+
} from 'table';
355+
356+
const data = [
357+
['-rw-r--r--', '1', 'pandorym', 'staff', '1529', 'May 23 11:25', 'LICENSE'],
358+
['-rw-r--r--', '1', 'pandorym', 'staff', '16327', 'May 23 11:58', 'README.md'],
359+
['drwxr-xr-x', '76', 'pandorym', 'staff', '2432', 'May 23 12:02', 'dist'],
360+
['drwxr-xr-x', '634', 'pandorym', 'staff', '20288', 'May 23 11:54', 'node_modules'],
361+
['-rw-r--r--', '1,', 'pandorym', 'staff', '525688', 'May 23 11:52', 'package-lock.json'],
362+
['-rw-r--r--@', '1', 'pandorym', 'staff', '2440', 'May 23 11:25', 'package.json'],
363+
['drwxr-xr-x', '27', 'pandorym', 'staff', '864', 'May 23 11:25', 'src'],
364+
['drwxr-xr-x', '20', 'pandorym', 'staff', '640', 'May 23 11:25', 'test'],
365+
];
366+
367+
const config = {
368+
singleLine: true
369+
};
370+
371+
const output = table(data, config);
372+
console.log(output);
373+
```
374+
375+
```
376+
╔═════════════╤═════╤══════════╤═══════╤════════╤══════════════╤═══════════════════╗
377+
║ -rw-r--r-- │ 1 │ pandorym │ staff │ 1529 │ May 23 11:25 │ LICENSE ║
378+
║ -rw-r--r-- │ 1 │ pandorym │ staff │ 16327 │ May 23 11:58 │ README.md ║
379+
║ drwxr-xr-x │ 76 │ pandorym │ staff │ 2432 │ May 23 12:02 │ dist ║
380+
║ drwxr-xr-x │ 634 │ pandorym │ staff │ 20288 │ May 23 11:54 │ node_modules ║
381+
║ -rw-r--r-- │ 1, │ pandorym │ staff │ 525688 │ May 23 11:52 │ package-lock.json ║
382+
║ -rw-r--r--@ │ 1 │ pandorym │ staff │ 2440 │ May 23 11:25 │ package.json ║
383+
║ drwxr-xr-x │ 27 │ pandorym │ staff │ 864 │ May 23 11:25 │ src ║
384+
║ drwxr-xr-x │ 20 │ pandorym │ staff │ 640 │ May 23 11:25 │ test ║
385+
╚═════════════╧═════╧══════════╧═══════╧════════╧══════════════╧═══════════════════╝
386+
```
387+
344388
<a name="table-usage-padding-cell-content"></a>
345389
### Padding Cell Content
346390

src/drawTable.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import drawRow from './drawRow';
1111
* @param {Array} columnSizeIndex
1212
* @param {Array} rowSpanIndex
1313
* @param {Function} drawHorizontalLine
14+
* @param {boolean} singleLine
1415
* @returns {string}
1516
*/
16-
export default (rows, border, columnSizeIndex, rowSpanIndex, drawHorizontalLine) => {
17+
export default (rows, border, columnSizeIndex, rowSpanIndex, drawHorizontalLine, singleLine) => {
1718
let output;
1819
let realRowIndex;
1920
let rowHeight;
@@ -39,7 +40,7 @@ export default (rows, border, columnSizeIndex, rowSpanIndex, drawHorizontalLine)
3940

4041
rowHeight--;
4142

42-
if (rowHeight === 0 && index0 !== rowCount - 1 && drawHorizontalLine(realRowIndex, rowCount)) {
43+
if (!singleLine && rowHeight === 0 && index0 !== rowCount - 1 && drawHorizontalLine(realRowIndex, rowCount)) {
4344
output += drawBorderJoin(columnSizeIndex, border);
4445
}
4546
});

src/makeConfig.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,9 @@ export default (rows, userConfig = {}) => {
6868
};
6969
}
7070

71+
if (config.singleLine === undefined) {
72+
config.singleLine = false;
73+
}
74+
7175
return config;
7276
};

src/table.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import truncateTableData from './truncateTableData';
6363
* @property {table~columns[]} columns Column specific configuration.
6464
* @property {table~columns} columnDefault Default values for all columns. Column specific settings overwrite the default values.
6565
* @property {table~drawHorizontalLine} drawHorizontalLine
66+
* @property {table~singleLine} singleLine Horizontal lines inside the table are not drawn.
6667
*/
6768

6869
/**
@@ -91,5 +92,5 @@ export default (data, userConfig = {}) => {
9192

9293
const cellWidthIndex = calculateCellWidthIndex(rows[0]);
9394

94-
return drawTable(rows, config.border, cellWidthIndex, rowHeightIndex, config.drawHorizontalLine);
95+
return drawTable(rows, config.border, cellWidthIndex, rowHeightIndex, config.drawHorizontalLine, config.singleLine);
9596
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
table
3+
} from '../../../src';
4+
import expectTable from './expectTable';
5+
6+
describe('README.md usage/', () => {
7+
it('single_line_mode', () => {
8+
const data = [
9+
['-rw-r--r--', '1', 'pandorym', 'staff', '1529', 'May 23 11:25', 'LICENSE'],
10+
['-rw-r--r--', '1', 'pandorym', 'staff', '16327', 'May 23 11:58', 'README.md'],
11+
['drwxr-xr-x', '76', 'pandorym', 'staff', '2432', 'May 23 12:02', 'dist'],
12+
['drwxr-xr-x', '634', 'pandorym', 'staff', '20288', 'May 23 11:54', 'node_modules'],
13+
['-rw-r--r--', '1,', 'pandorym', 'staff', '525688', 'May 23 11:52', 'package-lock.json'],
14+
['-rw-r--r--@', '1', 'pandorym', 'staff', '2440', 'May 23 11:25', 'package.json'],
15+
['drwxr-xr-x', '27', 'pandorym', 'staff', '864', 'May 23 11:25', 'src'],
16+
['drwxr-xr-x', '20', 'pandorym', 'staff', '640', 'May 23 11:25', 'test']
17+
];
18+
19+
const config = {
20+
singleLine: true
21+
};
22+
23+
const output = table(data, config);
24+
25+
// console.log(output);
26+
27+
/* eslint-disable no-restricted-syntax */
28+
expectTable(output, `
29+
╔═════════════╤═════╤══════════╤═══════╤════════╤══════════════╤═══════════════════╗
30+
║ -rw-r--r-- │ 1 │ pandorym │ staff │ 1529 │ May 23 11:25 │ LICENSE ║
31+
║ -rw-r--r-- │ 1 │ pandorym │ staff │ 16327 │ May 23 11:58 │ README.md ║
32+
║ drwxr-xr-x │ 76 │ pandorym │ staff │ 2432 │ May 23 12:02 │ dist ║
33+
║ drwxr-xr-x │ 634 │ pandorym │ staff │ 20288 │ May 23 11:54 │ node_modules ║
34+
║ -rw-r--r-- │ 1, │ pandorym │ staff │ 525688 │ May 23 11:52 │ package-lock.json ║
35+
║ -rw-r--r--@ │ 1 │ pandorym │ staff │ 2440 │ May 23 11:25 │ package.json ║
36+
║ drwxr-xr-x │ 27 │ pandorym │ staff │ 864 │ May 23 11:25 │ src ║
37+
║ drwxr-xr-x │ 20 │ pandorym │ staff │ 640 │ May 23 11:25 │ test ║
38+
╚═════════════╧═════╧══════════╧═══════╧════════╧══════════════╧═══════════════════╝
39+
`);
40+
/* eslint-enable no-restricted-syntax */
41+
});
42+
});

0 commit comments

Comments
 (0)