csvFieldfunctionfunction csvField(value: string, separator?: string): stringA field as CSV writes it: quoted, with its quotes doubled, where it holds the separator, a quote, a line break or leading or trailing space.
CSV by RFC 4180, read and written with no outside package (commons 0001-formats FR-005): fields separated by a comma (or another single character), a field quoted where it holds the separator, a quote or a line break, a quote inside one doubled, records ended by CRLF or LF. The reader refuses a quote in an unquoted field and a quoted field that is not closed, and says the line and column; it can give each record as an object by the header's names. Replaces @std/csv in erga.
import { parseCsv, toCsv } from "./csv.ts";
parseCsv('name,quote\nAda,"Hello, ""world"""\n'); // { kind: "ok", value: [["name", "quote"], ["Ada", 'Hello, "world"']] }
toCsv([["a", "b,c"]]); // 'a,"b,c"\r\n'csvFieldfunctionfunction csvField(value: string, separator?: string): stringA field as CSV writes it: quoted, with its quotes doubled, where it holds the separator, a quote, a line break or leading or trailing space.
formatCsvErrorfunctionfunction formatCsvError(e: CsvError): stringA CSV error as one line: 3:14: a quote in a field that is not quoted.
parseCsvfunctionfunction parseCsv(text: string, options?: CsvOptions): Result<readonly (readonly string[])[], CsvError>The records of a CSV text, each a list of fields; a final line break ends the last record and makes no empty one.
parseCsvRecordsfunctionfunction parseCsvRecords(text: string, options?: CsvOptions): Result<readonly Readonly<Record<string, string>>[], CsvError>The records of a CSV text with a header, each an object by the header's names; a record whose length is not the header's is an error.
toCsvfunctionfunction toCsv(records: readonly (readonly string[])[], options?: CsvOptions): stringRecords as CSV text, each ended by the line ending.
CsvErrortypeno test imports ittype CsvError = { readonly kind: "csv"; readonly line: number; readonly column: number; readonly message: string };Where a text stops being CSV, and why. Lines and columns count from 1.
CsvOptionstypeno test imports ittype CsvOptions = { readonly separator?: string; readonly lineEnding?: "\r\n" | "\n" };How to read and write: the separator (a comma by default), and for writing the line ending (CRLF by default, as RFC 4180 has it).