(tableData: Table)
| 105 | } |
| 106 | |
| 107 | export function getColumnAlignments(tableData: Table): TableAlignment[] { |
| 108 | const { rows, columns = [] } = tableData; |
| 109 | |
| 110 | // this is caught by the table schema in @code-pushup/models |
| 111 | if (rows.at(0) == null) { |
| 112 | throw new Error('first row can`t be undefined.'); |
| 113 | } |
| 114 | |
| 115 | if (Array.isArray(rows.at(0))) { |
| 116 | const firstPrimitiveRow = rows.at(0) as TableCellValue[]; |
| 117 | return Array.from({ length: firstPrimitiveRow.length }).map((_, idx) => |
| 118 | getColumnAlignmentForIndex(idx, columns as TableColumnPrimitive[]), |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | const biggestRow = rows |
| 123 | .toSorted((a, b) => Object.keys(a).length - Object.keys(b).length) |
| 124 | .at(-1); |
| 125 | if (columns.length > 0) { |
| 126 | return columns.map((column, idx) => |
| 127 | typeof column === 'string' |
| 128 | ? column |
| 129 | : getColumnAlignmentForKeyAndIndex( |
| 130 | column.key, |
| 131 | idx, |
| 132 | columns as TableColumnObject[], |
| 133 | ), |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | return Object.keys(biggestRow ?? {}).map(_ => 'center'); |
| 138 | } |
no test coverage detected