Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/calculateMaximumColumnWidths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ export const calculateMaximumCellWidth = (cell: Cell): number => {
export const calculateMaximumColumnWidths = (rows: Row[], spanningCellConfigs: SpanningCellConfig[] = []): number[] => {
const columnWidths = new Array(rows[0].length).fill(0);
const rangeCoordinates = spanningCellConfigs.map(calculateRangeCoordinate);
const isSpanningCell = (rowIndex: number, columnIndex: number): boolean => {
return rangeCoordinates.some((rangeCoordinate) => {
return isCellInRange({col: columnIndex,
row: rowIndex}, rangeCoordinate);
});
};

rows.forEach((row, rowIndex) => {
row.forEach((cell, cellIndex) => {
if (isSpanningCell(rowIndex, cellIndex)) {
const range = rangeCoordinates.find((rangeCoordinate) => {
return isCellInRange({col: cellIndex,
row: rowIndex}, rangeCoordinate);
});

if (range && (range.topLeft.col !== range.bottomRight.col || range.topLeft.row !== rowIndex)) {
return;
}
columnWidths[cellIndex] = Math.max(columnWidths[cellIndex], calculateMaximumCellWidth(cell));
Expand Down
47 changes: 47 additions & 0 deletions test/calculateMaximumColumnWidths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
import {
calculateMaximumColumnWidths,
} from '../src/calculateMaximumColumnWidths';
import {
stringToRed,
} from './utils';

describe('calculateMaximumColumnWidths', () => {
context('all cells have different width', () => {
Expand All @@ -28,4 +31,48 @@ describe('calculateMaximumColumnWidths', () => {
expect(cellWidths[0]).to.equal(4);
});
});
context('spanning cells', () => {
it('includes cells spanning a single column', () => {
const cellWidths = calculateMaximumColumnWidths([
['a', 'Heading'],
['b', 'c'],
], [{col: 1,
colSpan: 1,
row: 0}]);

expect(cellWidths).to.deep.equal([1, 7]);
});
it('includes the first cell of a row span and ignores covered cells', () => {
const cellWidths = calculateMaximumColumnWidths([
['Heading', 'y'],
['This content is hidden', 'z'],
], [{col: 0,
row: 0,
rowSpan: 2}]);

expect(cellWidths).to.deep.equal([7, 1]);
});
it('ignores all cells in a span covering multiple columns', () => {
const cellWidths = calculateMaximumColumnWidths([
['Heading spanning two columns', 'Hidden content'],
['More hidden content', 'Also hidden'],
['a', 'bb'],
], [{col: 0,
colSpan: 2,
row: 0,
rowSpan: 2}]);

expect(cellWidths).to.deep.equal([1, 2]);
});
it('measures the display width of multiline single-column spans', () => {
const cellWidths = calculateMaximumColumnWidths([
[stringToRed('a\n中文')],
['x'],
], [{col: 0,
colSpan: 1,
row: 0}]);

expect(cellWidths).to.deep.equal([4]);
});
});
});
63 changes: 63 additions & 0 deletions test/spanningCells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,69 @@ import {
} from './utils';

describe('spanningCells', () => {
it('uses single-column spanning cell content to size the column', () => {
const result = table([
['Heading', 'Value'],
['a', '1'],
], {
spanningCells: [{alignment: 'center',
col: 0,
colSpan: 1,
row: 0}],
});

expectTable(result, `
╔═════════╤═══════╗
║ Heading │ Value ║
╟─────────┼───────╢
║ a │ 1 ║
╚═════════╧═══════╝
`);
});

it('sizes a column from a cell spanning multiple rows', () => {
const result = table([
['a', 'x'],
['Group', 'y'],
['This content is hidden', 'z'],
], {
spanningCells: [{col: 0,
row: 1,
rowSpan: 2}],
});

expectTable(result, `
╔═══════╤═══╗
║ a │ x ║
╟───────┼───╢
║ Group │ y ║
║ ├───╢
║ │ z ║
╚═══════╧═══╝
`);
});

it('respects explicit column widths for single-column spans', () => {
const result = table([
['abcdef'],
['x'],
], {
columns: [{width: 3}],
spanningCells: [{col: 0,
colSpan: 1,
row: 0}],
});

expectTable(result, `
╔═════╗
║ abc ║
║ def ║
╟─────╢
║ x ║
╚═════╝
`);
});

const baseUserCellConfig: CellUserConfig = {
alignment: 'left',
verticalAlignment: 'top',
Expand Down
Loading