/** * CSV Format Configuration * * Pre-compiled configuration for CSV formatting performance. * Creates regex patterns, quote lookup functions, and format settings. */ import type { CsvFormatOptions, TypeTransformMap } from "../types.js"; import type { DecimalSeparator } from "../utils/number.js"; /** * Configuration for quoting specific columns */ export type QuoteColumnConfig = boolean | boolean[] | Record; /** * Pre-compiled regex patterns for CSV formatting performance */ export interface CsvFormatRegex { /** Regex to check if a field needs quoting (contains delimiter, quote, or newline) */ needsQuoteRegex: RegExp | null; /** Regex to find quote/escape characters for escaping */ escapeQuoteRegex: RegExp | null; /** The escaped quote sequence (escape + quote) */ escapedQuote: string; /** Whether quoting is enabled */ quoteEnabled: boolean; /** The quote character */ quote: string; /** The escape character */ escape: string; /** The delimiter character (for fast path) */ delimiter: string; /** Whether we can use fast string.includes() check */ useFastCheck: boolean; } /** * Options for creating format regex patterns */ export interface FormatRegexOptions { /** The quote character (false/null to disable quoting) */ quote: string | false | null; /** The delimiter character */ delimiter: string; /** The escape character (defaults to quote if not provided) */ escape?: string | false | null; } /** * Context for formatting a single field */ export interface FormatFieldContext { /** Column index */ index: number; /** Header name for this column (if known) */ header?: string; /** Whether this is a header row */ isHeader: boolean; /** Current output row index (for transform context) */ outputRowIndex: number; /** Force quote this field */ forceQuote: boolean; /** Quote all fields (when quoteColumns: true) */ quoteAll: boolean; /** Escape formulae (CSV injection protection) */ escapeFormulae: boolean; /** Decimal separator for number formatting */ decimalSeparator: DecimalSeparator; /** Type transform map */ transform?: TypeTransformMap; } /** * Options for formatting a row */ export interface FormatRowOptions { /** Pre-computed quote lookup function */ quoteLookup: QuoteLookupFn; /** Field delimiter */ delimiter: string; /** Header names for columns (used for transform context) */ headers?: string[]; /** Whether this row is a header row */ isHeader: boolean; /** Current output row index (0-based) */ outputRowIndex: number; /** Quote all fields (when quoteColumns: true) */ quoteAll: boolean; /** Escape formulae (CSV injection protection) */ escapeFormulae: boolean; /** Decimal separator for number formatting */ decimalSeparator: DecimalSeparator; /** Type transform map */ transform?: TypeTransformMap; } /** * Complete format configuration (shared by batch formatter and CsvFormatterStream) */ export interface FormatConfig { delimiter: string; lineEnding: string; quoteAll: boolean; escapeFormulae: boolean; decimalSeparator: DecimalSeparator; writeHeaders: boolean; bom: boolean; trailingNewline: boolean; typeTransform?: TypeTransformMap; regex: CsvFormatRegex; shouldQuoteColumn: QuoteLookupFn; shouldQuoteHeader: QuoteLookupFn; } /** * Create pre-compiled regex patterns for CSV formatting */ export declare function createFormatRegex(options: FormatRegexOptions): CsvFormatRegex; /** * Pre-compute a quote lookup function for better performance. * Avoids repeated type checks on quoteConfig during formatting. */ export type QuoteLookupFn = (index: number, header: string | undefined) => boolean; export declare function createQuoteLookup(quoteConfig: QuoteColumnConfig | undefined): QuoteLookupFn; /** * Create complete format configuration from options */ export declare function createFormatConfig(options: CsvFormatOptions): FormatConfig;