(filePath: string, options?: CsvInputOptionsNode)
| 49 | * ``` |
| 50 | */ |
| 51 | const $readCSV = async (filePath: string, options?: CsvInputOptionsNode): Promise<DataFrame> => { |
| 52 | const frameConfig = options?.frameConfig || {} |
| 53 | const hasStringType = frameConfig.dtypes?.includes("string") |
| 54 | |
| 55 | if (filePath.startsWith("http") || filePath.startsWith("https")) { |
| 56 | return new Promise((resolve, reject) => { |
| 57 | let hasError = false; |
| 58 | const optionsWithDefaults = { |
| 59 | header: true, |
| 60 | dynamicTyping: !hasStringType, |
| 61 | skipEmptyLines: 'greedy', |
| 62 | delimiter: ",", |
| 63 | ...options, |
| 64 | } |
| 65 | |
| 66 | const dataStream = request.get(filePath); |
| 67 | // reject any non-2xx status codes |
| 68 | dataStream.on('response', (response: any) => { |
| 69 | if (response.statusCode < 200 || response.statusCode >= 300) { |
| 70 | hasError = true; |
| 71 | reject(new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`)); |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | const parseStream: any = Papa.parse(Papa.NODE_STREAM_INPUT, optionsWithDefaults as any); |
| 76 | dataStream.pipe(parseStream); |
| 77 | |
| 78 | const data: any = []; |
| 79 | parseStream.on("data", (chunk: any) => { |
| 80 | if (!hasError) { |
| 81 | data.push(chunk); |
| 82 | } |
| 83 | }); |
| 84 | |
| 85 | parseStream.on("error", (error: any) => { |
| 86 | hasError = true; |
| 87 | reject(new Error(`Failed to parse CSV: ${error.message}`)); |
| 88 | }); |
| 89 | |
| 90 | parseStream.on("finish", () => { |
| 91 | if (hasError) return; |
| 92 | |
| 93 | if (!data || data.length === 0) { |
| 94 | reject(new Error('No data found in CSV file')); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | try { |
| 99 | const df = new DataFrame(data, frameConfig); |
| 100 | resolve(df); |
| 101 | } catch (error) { |
| 102 | const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; |
| 103 | reject(new Error(`Failed to create DataFrame: ${errorMessage}`)); |
| 104 | } |
| 105 | }); |
| 106 | }); |
| 107 | |
| 108 | } else { |
nothing calls this directly
no test coverage detected