({ rows, columns = [] }: Table)
| 8 | import { capitalize } from '../case-conversions.js'; |
| 9 | |
| 10 | export function rowToStringArray({ rows, columns = [] }: Table): string[][] { |
| 11 | if (Array.isArray(rows.at(0)) && typeof columns.at(0) === 'object') { |
| 12 | throw new TypeError( |
| 13 | 'Column can`t be object when rows are primitive values', |
| 14 | ); |
| 15 | } |
| 16 | |
| 17 | return rows.map(row => { |
| 18 | // row = ['100 ms', '200 ms'] |
| 19 | if (Array.isArray(row)) { |
| 20 | return row.map(String); |
| 21 | } |
| 22 | |
| 23 | // row = { prop1: '100 ms', prop2: '200 ms' } |
| 24 | const objectRow = row; |
| 25 | |
| 26 | // columns [] || column.at(0) = 'center' |
| 27 | if (columns.length === 0 || typeof columns.at(0) === 'string') { |
| 28 | return Object.values(objectRow).map(value => |
| 29 | value == null ? '' : String(value), |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | // column = {key: 'prop1'} |
| 34 | return (columns as TableColumnObject[]).map(({ key }): string => |
| 35 | objectRow[key] == null ? '' : String(objectRow[key]), |
| 36 | ); |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | // Determine effective columns based on the input rows and optional columns parameter |
| 41 | export function columnsToStringArray({ |
no outgoing calls
no test coverage detected