import * as Inputs from "../inputs"; /** * Reading and writing CSV, the plain-text table format with one row per line and a separator * between cells. The parsers split on `rowSeparator` and `columnSeparator`, honor double-quoted * cells with doubled quotes inside, skip blank lines and read `\n`, `\t` and `\r` written as two * characters as the real thing; the writers quote a cell that contains a separator, a quote or a * line break. */ export declare class CSVBitByBit { /** * Splits CSV text into a list of rows, each a list of cell strings; nothing is converted to * numbers. * * Blank lines are skipped, cells are trimmed with their line, and a double-quoted cell may * contain the separator and doubled quotes. Example: `a,b,c` and `1,2,3` on two lines -> * `[["a", "b", "c"], ["1", "2", "3"]]`. * @param inputs - The CSV text and the two separators * @returns The rows as lists of cell strings * @group parse * @shortname parse to array * @drawable false * @example * ```typescript * const rows = bitbybit.csv.parseToArray({ csv: "x,y,z\n1,2,3\n4,5,6", rowSeparator: "\n", columnSeparator: "," }); * ``` */ parseToArray(inputs: Inputs.CSV.ParseToArrayDto): string[][]; /** * Turns CSV text into a list of objects, one per data row, keyed by the header names of row * `headerRow`. * * Rows start at `dataStartRow`, columns named in `numberColumns` become numbers and a missing * cell becomes an empty string. Example: `name,age` then `John,30` -> `[{ name: "John", age: * "30" }]`. * @param inputs - The CSV text, the header and data row indexes, the separators and the number columns * @returns One object per data row * @group parse * @shortname parse to json * @drawable false * @example * ```typescript * const people = bitbybit.csv.parseToJson({ csv: "name,age\nJohn,30\nJane,25", headerRow: 0, dataStartRow: 1, rowSeparator: "\n", columnSeparator: ",", numberColumns: ["age"] }); * ``` */ parseToJson>(inputs: Inputs.CSV.ParseToJsonDto): T[]; /** * Turns CSV text into a list of objects keyed by the `headers` you give, for files without a * header line; a header line the file does have is skipped by setting `dataStartRow` past it. * * Columns named in `numberColumns` become numbers. Example: `John,30` with headers `["name", * "age"]` -> `[{ name: "John", age: "30" }]`. * @param inputs - The CSV text, the header names, the data start row, the separators and the number columns * @returns One object per data row * @group parse * @shortname parse to json with headers * @drawable false * @example * ```typescript * const people = bitbybit.csv.parseToJsonWithHeaders({ csv: "John,30\nJane,25", headers: ["name", "age"], dataStartRow: 0, rowSeparator: "\n", columnSeparator: ",", numberColumns: ["age"] }); * ``` */ parseToJsonWithHeaders>(inputs: Inputs.CSV.ParseToJsonWithHeadersDto): T[]; /** * Lists every value of one column, found by its header name, in row order; a row without that * cell gives an empty string. * * With `asNumber` true the values are parsed as numbers. Example: `name,age` then `John,30` and * `Jane,25`, column `name` -> `["John", "Jane"]`. * @param inputs - The CSV text, the column name, the header and data row indexes, the separators and the number flag * @returns The column's values, top to bottom * @group query * @shortname query column * @drawable false * @example * ```typescript * const ages = bitbybit.csv.queryColumn({ csv: "name,age\nJohn,30\nJane,25", column: "age", headerRow: 0, dataStartRow: 1, rowSeparator: "\n", columnSeparator: ",", asNumber: true }); * ``` */ queryColumn(inputs: Inputs.CSV.QueryColumnDto): (string | number)[]; /** * Keeps only the rows whose cell in `column` equals `value`, giving them as objects keyed by * the headers. * * The comparison is on text unless the column is listed in `numberColumns`, in which case both * sides are compared as numbers. Example: column `age`, value `30` -> `[{ name: "John", age: * "30" }]`. * @param inputs - The CSV text, the column name, the value, the row indexes, the separators and the number columns * @returns The matching rows as objects * @group query * @shortname query rows by value * @drawable false * @example * ```typescript * const thirty = bitbybit.csv.queryRowsByValue({ csv: "name,age\nJohn,30\nJane,25", column: "age", value: "30", headerRow: 0, dataStartRow: 1, rowSeparator: "\n", columnSeparator: "," }); * ``` */ queryRowsByValue>(inputs: Inputs.CSV.QueryRowsByValueDto): T[]; /** * Writes a list of rows, each a list of cells, as CSV text; a cell holding a separator, a quote * or a line break is wrapped in double quotes. * * Example: `[["name", "age"], ["John", "30"]]` -> `name,age` and `John,30` on two lines. * @param inputs - The rows and the two separators * @returns The CSV text * @group generate * @shortname array to csv * @drawable false * @example * ```typescript * const csv = bitbybit.csv.arrayToCsv({ array: [["x", "y", "z"], [1, 2, 3]], rowSeparator: "\n", columnSeparator: "," }); * ``` */ arrayToCsv(inputs: Inputs.CSV.ArrayToCsvDto): string; /** * Writes a list of objects as CSV text with the columns you name in `headers`, in that order; a * property an object lacks becomes an empty cell. * * With `includeHeaders` true the first line holds the header names. Example: `[{ name: "John", * age: "30" }]` with headers `["name", "age"]` -> `name,age` and `John,30`. * @param inputs - The objects, the column names, the header flag and the separators * @returns The CSV text * @group generate * @shortname json to csv * @drawable false * @example * ```typescript * const csv = bitbybit.csv.jsonToCsv({ json: people, headers: ["name", "age"], includeHeaders: true, rowSeparator: "\n", columnSeparator: "," }); * ``` */ jsonToCsv>(inputs: Inputs.CSV.JsonToCsvDto): string; /** * Writes a list of objects as CSV text using the property names of the first object as the * columns, in their order; an empty list gives empty text. * * Example: `[{ name: "John", age: "30" }]` -> `name,age` and `John,30`. * @param inputs - The objects, the header flag and the separators * @returns The CSV text * @group generate * @shortname json to csv auto * @drawable false * @example * ```typescript * const csv = bitbybit.csv.jsonToCsvAuto({ json: people, includeHeaders: true, rowSeparator: "\n", columnSeparator: "," }); * ``` */ jsonToCsvAuto>(inputs: Inputs.CSV.JsonToCsvAutoDto): string; /** * Reads the cells of row `headerRow` as the header names; a row index past the end throws an * error. * * Example: `name,age` then `John,30` -> `["name", "age"]`. * @param inputs - The CSV text, the header row index and the separators * @returns The header names in column order * @group query * @shortname get headers * @drawable false * @example * ```typescript * const headers = bitbybit.csv.getHeaders({ csv: "name,age\nJohn,30", headerRow: 0, rowSeparator: "\n", columnSeparator: "," }); * ``` */ getHeaders(inputs: Inputs.CSV.GetHeadersDto): string[]; /** * Counts the data rows: all non-blank lines minus the ones before `dataStartRow`, or minus one * header line when `hasHeaders` is true and `dataStartRow` is left out. * * Example: `name,age`, `John,30`, `Jane,25` with headers -> 2. * @param inputs - The CSV text, the header flag, the optional data start row and the separators * @returns The number of data rows * @group query * @shortname row count * @drawable false * @example * ```typescript * const count = bitbybit.csv.getRowCount({ csv: "name,age\nJohn,30\nJane,25", hasHeaders: true, rowSeparator: "\n", columnSeparator: "," }); * ``` */ getRowCount(inputs: Inputs.CSV.GetRowCountDto): number; /** * Counts the cells of the first non-blank row, which is the number of columns; empty text gives * 0. * * Example: `name,age,city` then `John,30,NYC` -> 3. * @param inputs - The CSV text and the two separators * @returns The number of columns * @group query * @shortname column count * @drawable false * @example * ```typescript * const columns = bitbybit.csv.getColumnCount({ csv: "name,age,city\nJohn,30,NYC", rowSeparator: "\n", columnSeparator: "," }); * ``` */ getColumnCount(inputs: Inputs.CSV.ParseToArrayDto): number; private parseCsvLine; private escapeCsvCell; /** * Converts literal escape sequence strings to their actual characters. * For example, converts "\\n" (two characters) to "\n" (newline character). */ private convertEscapeSequences; }