Metreeca Gear
    Preparing search index...

    Function csv

    • Creates a CSV parser.

      The generated task converts a feed of CSV documents into a feed of records, one Record per data row, so that a consumer works on structured data rather than on text.

      A document is given either as text or as a response carrying it as its body, and is read on its own, header row included. A response carrying no body contributes no record.

      Response bodies are decoded as the charset parameter of the content type states, and as UTF-8 where it states none, whatever the US-ASCII default carried by the text media types, as UTF-8 is what CSV is exchanged under in practice. A byte order mark opening a document is stripped, both from text and from a body decoded under a Unicode charset.

      A response stating a content type other than text/csv or application/csv, or a charset the platform doesn't decode, is reported to the log and read all the same, the body decoded as UTF-8 where the charset is not known, so that a mis-declared source is diagnosed without being shut out: CSV is served under text/plain and vendor types as readily as under text/csv.

      Note

      • Incremental: each record is emitted as soon as it is parsed, so the feed produced runs dry as the feed drawn from does and an endless source is read as long as it is consumed.
      • Streaming: documents are drawn one at a time and a response body is pulled as records are asked for, so resources of any size are handled without holding them in memory and a consumer that stops early releases the source; a document given as text is nonetheless parsed in one go, and some of a body is read ahead of the records actually consumed.
      • Stateless: every document is parsed on its own, reading a header row of its own, so the outcome is unaffected by how the feed is split across nested feeds or runs.
      Warning

      Records that cannot be parsed are skipped and reported to the log, leaving the feed to run to completion.

      Type Parameters

      • R extends Record = Record

        The type of the records produced; field values are emitted as parsed, without being validated against it

      Parameters

      • options: {
            header?: boolean;
            skip?: boolean;
            trim?: boolean;
            flex?: boolean;
            quote?: string;
            delimiter?: string;
        } = {}

        The parsing options

        • Optional Readonlyheader?: boolean

          Reads the first row as column labels, keying records by label rather than by positional index; defaults to false

        • Optional Readonlyskip?: boolean

          Ignores empty lines rather than emitting them as records; defaults to false

        • Optional Readonlytrim?: boolean

          Strips surrounding whitespace from field values; defaults to false

        • Optional Readonlyflex?: boolean

          Emits records whose field count doesn't match the header, leaving out missing fields and discarding fields beyond the header, rather than skipping them; defaults to false

        • Optional Readonlyquote?: string

          The character wrapping field values; defaults to " if unset or empty

        • Optional Readonlydelimiter?: string

          The character separating fields; defaults to , if unset or empty

      Returns Task<string | Response, R>

      A task converting a feed of CSV documents, given as text or as responses, into a feed of records

      Error While the feed is consumed, whatever the source reports while producing documents, or whatever reading the body of a response reports