Skip to content

Commit

Permalink
Horizontal borders draws wrongly
Browse files Browse the repository at this point in the history
  • Loading branch information
nhle-mgmtp committed Apr 21, 2021
1 parent 526a098 commit d5f8be5
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 39 deletions.
69 changes: 34 additions & 35 deletions src/drawTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,53 @@ import type {
TableConfig, Row,
} from './types/internal';

export default (
rows: Row[],
columnSizeIndex: number[],
rowSpanIndex: number[],
config: TableConfig,
): string => {
const {
drawHorizontalLine,
singleLine,
} = config;
/**
* Groups mapped rows into chunks by calculated heights
*/

let output: string;
let realRowIndex: number;
let rowHeight: number;
const groupRows = (rows: Row[], rowHeights: number[], config: TableConfig): Row => {
let startIndex = 0;

const rowCount = rows.length;
return rowHeights.map((rowHeight) => {
const chunk = rows.slice(startIndex, startIndex + rowHeight).map((row) => {
return drawRow(row, config);
}).join('');

realRowIndex = 0;
startIndex += rowHeight;

output = '';
return chunk;
});
};

if (drawHorizontalLine(realRowIndex, rowCount)) {
output += drawBorderTop(columnSizeIndex, config);
}
const shouldDrawBorderJoin = (rowIndex: number, rowCount: number, config: TableConfig): boolean => {
const {singleLine, drawHorizontalLine} = config;

rows.forEach((row, index0) => {
output += drawRow(row, config);
return !singleLine && rowIndex + 1 < rowCount && drawHorizontalLine(rowIndex + 1, rowCount);
};

if (!rowHeight) {
rowHeight = rowSpanIndex[realRowIndex];
export default (rows: Row[], columnWidths: number[], rowHeights: number[], config: TableConfig): string => {
const {
drawHorizontalLine,
} = config;

realRowIndex++;
}
const groupedRows = groupRows(rows, rowHeights, config);
const rowCount = groupedRows.length;
let output = '';

if (drawHorizontalLine(0, rowCount)) {
output += drawBorderTop(columnWidths, config);
}

rowHeight--;
groupedRows.forEach((row, rowIndex) => {
output += row;

if (
!singleLine &&
rowHeight === 0 &&
index0 !== rowCount - 1 &&
drawHorizontalLine(realRowIndex, rowCount)
) {
output += drawBorderJoin(columnSizeIndex, config);
if (shouldDrawBorderJoin(rowIndex, rowCount, config)) {
output += drawBorderJoin(columnWidths, config);
}
});

if (drawHorizontalLine(realRowIndex, rowCount)) {
output += drawBorderBottom(columnSizeIndex, config);
if (drawHorizontalLine(rowCount, rowCount)) {
output += drawBorderBottom(columnWidths, config);
}

return output;
Expand Down
10 changes: 6 additions & 4 deletions src/makeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ const makeColumns = (rows: Row[],
columnDefault?: ColumnUserConfig): Indexable<ColumnConfig> => {
const maximumColumnWidthIndex = calculateMaximumColumnWidthIndex(rows);

return rows[0].map((_cell, index) => {
return {
return rows[0].reduce<Record<number, ColumnConfig>>((result, _cell, index) => {
result[index] = {
alignment: 'left',
paddingLeft: 1,
paddingRight: 1,
truncate: Number.POSITIVE_INFINITY,
truncate: Number.MAX_VALUE,
width: maximumColumnWidthIndex[index],
wrapWord: false,
...columnDefault,
...columns?.[index],
};
});

return result;
}, {});
};

/**
Expand Down
95 changes: 95 additions & 0 deletions test/drawTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/* eslint-disable max-nested-callbacks */

import {
expect,
} from 'chai';
import makeConfig from '../src/makeConfig';
import table from '../src/table';
import type {
TableConfig,
} from '../src/types/internal';

const data = [
['Lorem ipsum', 'dolor sit'],
['amet', 'consectetur'],
['adipiscing', 'elit'],
];

const basicConfig = makeConfig(data, {
columnDefault: {
width: 5,
},
});

describe('drawTable', () => {
describe('drawHorizontalLine', () => {
context('only draw top and bottom borders', () => {
it('draws proper borders', () => {
const config: TableConfig = {
...basicConfig,
drawHorizontalLine: (index, size) => {
return index === 0 || index === size;
},
};

expect(table(data, config)).to.be.deep.equal(`
╔═══════╤═══════╗
║ Lorem │ dolor ║
║ ipsum │ sit ║
║ amet │ conse ║
║ │ ctetu ║
║ │ r ║
║ adipi │ elit ║
║ scing │ ║
╚═══════╧═══════╝
`.trimLeft());
});
});

context('only draw inner borders', () => {
it('draws proper borders', () => {
const config: TableConfig = {
...basicConfig,
drawHorizontalLine: (index, size) => {
return index > 0 && index < size;
},
};

expect(table(data, config)).to.be.deep.equal(`
║ Lorem │ dolor ║
║ ipsum │ sit ║
╟───────┼───────╢
║ amet │ conse ║
║ │ ctetu ║
║ │ r ║
╟───────┼───────╢
║ adipi │ elit ║
║ scing │ ║
`.trimLeft());
});
});

context('only draw top and next-to-last borders', () => {
it('draws proper borders', () => {
const config: TableConfig = {
...basicConfig,
drawHorizontalLine: (index, size) => {
return index === 0 || index === size - 1;
},
};

expect(table(data, config)).to.be.deep.equal(`
╔═══════╤═══════╗
║ Lorem │ dolor ║
║ ipsum │ sit ║
║ amet │ conse ║
║ │ ctetu ║
║ │ r ║
╟───────┼───────╢
║ adipi │ elit ║
║ scing │ ║
`.trimLeft());
});
});
});
});

0 comments on commit d5f8be5

Please sign in to comment.