Skip to content

Commit

Permalink
fix(table): table fails if word is longer than maxCellWidth (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 29, 2020
1 parent 6d00cc3 commit b6c5f07
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/table/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,16 @@ export class Table extends Array<IRow> {
protected renderCell( cell: Cell, maxLength: number ): { current: string, next: Cell } {

const length: number = Math.min( maxLength, stripeColors( cell.toString() ).length );
const words: string = consumeWords( length, cell.toString() );
const next = cell.slice( words.length + 1 );
let words: string = consumeWords( length, cell.toString() );

// break word if word is longer than max length
const breakWord = stripeColors( words ).length > length;
if ( breakWord ) {
words = words.slice( 0, length );
}

// get next content and remove leading space if breakWord is not true
const next = cell.slice( words.length + ( breakWord ? 0 : 1 ) );
const fillLength = maxLength - stripeColors( words ).length;
const current = words + ' '.repeat( fillLength );

Expand Down
24 changes: 24 additions & 0 deletions packages/table/test/table_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ cell1 cell2 cell3
`.slice( 1 ) );
} );

Deno.test( 'simple table with word break', () => {
assertEquals(
Table.from( [
[ 'cell1', 'cell2 cell2', 'cell3' ],
[ 'cell1', 'cell2', 'cell3' ],
[ 'cell1', 'cell2', 'cell3 cell3' ]
] )
.maxCellWidth( 4 )
.padding( 1 )
.toString(),
`
cell cell cell
1 2 3
cell
2
cell cell cell
1 2 3
cell cell cell
1 2 3
cell
3
`.slice( 1 ) );
} );

Deno.test( 'simple border table', () => {
assertEquals(
Table.from( [
Expand Down

0 comments on commit b6c5f07

Please sign in to comment.