Skip to content

Commit

Permalink
feat: single line mode (compact) (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pandorym authored and gajus committed May 23, 2019
1 parent 64364f8 commit 0627005
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 3 deletions.
1 change: 1 addition & 0 deletions .README/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Produces a string that represents array data in a text table.
{"gitdown": "include", "file": "./usage/column_width.md"}
{"gitdown": "include", "file": "./usage/custom_border.md"}
{"gitdown": "include", "file": "./usage/draw_horizontal_line.md"}
{"gitdown": "include", "file": "./usage/single_line_mode.md"}
{"gitdown": "include", "file": "./usage/padding_cell_content.md"}
{"gitdown": "include", "file": "./usage/predefined_border_templates.md"}
{"gitdown": "include", "file": "./usage/streaming.md"}
Expand Down
41 changes: 41 additions & 0 deletions .README/usage/single_line_mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
### Single Line Mode

Horizontal lines inside the table are not drawn.

```js
import {
table,
getBorderCharacters
} from 'table';

const data = [
['-rw-r--r--', '1', 'pandorym', 'staff', '1529', 'May 23 11:25', 'LICENSE'],
['-rw-r--r--', '1', 'pandorym', 'staff', '16327', 'May 23 11:58', 'README.md'],
['drwxr-xr-x', '76', 'pandorym', 'staff', '2432', 'May 23 12:02', 'dist'],
['drwxr-xr-x', '634', 'pandorym', 'staff', '20288', 'May 23 11:54', 'node_modules'],
['-rw-r--r--', '1,', 'pandorym', 'staff', '525688', 'May 23 11:52', 'package-lock.json'],
['-rw-r--r--@', '1', 'pandorym', 'staff', '2440', 'May 23 11:25', 'package.json'],
['drwxr-xr-x', '27', 'pandorym', 'staff', '864', 'May 23 11:25', 'src'],
['drwxr-xr-x', '20', 'pandorym', 'staff', '640', 'May 23 11:25', 'test'],
];

const config = {
singleLine: true
};

const output = table(data, config);
console.log(output);
```

```
╔═════════════╤═════╤══════════╤═══════╤════════╤══════════════╤═══════════════════╗
║ -rw-r--r-- │ 1 │ pandorym │ staff │ 1529 │ May 23 11:25 │ LICENSE ║
║ -rw-r--r-- │ 1 │ pandorym │ staff │ 16327 │ May 23 11:58 │ README.md ║
║ drwxr-xr-x │ 76 │ pandorym │ staff │ 2432 │ May 23 12:02 │ dist ║
║ drwxr-xr-x │ 634 │ pandorym │ staff │ 20288 │ May 23 11:54 │ node_modules ║
║ -rw-r--r-- │ 1, │ pandorym │ staff │ 525688 │ May 23 11:52 │ package-lock.json ║
║ -rw-r--r--@ │ 1 │ pandorym │ staff │ 2440 │ May 23 11:25 │ package.json ║
║ drwxr-xr-x │ 27 │ pandorym │ staff │ 864 │ May 23 11:25 │ src ║
║ drwxr-xr-x │ 20 │ pandorym │ staff │ 640 │ May 23 11:25 │ test ║
╚═════════════╧═════╧══════════╧═══════╧════════╧══════════════╧═══════════════════╝
```
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* [Column Width](#table-usage-column-width)
* [Custom Border](#table-usage-custom-border)
* [Draw Horizontal Line](#table-usage-draw-horizontal-line)
* [Single Line Mode](#table-usage-single-line-mode)
* [Padding Cell Content](#table-usage-padding-cell-content)
* [Predefined Border Templates](#table-usage-predefined-border-templates)
* [Streaming](#table-usage-streaming)
Expand Down Expand Up @@ -341,6 +342,49 @@ console.log(output);
```

<a name="table-usage-single-line-mode"></a>
### Single Line Mode

Horizontal lines inside the table are not drawn.

```js
import {
table,
getBorderCharacters
} from 'table';

const data = [
['-rw-r--r--', '1', 'pandorym', 'staff', '1529', 'May 23 11:25', 'LICENSE'],
['-rw-r--r--', '1', 'pandorym', 'staff', '16327', 'May 23 11:58', 'README.md'],
['drwxr-xr-x', '76', 'pandorym', 'staff', '2432', 'May 23 12:02', 'dist'],
['drwxr-xr-x', '634', 'pandorym', 'staff', '20288', 'May 23 11:54', 'node_modules'],
['-rw-r--r--', '1,', 'pandorym', 'staff', '525688', 'May 23 11:52', 'package-lock.json'],
['-rw-r--r--@', '1', 'pandorym', 'staff', '2440', 'May 23 11:25', 'package.json'],
['drwxr-xr-x', '27', 'pandorym', 'staff', '864', 'May 23 11:25', 'src'],
['drwxr-xr-x', '20', 'pandorym', 'staff', '640', 'May 23 11:25', 'test'],
];

const config = {
singleLine: true
};

const output = table(data, config);
console.log(output);
```

```
╔═════════════╤═════╤══════════╤═══════╤════════╤══════════════╤═══════════════════╗
║ -rw-r--r-- │ 1 │ pandorym │ staff │ 1529 │ May 23 11:25 │ LICENSE ║
║ -rw-r--r-- │ 1 │ pandorym │ staff │ 16327 │ May 23 11:58 │ README.md ║
║ drwxr-xr-x │ 76 │ pandorym │ staff │ 2432 │ May 23 12:02 │ dist ║
║ drwxr-xr-x │ 634 │ pandorym │ staff │ 20288 │ May 23 11:54 │ node_modules ║
║ -rw-r--r-- │ 1, │ pandorym │ staff │ 525688 │ May 23 11:52 │ package-lock.json ║
║ -rw-r--r--@ │ 1 │ pandorym │ staff │ 2440 │ May 23 11:25 │ package.json ║
║ drwxr-xr-x │ 27 │ pandorym │ staff │ 864 │ May 23 11:25 │ src ║
║ drwxr-xr-x │ 20 │ pandorym │ staff │ 640 │ May 23 11:25 │ test ║
╚═════════════╧═════╧══════════╧═══════╧════════╧══════════════╧═══════════════════╝
```

<a name="table-usage-padding-cell-content"></a>
### Padding Cell Content

Expand Down
5 changes: 3 additions & 2 deletions src/drawTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import drawRow from './drawRow';
* @param {Array} columnSizeIndex
* @param {Array} rowSpanIndex
* @param {Function} drawHorizontalLine
* @param {boolean} singleLine
* @returns {string}
*/
export default (rows, border, columnSizeIndex, rowSpanIndex, drawHorizontalLine) => {
export default (rows, border, columnSizeIndex, rowSpanIndex, drawHorizontalLine, singleLine) => {
let output;
let realRowIndex;
let rowHeight;
Expand All @@ -39,7 +40,7 @@ export default (rows, border, columnSizeIndex, rowSpanIndex, drawHorizontalLine)

rowHeight--;

if (rowHeight === 0 && index0 !== rowCount - 1 && drawHorizontalLine(realRowIndex, rowCount)) {
if (!singleLine && rowHeight === 0 && index0 !== rowCount - 1 && drawHorizontalLine(realRowIndex, rowCount)) {
output += drawBorderJoin(columnSizeIndex, border);
}
});
Expand Down
4 changes: 4 additions & 0 deletions src/makeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,9 @@ export default (rows, userConfig = {}) => {
};
}

if (config.singleLine === undefined) {
config.singleLine = false;
}

return config;
};
3 changes: 2 additions & 1 deletion src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import truncateTableData from './truncateTableData';
* @property {table~columns[]} columns Column specific configuration.
* @property {table~columns} columnDefault Default values for all columns. Column specific settings overwrite the default values.
* @property {table~drawHorizontalLine} drawHorizontalLine
* @property {table~singleLine} singleLine Horizontal lines inside the table are not drawn.
*/

/**
Expand Down Expand Up @@ -91,5 +92,5 @@ export default (data, userConfig = {}) => {

const cellWidthIndex = calculateCellWidthIndex(rows[0]);

return drawTable(rows, config.border, cellWidthIndex, rowHeightIndex, config.drawHorizontalLine);
return drawTable(rows, config.border, cellWidthIndex, rowHeightIndex, config.drawHorizontalLine, config.singleLine);
};
42 changes: 42 additions & 0 deletions test/README/usage/single_line_mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
table
} from '../../../src';
import expectTable from './expectTable';

describe('README.md usage/', () => {
it('single_line_mode', () => {
const data = [
['-rw-r--r--', '1', 'pandorym', 'staff', '1529', 'May 23 11:25', 'LICENSE'],
['-rw-r--r--', '1', 'pandorym', 'staff', '16327', 'May 23 11:58', 'README.md'],
['drwxr-xr-x', '76', 'pandorym', 'staff', '2432', 'May 23 12:02', 'dist'],
['drwxr-xr-x', '634', 'pandorym', 'staff', '20288', 'May 23 11:54', 'node_modules'],
['-rw-r--r--', '1,', 'pandorym', 'staff', '525688', 'May 23 11:52', 'package-lock.json'],
['-rw-r--r--@', '1', 'pandorym', 'staff', '2440', 'May 23 11:25', 'package.json'],
['drwxr-xr-x', '27', 'pandorym', 'staff', '864', 'May 23 11:25', 'src'],
['drwxr-xr-x', '20', 'pandorym', 'staff', '640', 'May 23 11:25', 'test']
];

const config = {
singleLine: true
};

const output = table(data, config);

// console.log(output);

/* eslint-disable no-restricted-syntax */
expectTable(output, `
╔═════════════╤═════╤══════════╤═══════╤════════╤══════════════╤═══════════════════╗
║ -rw-r--r-- │ 1 │ pandorym │ staff │ 1529 │ May 23 11:25 │ LICENSE ║
║ -rw-r--r-- │ 1 │ pandorym │ staff │ 16327 │ May 23 11:58 │ README.md ║
║ drwxr-xr-x │ 76 │ pandorym │ staff │ 2432 │ May 23 12:02 │ dist ║
║ drwxr-xr-x │ 634 │ pandorym │ staff │ 20288 │ May 23 11:54 │ node_modules ║
║ -rw-r--r-- │ 1, │ pandorym │ staff │ 525688 │ May 23 11:52 │ package-lock.json ║
║ -rw-r--r--@ │ 1 │ pandorym │ staff │ 2440 │ May 23 11:25 │ package.json ║
║ drwxr-xr-x │ 27 │ pandorym │ staff │ 864 │ May 23 11:25 │ src ║
║ drwxr-xr-x │ 20 │ pandorym │ staff │ 640 │ May 23 11:25 │ test ║
╚═════════════╧═════╧══════════╧═══════╧════════╧══════════════╧═══════════════════╝
`);
/* eslint-enable no-restricted-syntax */
});
});

0 comments on commit 0627005

Please sign in to comment.