({
rows,
columns = [],
}: Pick<Table, 'columns' | 'rows'>)
| 39 | |
| 40 | // Determine effective columns based on the input rows and optional columns parameter |
| 41 | export function columnsToStringArray({ |
| 42 | rows, |
| 43 | columns = [], |
| 44 | }: Pick<Table, 'columns' | 'rows'>): string[] { |
| 45 | const firstRow = rows.at(0); |
| 46 | const primitiveRows = Array.isArray(firstRow); |
| 47 | |
| 48 | if (typeof columns.at(0) === 'string' && !primitiveRows) { |
| 49 | throw new Error('invalid union type. Caught by model parsing.'); |
| 50 | } |
| 51 | |
| 52 | if (columns.length === 0) { |
| 53 | if (Array.isArray(firstRow)) { |
| 54 | return firstRow.map((_, idx) => String(idx)); |
| 55 | } |
| 56 | return Object.keys(firstRow as object); |
| 57 | } |
| 58 | |
| 59 | // columns = ['right', 'left']; row = ['100 ms', '200 ms'] => 1,2,3 |
| 60 | if (typeof columns.at(0) === 'string') { |
| 61 | return columns.map(String); |
| 62 | } |
| 63 | |
| 64 | // columns = [ |
| 65 | // {key: 'prop1' label: 'Property 1'}, |
| 66 | // {key: 'prop2'} |
| 67 | // ]; row = [ {prop1: '100 ms', prop2: '200 ms'}] => Property 1, Prop2 |
| 68 | const cols = columns as TableColumnObject[]; |
| 69 | return cols.map(({ label, key }) => label ?? capitalize(key)); |
| 70 | } |
| 71 | |
| 72 | export function getColumnAlignmentForKeyAndIndex( |
| 73 | targetKey: string, |
no test coverage detected