* Returns an array of values from an object using the property accessors * (and optional transform function) in each column
( item: DataItem, normalizedColumns: readonly NormalizedColumn[], )
| 175 | * (and optional transform function) in each column |
| 176 | */ |
| 177 | function getValuesFromItem( |
| 178 | item: DataItem, |
| 179 | normalizedColumns: readonly NormalizedColumn[], |
| 180 | ): unknown[] { |
| 181 | const values: unknown[] = []; |
| 182 | |
| 183 | if (normalizedColumns.length) { |
| 184 | for (const column of normalizedColumns) { |
| 185 | let value: unknown = item; |
| 186 | |
| 187 | for (const prop of column.prop) { |
| 188 | if (typeof value !== "object" || value === null) { |
| 189 | continue; |
| 190 | } |
| 191 | if (Array.isArray(value)) { |
| 192 | if (typeof prop === "number") value = value[prop]; |
| 193 | else { |
| 194 | throw new TypeError( |
| 195 | 'Property accessor is not of type "number"', |
| 196 | ); |
| 197 | } |
| 198 | } // I think this assertion is safe. Confirm? |
| 199 | else value = (value as Record<string, unknown>)[prop]; |
| 200 | } |
| 201 | |
| 202 | values.push(value); |
| 203 | } |
| 204 | } else { |
| 205 | if (Array.isArray(item)) { |
| 206 | values.push(...item); |
| 207 | } else if (typeof item === "object") { |
| 208 | throw new TypeError( |
| 209 | "No property accessor function was provided for object", |
| 210 | ); |
| 211 | } else { |
| 212 | values.push(item); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return values; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Converts an array of objects into a CSV string. |