/** * CSV Parse Utilities * * Shared parsing helpers used by both sync (parseCsv) and streaming (CsvParserStream) * parsers to ensure consistent behavior: * * - Header processing: Handle headers option (true/array/transform) * - Column validation: Check row length against expected column count * - Row-to-object conversion: Transform string[] to Record * - Dynamic typing: Apply type coercion based on configuration * * These utilities are extracted to avoid code duplication between * the batch parser (parse.ts) and the streaming parser (csv-stream.ts). */ import type { CsvRecordError, OnSkipCallback } from "../types.js"; import { type HeaderArray } from "../utils/row.js"; /** * Options for header processing */ interface HeaderProcessOptions { /** Headers configuration: true, array, or function */ headers: boolean | string[] | ((row: string[]) => (string | null | undefined)[]); /** Whether to group columns by name (affects originalHeaders computation) */ groupColumnsByName?: boolean; } /** * Options for column validation */ interface ColumnValidationOptions { /** Strategy for rows with fewer columns than expected */ columnLess: "error" | "pad"; /** Strategy for rows with more columns than expected */ columnMore: "error" | "truncate" | "keep"; } /** * Column validation result */ interface ColumnValidationResult { /** Whether the row is valid */ isValid: boolean; /** Error code if invalid: 'TooManyFields' or 'TooFewFields' */ errorCode?: "TooManyFields" | "TooFewFields"; /** Error message if invalid */ reason?: string; /** Whether the row was modified (padded or trimmed) */ modified: boolean; /** Extra columns when columnMismatch.more is 'keep' */ extras?: string[]; } /** * Result of processing headers */ interface HeaderProcessResult { /** The processed (deduplicated) headers */ headers: HeaderArray; /** The original (non-deduplicated) headers, for groupColumnsByName support. Null when groupColumnsByName is false. */ originalHeaders: HeaderArray | null; /** Map of renamed headers (new name -> original name) */ renamedHeaders: Record | null; /** Whether the current row should be skipped (was used as headers) */ skipCurrentRow: boolean; } /** * Process headers from first row or configuration. * Shared logic between parseCsv and CsvParserStream. * * @param row - The current row being processed * @param options - Header processing options * @param existingHeaders - Already configured headers (for array case) * @returns Processing result or null if headers not applicable */ export declare function processHeaders(row: string[], options: HeaderProcessOptions, existingHeaders: HeaderArray | null): HeaderProcessResult | null; /** * Validate and adjust row column count against expected headers. * Shared logic between parseCsv and CsvParserStream. * * @param row - The row to validate (will be modified in place if needed) * @param expectedCols - Expected number of columns (from headers) * @param options - Validation options * @returns Validation result */ export declare function validateAndAdjustColumns(row: string[], expectedCols: number, options: ColumnValidationOptions): ColumnValidationResult; /** * Create a safe onSkip handler that catches errors from user callback. * * The onSkip callback is user-provided and may throw errors. We wrap it * to prevent callback errors from interrupting parsing. Errors in the * callback are silently ignored since there's no good way to surface them * in the sync parsing context. * * For better error visibility in async/streaming contexts, consider * emitting a warning event on the stream instead. */ export declare function createOnSkipHandler(onSkip: OnSkipCallback | undefined): ((error: CsvRecordError, record: string[] | null) => void) | null; /** * Convert a row array to an object, optionally grouping duplicate column names. * Unified function that handles both normal and grouped modes. * * @param row - The row values as an array * @param headers - The deduplicated header names * @param originalHeaders - The original (non-deduplicated) headers for grouping * @param groupColumnsByName - Whether to group duplicate column names * @returns Object with header keys and row values */ export declare function convertRowToObject(row: string[], headers: HeaderArray, originalHeaders: HeaderArray | null, groupColumnsByName: boolean): Record; /** * Filter out null/undefined values from a header array. * Returns only the valid string headers. * * @param headers - Header array that may contain null/undefined values * @returns Array of valid string headers (null/undefined removed) */ export declare function filterValidHeaders(headers: HeaderArray): string[]; export {};