Class: CsvPowers
Methods
parse()
static parse<T>(data, options?): Promise<T[]>;
Parses buffer data using CsvParseOptions to create an array of type T.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
data |
Buffer |
Buffer with CSV data. |
options? |
CsvParseOptions |
Various options to control how your CSV is parsed. By default, CSV rows will be parsed into objects. |
Returns
Promise<T[]>
Throws
When data is missing.
Throws
When invalid CsvParseOptions are passed in.
Throws
When the csv file cannot be parsed with the default or provided options.
Example
In this example, data is pulled from a csv file and parsed into array of Data Rows.
// Ensure you have imported the CsvPowers
// import { CsvPowers, CsvParseOptions, DataPowers } from "@andi/powers";
type DataRow = { column1: string, column2: string };
const rawData = await DataPowers.get("example.csv", { type: DataType.File, parseOptions: "buffer" });
const exampleData = await CsvPowers.parse<DataRow>(rawData);
// Recommended: if manual parsing is not needed, you can combine load and parse
const orgData: DataRow[] = await DataPowers.get<DataRow>("example.csv", { type: DataType.File, parseOptions: "csv" });
// Alternative: with custom csv options
const orgData: DataRow[] = await DataPowers.get<DataRow>("example.csv", { type: DataType.File, parseOptions: { cast: true, skip_lines_with_error: true} as CsvParseOptions });
parseCsvOptionsOrDefault()
static parseCsvOptionsOrDefault():
| CsvParseOptions
| "csv"
| "buffer"
| "text"
| "json";
Returns
| CsvParseOptions
| "csv"
| "buffer"
| "text"
| "json"