Interface DSV

A DSV parser and formatter

interface DSV {
    format<T>(rows, columns?): string;
    formatBody<T>(rows, columns?): string;
    formatRow(row): string;
    formatRows(rows): string;
    formatValue(value): string;
    parse<Columns>(dsvString): DSVRowArray<Columns>;
    parse<ParsedRow, Columns>(dsvString, row): DSVParsedArray<ParsedRow>;
    parseRows(dsvString): string[][];
    parseRows<ParsedRow>(dsvString, row): ParsedRow[];
}

Methods

  • Formats the specified array of object rows as delimiter-separated values, returning a string. This operation is the inverse of dsv.parse. Each row will be separated by a newline (\n), and each column within each row will be separated by the delimiter (such as a comma, ,). Values that contain either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.

    If columns is not specified, the list of column names that forms the header row is determined by the union of all properties on all objects in rows; the order of columns is nondeterministic.

    Type Parameters

    • T extends object

    Parameters

    • rows: readonly T[]

      Array of object rows.

    • Optional columns: readonly (keyof T)[]

      An array of strings representing the column names.

    Returns string

  • Equivalent to dsv.format, but omits the header row. This is useful, for example, when appending rows to an existing file.

    Type Parameters

    • T extends object

    Parameters

    • rows: readonly T[]

      Array of object rows.

    • Optional columns: readonly (keyof T)[]

      An array of strings representing the column names.

    Returns string

  • Formats a single array row of strings as delimiter-separated values, returning a string. Each column within the row will be separated by the delimiter (such as a comma, ,). Values that contain either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.

    Parameters

    • row: readonly string[]

      An array of strings representing a row.

    Returns string

  • Formats the specified array of array of string rows as delimiter-separated values, returning a string. This operation is the reverse of dsv.parseRows. Each row will be separated by a newline (\n), and each column within each row will be separated by the delimiter (such as a comma, ,). Values that contain either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.

    To convert an array of objects to an array of arrays while explicitly specifying the columns, use array.map. If you like, you can also array.concat this result with an array of column names to generate the first row.

    Parameters

    • rows: readonly string[][]

      An array of array of string rows.

    Returns string

  • Format a single value or string as a delimiter-separated value, returning a string. A value that contains either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.

    Parameters

    • value: string

      A value.

    Returns string

  • Parses the specified string, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of objects representing the parsed rows.

    Unlike dsv.parseRows, this method requires that the first line of the DSV content contains a delimiter-separated list of column names; these column names become the attributes on the returned objects.

    The returned array also exposes a columns property containing the column names in input order (in contrast to Object.keys, whose iteration order is arbitrary).

    If the column names are not unique, only the last value is returned for each name; to access all values, use dsv.parseRows instead.

    Note: requires unsafe-eval content security policy.

    Type Parameters

    • Columns extends string

    Parameters

    • dsvString: string

      A string, which must be in the delimiter-separated values format with the appropriate delimiter.

    Returns DSVRowArray<Columns>

  • Parses the specified string, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of objects representing the parsed rows.

    Unlike dsv.parseRows, this method requires that the first line of the DSV content contains a delimiter-separated list of column names; these column names become the attributes on the returned objects.

    The returned array also exposes a columns property containing the column names in input order (in contrast to Object.keys, whose iteration order is arbitrary).

    If the column names are not unique, only the last value is returned for each name; to access all values, use dsv.parseRows instead.

    Note: requires unsafe-eval content security policy.

    Type Parameters

    • ParsedRow extends object

    • Columns extends string

    Parameters

    • dsvString: string

      A string, which must be in the delimiter-separated values format with the appropriate delimiter.

    • row: ((rawRow, index, columns) => ParsedRow)

      A row conversion function which is invoked for each row, being passed an object representing the current row (d), the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined, the row is skipped and will be omitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object. In effect, row is similar to applying a map and filter operator to the returned rows.

        • (rawRow, index, columns): ParsedRow
        • Parameters

          • rawRow: DSVRowString<Columns>
          • index: number
          • columns: Columns[]

          Returns ParsedRow

    Returns DSVParsedArray<ParsedRow>

  • Parses the specified string, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of arrays representing the parsed rows.

    Unlike dsv.parse, this method treats the header line as a standard row, and should be used whenever DSV content does not contain a header. Each row is represented as an array rather than an object. Rows may have variable length.

    If a row conversion function is not specified, field values are strings. For safety, there is no automatic conversion to numbers, dates, or other types. In some cases, JavaScript may coerce strings to numbers for you automatically (for example, using the + operator), but better is to specify a row conversion function.

    Parameters

    • dsvString: string

      A string, which must be in the delimiter-separated values format with the appropriate delimiter.

    Returns string[][]

  • Parses the specified string, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of arrays representing the parsed rows.

    Unlike dsv.parse, this method treats the header line as a standard row, and should be used whenever DSV content does not contain a header. Each row is represented as an array rather than an object. Rows may have variable length.

    Type Parameters

    • ParsedRow extends object

    Parameters

    • dsvString: string

      A string, which must be in the delimiter-separated values format with the appropriate delimiter.

    • row: ((rawRow, index) => ParsedRow)

      A row conversion function which is invoked for each row, being passed an array representing the current row (d), the index (i) starting at zero for the first row, and the array of column names. If the returned value is null or undefined, the row is skipped and will be omitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object. In effect, row is similar to applying a map and filter operator to the returned rows.

        • (rawRow, index): ParsedRow
        • Parameters

          • rawRow: string[]
          • index: number

          Returns ParsedRow

    Returns ParsedRow[]