MCPcopy Create free account
hub / github.com/Datakitpage/Datakit / parseCSVText

Function parseCSVText

frontend/src/hooks/stream/useStreamingCSVParser.ts:349–403  ·  view source on GitHub ↗
(
    text: string,
    options: {
      delimiter?: string;
      header?: boolean;
    } = {}
  )

Source from the content-addressed store, hash-verified

347 * Useful for testing or small data snippets
348 */
349 const parseCSVText = (
350 text: string,
351 options: {
352 delimiter?: string;
353 header?: boolean;
354 } = {}
355 ): CSVParseResult => {
356 try {
357 const { delimiter = ',', header = true } = options;
358
359 // Use PapaParse directly for small text fragments
360 const parseResult = Papa.parse(text, {
361 delimiter,
362 header: false, // We'll handle headers ourselves for consistency
363 skipEmptyLines: true
364 });
365
366 // Extract data
367 const rawData = parseResult.data as string[][];
368
369 if (rawData.length === 0) {
370 throw new Error('No data found in CSV text');
371 }
372
373 // Extract headers and data
374 const headers = header ? rawData[0] : rawData[0].map((_, i) => `Column ${i + 1}`);
375 const data = header ? rawData : [headers, ...rawData];
376
377 // Detect column types
378 const columnTypes = detectColumnTypes(
379 header ? rawData.slice(1) : rawData,
380 headers
381 );
382
383 // Prepare result
384 const result: CSVParseResult = {
385 data,
386 columnTypes,
387 fileName: 'csv-text',
388 rowCount: data.length - 1, // Exclude header row
389 columnCount: headers.length,
390 stats: {
391 bytesProcessed: text.length,
392 rowsProcessed: data.length - 1,
393 chunksProcessed: 1
394 }
395 };
396
397 return result;
398 } catch (err) {
399 const errorMessage = err instanceof Error ? err.message : 'Unknown error parsing CSV text';
400 setError(errorMessage);
401 throw err;
402 }
403 };
404
405 // Helper function to format time
406 const formatTime = (seconds: number): string => {

Callers

nothing calls this directly

Calls 1

detectColumnTypesFunction · 0.70

Tested by

no test coverage detected