Bitbybit Docs
    Preparing search index...

    Class CSVBitByBit

    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.

    Index

    Constructors

    generate

    • 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.

      Parameters

      Returns string

      The CSV text

      const csv = bitbybit.csv.arrayToCsv({ array: [["x", "y", "z"], [1, 2, 3]], rowSeparator: "\n", columnSeparator: "," });
      
    • 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.

      Type Parameters

      • T = Record<string, unknown>

      Parameters

      • inputs: JsonToCsvDto<T>

        The objects, the column names, the header flag and the separators

      Returns string

      The CSV text

      const csv = bitbybit.csv.jsonToCsv({ json: people, headers: ["name", "age"], includeHeaders: true, rowSeparator: "\n", columnSeparator: "," });
      
    • 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.

      Type Parameters

      • T = Record<string, unknown>

      Parameters

      Returns string

      The CSV text

      const csv = bitbybit.csv.jsonToCsvAuto({ json: people, includeHeaders: true, rowSeparator: "\n", columnSeparator: "," });
      

    parse

    • 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"]].

      Parameters

      Returns string[][]

      The rows as lists of cell strings

      const rows = bitbybit.csv.parseToArray({ csv: "x,y,z\n1,2,3\n4,5,6", rowSeparator: "\n", columnSeparator: "," });
      
    • 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" }].

      Type Parameters

      • T = Record<string, string | number>

      Parameters

      • inputs: ParseToJsonDto

        The CSV text, the header and data row indexes, the separators and the number columns

      Returns T[]

      One object per data row

      const people = bitbybit.csv.parseToJson({ csv: "name,age\nJohn,30\nJane,25", headerRow: 0, dataStartRow: 1, rowSeparator: "\n", columnSeparator: ",", numberColumns: ["age"] });
      
    • 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" }].

      Type Parameters

      • T = Record<string, string | number>

      Parameters

      Returns T[]

      One object per data row

      const people = bitbybit.csv.parseToJsonWithHeaders({ csv: "John,30\nJane,25", headers: ["name", "age"], dataStartRow: 0, rowSeparator: "\n", columnSeparator: ",", numberColumns: ["age"] });
      

    query

    • 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"].

      Parameters

      • inputs: QueryColumnDto

        The CSV text, the column name, the header and data row indexes, the separators and the number flag

      Returns (string | number)[]

      The column's values, top to bottom

      const ages = bitbybit.csv.queryColumn({ csv: "name,age\nJohn,30\nJane,25", column: "age", headerRow: 0, dataStartRow: 1, rowSeparator: "\n", columnSeparator: ",", asNumber: true });
      
    • 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" }].

      Type Parameters

      • T = Record<string, string | number>

      Parameters

      • inputs: QueryRowsByValueDto

        The CSV text, the column name, the value, the row indexes, the separators and the number columns

      Returns T[]

      The matching rows as objects

      const thirty = bitbybit.csv.queryRowsByValue({ csv: "name,age\nJohn,30\nJane,25", column: "age", value: "30", headerRow: 0, dataStartRow: 1, rowSeparator: "\n", columnSeparator: "," });
      
    • 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"].

      Parameters

      • inputs: GetHeadersDto

        The CSV text, the header row index and the separators

      Returns string[]

      The header names in column order

      const headers = bitbybit.csv.getHeaders({ csv: "name,age\nJohn,30", headerRow: 0, rowSeparator: "\n", columnSeparator: "," });
      
    • 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.

      Parameters

      • inputs: GetRowCountDto

        The CSV text, the header flag, the optional data start row and the separators

      Returns number

      The number of data rows

      const count = bitbybit.csv.getRowCount({ csv: "name,age\nJohn,30\nJane,25", hasHeaders: true, rowSeparator: "\n", columnSeparator: "," });
      
    • 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.

      Parameters

      Returns number

      The number of columns

      const columns = bitbybit.csv.getColumnCount({ csv: "name,age,city\nJohn,30,NYC", rowSeparator: "\n", columnSeparator: "," });