csv-stream-lite - v1.0.4
    Preparing search index...

    Class Csv<T, I>

    Main CSV parser class for parsing complete CSV documents. Supports reading headers, streaming rows, and converting rows to objects.

    The output type after optional transformation (defaults to T)

    // Parse CSV with headers
    const csv = new Csv(fileStream)
    for await (const row of csv.streamObjectsAsync()) {
    console.log(row)
    }

    Type Parameters

    • T extends object

      The object type for each row when reading as objects

    • I = unknown

    Hierarchy (View Summary)

    Index

    Constructors

    • Creates a new CSV parser.

      Type Parameters

      • T extends object

        The object type for each row when reading as objects

      • I = unknown

      Parameters

      • OptionalasyncIterable: ByteBuffer | ByteStream<T>

        Optional byte stream or buffer containing CSV data

      • Optionaloptions: CsvEntityOptions & {
            headers?: string[];
            ignoreUtf8Bom?: boolean;
            includeExtraCells?: boolean;
            readHeaders?: boolean;
            shape?: CsvObjectShape<T>;
            strictColumns?: boolean;
            transform?: (row: I) => T;
        }

        Configuration options for CSV parsing

      Returns Csv<T, I>

      If both headers and shape options are specified

    Properties

    byteBuffer: ByteBuffer
    consumed: boolean = false
    escapeChar: string = '"'
    headers?: string[]
    ignoreUtf8Bom: boolean = true
    includeExtraCells: boolean = false
    newline?: string
    quoteChar: string = '"'
    readHeaders: boolean = true
    separator: string = ','
    shape?: CsvObjectShape<T>
    strictColumns: boolean = false
    transform?: (row: I) => T
    trim: boolean = false

    Accessors

    Methods

    • Asynchronously consumes the entity if it hasn't been consumed yet. This ensures the buffer advances to the end of this entity.

      Returns Promise<void>

      A promise that resolves when consumption is complete

    • Synchronously streams CSV rows as objects. Reads headers from the first row if readHeaders is true.

      Returns Generator<T>

      A generator that yields parsed objects of type O

      const csv = new Csv(csvString)
      for (const row of csv.streamObjects()) {
      console.log(row)
      }
    • Asynchronously streams CSV rows as objects. Automatically handles buffer refills as needed.

      Returns AsyncGenerator<T>

      An async generator that yields parsed objects of type O

      const csv = new Csv(fileStream)
      for await (const row of csv.streamObjectsAsync()) {
      console.log(row)
      }
    • Static method to create a CsvStringify instance for advanced usage.

      Type Parameters

      • T extends object

        The input object type

      • O extends object = T

        The output object type after optional transformation (defaults to T)

      Parameters

      • values: T[]

        Array of objects to convert to CSV

      • Optionaloptions: CsvStringifyOptions<T, T extends O ? T<T> : O>

        Optional configuration for CSV stringification

      Returns CsvStringify<T, T extends O ? T<T> : O>

      A CsvStringify instance

    • Static method to stringify an array of objects into CSV format. Returns a synchronous generator that yields CSV string chunks.

      Type Parameters

      • T extends object

        The input object type

      • O extends object = T

        The output object type after optional transformation (defaults to T)

      Parameters

      • values: T[]

        Array of objects to convert to CSV

      • Optionaloptions: CsvStringifyOptions<T, T extends O ? T<T> : O>

        Optional configuration for CSV stringification

      Returns Generator<CsvString<T extends O ? T<T> : O>>

      A generator that yields CSV string chunks

      const data = [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }]
      for (const chunk of Csv.stringify(data, { headers: ['name', 'age'] })) {
      process.stdout.write(chunk)
      }
    • Static method to stringify an array of objects into CSV format asynchronously. Returns an asynchronous generator that yields CSV string chunks.

      Type Parameters

      • T extends object

        The input object type

      • O extends object = T

        The output object type after optional transformation (defaults to T)

      Parameters

      • values: T[]

        Array of objects to convert to CSV

      • Optionaloptions: CsvStringifyOptions<T, T extends O ? T<T> : O>

        Optional configuration for CSV stringification

      Returns AsyncGenerator<CsvString<T extends O ? T<T> : O>>

      An async generator that yields CSV string chunks

      const data = [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }]
      for await (const chunk of Csv.stringifyAsync(data)) {
      process.stdout.write(chunk)
      }