(
data: string[][],
sourceType: DataSourceType = DataSourceType.CSV
)
| 206 | * Detect column types from a data sample |
| 207 | */ |
| 208 | const detectColumnTypes = ( |
| 209 | data: string[][], |
| 210 | sourceType: DataSourceType = DataSourceType.CSV |
| 211 | ): ColumnType[] => { |
| 212 | try { |
| 213 | if (sourceType === DataSourceType.CSV) { |
| 214 | // Use CSV parser's type detection |
| 215 | if (data.length < 2) return []; |
| 216 | |
| 217 | return csvParser.parseCSVText( |
| 218 | data.map(row => row.join(',')).join('\n'), |
| 219 | { header: true } |
| 220 | ).columnTypes; |
| 221 | } |
| 222 | else if (sourceType === DataSourceType.JSON) { |
| 223 | // Use JSON parser's type detection |
| 224 | if (data.length < 2) return []; |
| 225 | |
| 226 | const headers = data[0]; |
| 227 | const jsonData = data.slice(1).map(row => { |
| 228 | const obj: Record<string, string> = {}; |
| 229 | headers.forEach((header, index) => { |
| 230 | obj[header] = row[index] || ''; |
| 231 | }); |
| 232 | return obj; |
| 233 | }); |
| 234 | |
| 235 | // Extract column types from jsonParser's result |
| 236 | const jsonResult = jsonParser.parseJsonText(JSON.stringify(jsonData)); |
| 237 | return jsonResult.columnTypes; |
| 238 | } |
| 239 | else { |
| 240 | return []; |
| 241 | } |
| 242 | } catch (err) { |
| 243 | console.error('Error detecting column types:', err); |
| 244 | return []; |
| 245 | } |
| 246 | }; |
| 247 | |
| 248 | return { |
| 249 | parseFile, |
nothing calls this directly
no outgoing calls
no test coverage detected