@@ -18,9 +18,11 @@ function transposeInPlace (matrix, maxSize) {
18
18
const rowI = getRow ( matrix , i ) ;
19
19
for ( let j = 0 ; j < i ; j ++ ) {
20
20
const rowJ = getRow ( matrix , j ) ;
21
- const temp = rowI [ j ] ;
22
- rowI [ j ] = rowJ [ i ] ;
23
- rowJ [ i ] = temp ;
21
+ if ( rowI [ j ] || rowJ [ i ] ) {
22
+ const temp = rowI [ j ] ;
23
+ rowI [ j ] = rowJ [ i ] ;
24
+ rowJ [ i ] = temp ;
25
+ }
24
26
}
25
27
}
26
28
}
@@ -34,10 +36,17 @@ function putCellIntoLayout (cell, layout, baseRow, baseCol) {
34
36
}
35
37
}
36
38
39
+ function getOrInitOffset ( offsets , index ) {
40
+ if ( offsets [ index ] === undefined ) {
41
+ offsets [ index ] = ( index === 0 ) ? 0 : 1 + getOrInitOffset ( offsets , index - 1 ) ;
42
+ }
43
+ return offsets [ index ] ;
44
+ }
45
+
37
46
function updateOffset ( offsets , base , span , value ) {
38
47
offsets [ base + span ] = Math . max (
39
- offsets [ base + span ] || 0 ,
40
- offsets [ base ] + value
48
+ getOrInitOffset ( offsets , base + span ) ,
49
+ getOrInitOffset ( offsets , base ) + value
41
50
) ;
42
51
}
43
52
@@ -82,19 +91,27 @@ function tableToString (tableRows, rowSpacing, colSpacing) {
82
91
for ( let x = 0 ; x < colNumber ; x ++ ) {
83
92
let y = 0 ;
84
93
let cell ;
85
- while ( y < rowNumber && ( cell = layout [ x ] [ y ] ) ) {
86
- if ( ! cell . rendered ) {
87
- let cellWidth = 0 ;
88
- for ( let j = 0 ; j < cell . lines . length ; j ++ ) {
89
- const line = cell . lines [ j ] ;
90
- const lineOffset = rowOffsets [ y ] + j ;
91
- outputLines [ lineOffset ] = ( outputLines [ lineOffset ] || '' ) . padEnd ( colOffsets [ x ] ) + line ;
92
- cellWidth = ( line . length > cellWidth ) ? line . length : cellWidth ;
94
+ const rowsInThisColumn = Math . min ( rowNumber , layout [ x ] . length ) ;
95
+ while ( y < rowsInThisColumn ) {
96
+ cell = layout [ x ] [ y ] ;
97
+ if ( cell ) {
98
+ if ( ! cell . rendered ) {
99
+ let cellWidth = 0 ;
100
+ for ( let j = 0 ; j < cell . lines . length ; j ++ ) {
101
+ const line = cell . lines [ j ] ;
102
+ const lineOffset = rowOffsets [ y ] + j ;
103
+ outputLines [ lineOffset ] = ( outputLines [ lineOffset ] || '' ) . padEnd ( colOffsets [ x ] ) + line ;
104
+ cellWidth = ( line . length > cellWidth ) ? line . length : cellWidth ;
105
+ }
106
+ updateOffset ( colOffsets , x , cell . colspan , cellWidth + colSpacing ) ;
107
+ cell . rendered = true ;
93
108
}
94
- updateOffset ( colOffsets , x , cell . colspan , cellWidth + colSpacing ) ;
95
- cell . rendered = true ;
109
+ y += cell . rowspan ;
110
+ } else {
111
+ const lineOffset = rowOffsets [ y ] ;
112
+ outputLines [ lineOffset ] = ( outputLines [ lineOffset ] || '' ) ;
113
+ y ++ ;
96
114
}
97
- y += cell . rowspan ;
98
115
}
99
116
}
100
117
0 commit comments