import { DEFAULT_DELIMITER, DEFAULT_QUOTATION } from '../../core/constants'; import { CSVBinary, CSVRecord, CSVString, InferCSVRecord, ParseBinaryOptions, ParseOptions, PickCSVHeader } from '../../core/types'; /** * Parse CSV to records. * * {@link !String}, {@link !ReadableStream}, {@link !Response}, {@link !Request}, {@link !Blob}, and {@link !File} are supported. * * * @typeParam Header Header type like `['name', 'age']`. * * @param csv CSV string to parse. * @param options Parsing options for CSV string parsing. * @returns Async iterable iterator of records. * * If you want array of records, use {@link parse.toArray} function. * @category High-level API * * @remarks * {@link parseString}, {@link parseBinary}, {@link parseBinaryStream}, * {@link parseStringStream}, {@link parseResponse}, {@link parseRequest}, and {@link parseBlob} are used internally. * * If you known the type of the CSV, it performs better to use them directly. * * | If you want to parse a... | Use... | Options... | * | -------------------------------------------- | ----------------------------- | -------------------------- | * | {@link !String} | {@link parseString} | {@link ParseOptions} | * | {@link !ReadableStream}<{@link !String}> | {@link parseStringStream} | {@link ParseOptions} | * | {@link !Uint8Array} \| {@link !ArrayBuffer} | {@link parseBinary} | {@link ParseBinaryOptions} | * | {@link !ReadableStream}<{@link !Uint8Array}> | {@link parseBinaryStream} | {@link ParseBinaryOptions} | * | {@link !Response} | {@link parseResponse} | {@link ParseBinaryOptions} | * | {@link !Request} | {@link parseRequest} | {@link ParseBinaryOptions} | * | {@link !Blob} \| {@link !File} | {@link parseBlob} | {@link ParseBinaryOptions} | * * **Performance Characteristics:** * - **Memory usage**: O(1) - constant per record (streaming approach) * - **Suitable for**: Files of any size, browser and server environments * - **Recommended for**: Large files (> 10MB) or memory-constrained environments * * This function processes CSV data as an async iterable iterator, yielding one record at a time. * Memory footprint remains constant regardless of file size, making it ideal for large datasets. * * @example Parsing CSV files from strings * * ```ts * import { parse } from 'web-csv-toolbox'; * * const csv = `name,age * Alice,42 * Bob,69`; * * for await (const record of parse(csv)) { * console.log(record); * } * // Prints: * // { name: 'Alice', age: '42' } * // { name: 'Bob', age: '69' } * ``` * * @example Parsing CSV files from streams * * ```ts * import { parse } from 'web-csv-toolbox'; * * const csv = `name,age * Alice,42 * Bob,69`; * * const stream = new ReadableStream({ * start(controller) { * controller.enqueue(csv); * controller.close(); * } * }); * * for await (const record of parse(stream)) { * console.log(record); * } * // Prints: * // { name: 'Alice', age: '42' } * // { name: 'Bob', age: '69' } * ``` * * * @example Parsing CSV files with headers * * ```ts * import { parse } from 'web-csv-toolbox'; * * // This CSV has no header. * const csv = `Alice,42 * Bob,69`; * * for await (const record of parse(csv, { header: ['name', 'age'] })) { * console.log(record); * } * // Prints: * // { name: 'Alice', age: '42' } * // { name: 'Bob', age: '69' } * ``` * * @example Parsing CSV files with different delimiters characters * * ```ts * import { parse } from 'web-csv-toolbox'; * * const csv = `name\tage * Alice\t42 * Bob\t69`; * * for await (const record of parse(csv, { delimiter: '\t' })) { * console.log(record); * } * // Prints: * // { name: 'Alice', age: '42' } * // { name: 'Bob', age: '69' } * ``` */ export declare function parse(csv: CSVSource): AsyncIterableIterator, "object">>; export declare function parse>(csv: CSVString): AsyncIterableIterator>; export declare function parse = PickCSVHeader, const Options extends ParseOptions = ParseOptions>(csv: CSVSource, options: Options): AsyncIterableIterator>; export declare function parse, const Options extends ParseOptions
= ParseOptions
>(csv: CSVString, options: Options): AsyncIterableIterator>; /** * Parse CSV binary to records. * * @param csv CSV binary to parse. * @param options Parsing options for CSV binary parsing. * * @example Parsing CSV files from responses * * ```ts * import { parse } from 'web-csv-toolbox'; * * // This CSV data is not gzipped and encoded in utf-8. * const response = await fetch('https://example.com/data.csv'); * * for await (const record of parse(response)) { * // ... * } * ``` * * @example Parsing CSV files with options spcialized for binary * * ```ts * import { parse } from 'web-csv-toolbox'; * * // This CSV data is gzipped and encoded in shift-jis and has BOM. * const response = await fetch('https://example.com/data.csv.gz'); * * for await (const record of parse(response, { * charset: 'shift-jis', * ignoreBOM: true, * decompression: 'gzip', * })) { * // ... * } * ``` */ export declare function parse, const Options extends ParseBinaryOptions
= ParseBinaryOptions
>(csv: CSVBinary, options?: Options): AsyncIterableIterator>; export declare namespace parse { /** * Parse CSV string to array of records, * ideal for smaller data sets. * * @remarks * **Performance Characteristics:** * - **Memory usage**: O(n) - proportional to file size (loads entire result into memory) * - **Suitable for**: Small datasets, quick prototyping * - **Recommended max**: ~10MB (browser), ~100MB (Node.js/Deno) * * This function collects all records into an array before returning. * For large files, consider using the streaming {@link parse} function instead. * * @example Parse a CSV as array of records * * ```ts * import { parse } from 'web-csv-toolbox'; * * const csv = `name,age * Alice,42 * Bob,69`; * * const records = await parse.toArray(csv); * console.log(records); * // Prints: * // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ] * ``` */ function toArray
, Options extends ParseOptions
= ParseOptions
>(csv: CSVString, options?: Options): Promise[]>; /** * Parse CSV binary to array of records, * ideal for smaller data sets. * * @remarks * **Performance Characteristics:** * - **Memory usage**: O(n) - proportional to file size (loads entire result into memory) * - **Suitable for**: Small datasets, quick prototyping * - **Recommended max**: ~10MB (browser), ~100MB (Node.js/Deno) * * This function collects all records into an array before returning. * For large files, consider using the streaming {@link parse} function instead. * * @example Parse a CSV as array of records * * ```ts * import { parse } from 'web-csv-toolbox'; * * const response = await fetch('https://example.com/data.csv'); * * const records = await parse.toArray(response); * console.log(records); * ``` */ function toArray
, Options extends ParseBinaryOptions
= ParseBinaryOptions
>(csv: CSVBinary, options?: Options): Promise[]>; }