Provides a basic API to stream and serialize a CSV file allowing custom programs performing simple filtering / modifications. The CSV files must have a header row describing the column keys.

// This example processes a Manabox CSV file altering the original `Purchase price` column data.
import { CSVFile } from '@typhonjs-tcg/scrydex/data/import/csv';

const rows = [];

for await (const row of CSVFile.asStream({ filepath: './manabox-file.csv' }))
{
const price = Number(row['Purchase price']);

// If `Purchase price` is a finite number reduce it by a factor of 65% or passthrough original value.
row['Purchase price'] = Number.isFinite(price) ? (price * .65).toFixed(2) : row['Purchase price'];
rows.push(row);
}

await CSVFile.save({ filepath: './manabox-processed.csv', rows });
Index

Methods

  • Stream row by row the given CSV file.

    Parameters

    • options: { filepath: string; signal?: AbortSignal }

      Options.

      • filepath: string

        A valid CSV file path.

      • Optionalsignal?: AbortSignal

        AbortSignal via AbortController to stop iteration and cleanup streaming.

    Returns AsyncGenerator<{ [key: string]: string }>

    Asynchronous iterator over each row as an object keyed by column header name.

  • Get CSV column headers defined on the first line of a CSV file.

    Parameters

    • options: { filepath: string }

      Options.

      • filepath: string

        A valid CSV file path.

    Returns Promise<string[]>

    An array of parsed column headers.

  • Return all rows of the given CSV file path.

    Parameters

    • options: { filepath: string }

      Options.

      • filepath: string

        Output CSV file path.

    Returns Promise<{ [key: string]: string }[]>

    An array of rows as an object keyed by column header name.

  • Save CSV row data to the given file path.

    Parameters

    • options: { filepath: string; rows: { [key: string]: string }[] }

      Options.

      • filepath: string

        Output CSV file path.

      • rows: { [key: string]: string }[]

        CSV row data to write.

    Returns Promise<void>