All files / packages/sds-metadata/src/parsers csv-parser.ts

0% Statements 0/4
100% Branches 0/0
100% Functions 0/0
0% Lines 0/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                                                               
import fs from 'fs';
import { parse } from 'csv-parse';
import { CSVRowProcessor } from '../types.js';
 
/**
 * Process a CSV file and extract data using the provided processor
 * @param filePath Path to the CSV file
 * @param processor The processor to use for each row
 * @returns A promise that resolves to the processor results
 */
export async function parseCSVFile<T>(
  filePath: string,
  processor: CSVRowProcessor<T>
): Promise<T> {
  // Create a readable stream from the CSV file
  const fileStream = fs.createReadStream(filePath);
  
  // Create a parser with headers enabled
  const parser = parse({    
    
    delimiter: ','
  });
 
  // Process each row from the CSV
  for await (const row of fileStream.pipe(parser)) {
    processor.processRow(row);
  }
 
  // Return the final results from the processor
  return processor.getResults();
}