Skip to content

Commit

Permalink
fix(row-separator): make row separator optional (#373)
Browse files Browse the repository at this point in the history
* fix(row-separator): make row separator symbols optional

* chore(deps): update dependencies
  • Loading branch information
ayonious committed Jun 12, 2021
1 parent 63855ae commit 325db51
Show file tree
Hide file tree
Showing 9 changed files with 6,395 additions and 46 deletions.
8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -28,8 +28,8 @@
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@types/jest": "^26.0.23",
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"@typescript-eslint/parser": "^4.26.1",
"chalk": "^4.1.1",
"eslint": "^7.28.0",
"eslint-config-airbnb-base": "^14.2.1",
Expand All @@ -38,10 +38,10 @@
"eslint-plugin-prettier": "^3.4.0",
"husky": "^6.0.0",
"jest": "^27.0.4",
"prettier": "^2.3.0",
"prettier": "^2.3.1",
"pretty-quick": "^3.1.0",
"semantic-release": "^17.4.3",
"ts-jest": "^27.0.2",
"ts-jest": "^27.0.3",
"typescript": "^4.3.2"
},
"homepage": "https://console-table.netlify.app",
Expand Down
22 changes: 12 additions & 10 deletions src/internalTable/internal-table-printer.ts
Expand Up @@ -177,18 +177,20 @@ const renderTableEnding = (table: TableInternal): string[] => {

const renderRowSeparator = (table: TableInternal, row: Row): string[] => {
const ret: string[] = [];
let lastRowIndex = table.rows.length - 1;
let rowIndex = table.rows.indexOf(row);
let addSeparator = row.separator !== undefined ? row.separator : table.rowSeparator;

if (rowIndex > -1 && rowIndex < lastRowIndex && addSeparator) {
ret.push(renderTableHorizontalBorders(
table.tableStyle.rowSeparator,
table.columns.map((m) => m.length || DEFAULT_COLUMN_LEN)
));
const lastRowIndex = table.rows.length - 1;
const currentRowIndex = table.rows.indexOf(row);

if (currentRowIndex !== lastRowIndex && row.separator) {
// β•Ÿβ•β•β•β•β•β•β•β•¬β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•¬β•β•β•β•β•β•β•β•β•’
ret.push(
renderTableHorizontalBorders(
table.tableStyle.rowSeparator,
table.columns.map((m) => m.length || DEFAULT_COLUMN_LEN)
)
);
}
return ret;
}
};

export const renderTable = (table: TableInternal): string => {
preProcessColumns(table); // enable / disable cols, find maxLn of each col/ computed Columns
Expand Down
9 changes: 6 additions & 3 deletions src/internalTable/internal-table.ts
Expand Up @@ -10,7 +10,7 @@ import {
DEFAULT_TABLE_STYLE,
DEFAULT_ROW_ALIGNMENT,
DEFAULT_ROW_FONT_COLOR,
DEFAULT_ROW_SEPARATOR
DEFAULT_ROW_SEPARATOR,
} from '../utils/table-constants';
import {
createColumFromComputedColumn,
Expand Down Expand Up @@ -62,7 +62,8 @@ class TableInternal {
this.enabledColumns = options?.enabledColumns || this.enabledColumns;
this.disabledColumns = options?.disabledColumns || this.disabledColumns;
this.computedColumns = options?.computedColumns || this.computedColumns;
this.columns = options?.columns?.map(rawColumnToInternalColumn) || this.columns;
this.columns =
options?.columns?.map(rawColumnToInternalColumn) || this.columns;
this.rowSeparator = options?.rowSeparator || this.rowSeparator;
}

Expand Down Expand Up @@ -115,7 +116,9 @@ class TableInternal {
createRow(
options?.color || DEFAULT_ROW_FONT_COLOR,
text,
options?.separator
options?.separator !== undefined
? options?.separator
: this.rowSeparator
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/common.ts
Expand Up @@ -8,6 +8,6 @@ export interface Dictionary {
}
export interface Row {
color: COLOR;
separator?: boolean;
separator: boolean;
text: Dictionary;
}
2 changes: 1 addition & 1 deletion src/models/internal-table.ts
Expand Up @@ -27,5 +27,5 @@ export type TableStyleDetails = {
headerBottom: TableLineDetails;
tableBottom: TableLineDetails;
vertical: string;
rowSeparator: TableLineDetails;
rowSeparator?: TableLineDetails;
};
5 changes: 3 additions & 2 deletions src/utils/table-helpers.ts
Expand Up @@ -8,6 +8,7 @@ import {
DEFAULT_COLUMN_LEN,
DEFAULT_ROW_ALIGNMENT,
DEFAULT_ROW_SEPARATOR,
DEFAULT_HEADER_FONT_COLOR,
} from './table-constants';

const max = (a: number, b: number) => Math.max(a, b);
Expand Down Expand Up @@ -82,7 +83,7 @@ export const createColumFromComputedColumn = (
export const createRow = (
color: COLOR,
text: Dictionary,
separator?: boolean
separator: boolean
): Row => ({
color,
separator,
Expand Down Expand Up @@ -127,7 +128,7 @@ export const renderTableHorizontalBorders = (
};

export const createHeaderAsRow = (createRowFn: any, columns: Column[]): Row => {
const headerColor: COLOR = 'white_bold';
const headerColor: COLOR = DEFAULT_HEADER_FONT_COLOR;
const row: Row = createRowFn(headerColor, {}, false);
columns.forEach((column) => {
row.text[column.name] = column.title;
Expand Down
12 changes: 0 additions & 12 deletions test/internalTable/borderStyle.test.ts
Expand Up @@ -33,12 +33,6 @@ describe('Example: Check if borders are styled properly', () => {
other: '═',
},
vertical: 'β•‘',
rowSeparator: {
left: 'β•Ÿ',
mid: '╬',
right: 'β•’',
other: '═',
},
},
columns: [
{ name: 'index', alignment: 'left' },
Expand Down Expand Up @@ -107,12 +101,6 @@ describe('Example: Check if borders are styled properly', () => {
other: '\x1b[31m═\x1b[0m',
},
vertical: '\x1b[31mβ•‘\x1b[0m',
rowSeparator: {
left: '\x1b[31mβ•Ÿ\x1b[0m',
mid: '\x1b[31m╬\x1b[0m',
right: '\x1b[31mβ•’\x1b[0m',
other: '\x1b[31m═\x1b[0m',
},
},
columns: [
{ name: 'index', alignment: 'left' },
Expand Down
49 changes: 36 additions & 13 deletions test/internalTable/rowSeparator.test.ts
Expand Up @@ -29,6 +29,7 @@ describe('Testing Row separator', () => {
'β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜',
];
expect(returned).toBe(expected.join('\n'));
p.printTable();
});

it('Batch Row default separator is working', () => {
Expand All @@ -53,6 +54,7 @@ describe('Testing Row separator', () => {
'β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜',
];
expect(returned).toBe(expected.join('\n'));
p.printTable();
});

it('Batch Row table separator option is working', () => {
Expand All @@ -78,17 +80,21 @@ describe('Testing Row separator', () => {
'β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜',
];
expect(returned).toBe(expected.join('\n'));
p.printTable();
});

it('Batch Row table separator override is working', () => {
// Create a table
const p = new Table({ rowSeparator: true });

p.addRows([
// adding multiple rows are possible
{ index: 3, text: 'override table row separator', value: 100 },
{ index: 4, text: 'override table row separator', value: 300 },
], { separator: false });
p.addRows(
[
// adding multiple rows are possible
{ index: 3, text: 'override table row separator', value: 100 },
{ index: 4, text: 'override table row separator', value: 300 },
],
{ separator: false }
);

// print
const returned = renderTable(p.table);
Expand All @@ -102,6 +108,7 @@ describe('Testing Row separator', () => {
'β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜',
];
expect(returned).toBe(expected.join('\n'));
p.printTable();
});

it('Batch Row separator is working', () => {
Expand Down Expand Up @@ -134,16 +141,31 @@ describe('Testing Row separator', () => {

it('Batch Row separator combined with sorting', () => {
// Create a table and sort by index
const p = new Table({ sort: (row1, row2) => row1.index - row2.index, rowSeparator: true });
const p = new Table({
sort: (row1, row2) => row1.index - row2.index,
rowSeparator: true,
});

//Row with index 1 will have separator because it inherits from Table options
// Row with index 1 will have separator because it inherits from Table options
p.addRow({ index: 1, text: 'row inherit separator', value: 100 });
p.addRow({ index: 4, text: 'row without separator', value: 100 }, { separator: false });
//Row with index 5 will be last row so separator will be ignored anyway
p.addRow({ index: 5, text: 'row with separator', value: 100 }, { separator: true });
p.addRow({ index: 2, text: 'row with separator', value: 100 }, { separator: true });
p.addRow({ index: 3, text: 'row without separator', value: 100 }, { separator: false });

p.addRow(
{ index: 4, text: 'row without separator', value: 100 },
{ separator: false }
);
// Row with index 5 will be last row so separator will be ignored anyway
p.addRow(
{ index: 5, text: 'row with separator', value: 100 },
{ separator: true }
);
p.addRow(
{ index: 2, text: 'row with separator', value: 100 },
{ separator: true }
);
p.addRow(
{ index: 3, text: 'row without separator', value: 100 },
{ separator: false }
);

// print
const returned = renderTable(p.table);

Expand All @@ -161,5 +183,6 @@ describe('Testing Row separator', () => {
'β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜',
];
expect(returned).toBe(expected.join('\n'));
p.printTable();
});
});

0 comments on commit 325db51

Please sign in to comment.