Skip to main content

Class: CSVFile<T>

Defined in: src/lib/utils/csvParser.ts:105 Represents a CSV file with optional type information for its columns. Provides methods for reading, parsing, and manipulating CSV data with optional type safety through column structure definitions. Supports both streaming and batch operations for efficient processing of large files.

Examples

Type Parameters

T

T extends ColumnStructure | undefined = undefined Optional column structure type for typed access CSVFile

Constructors

Constructor

new CSVFile<T>(filePath, columnStructure?, options?): CSVFile<T>
Defined in: src/lib/utils/csvParser.ts:141 Creates a new CSVFile instance.

Parameters

filePath
string The path to the CSV file
columnStructure?
T Optional column structure mapping column names to indices
options?
CSVParseOptions = {} Optional parsing configuration

Returns

CSVFile<T>

Throws

When column structure is provided but doesn’t match the file headers

Examples

Methods

filter()

filter(predicate): Promise<T extends ColumnStructure ? { [K in string | number | symbol]: string } : string[][]>
Defined in: src/lib/utils/csvParser.ts:417 Filters rows of the CSV file based on a predicate function.

Parameters

predicate
(row) => boolean A function that takes a row and returns true if the row should be included in the result.

Returns

Promise<T extends ColumnStructure ? { [K in string | number | symbol]: string } : string[][]> A promise that resolves to an array of filtered rows.

getColumnCount()

getColumnCount(): Promise<number>
Defined in: src/lib/utils/csvParser.ts:270 Gets the number of columns in the CSV file.

Returns

Promise<number> The number of columns

Async

Throws

When unable to read the header row

Example


getHeader()

getHeader(): Promise<null | string[]>
Defined in: src/lib/utils/csvParser.ts:291 Gets the header row of the CSV file.

Returns

Promise<null | string[]> Array of header field names, or null if no header

Async

Throws

When unable to read the header row

Example


getRow()

getRow(index): Promise<null | T extends ColumnStructure ? { [K in string | number | symbol]: string } : string[]>
Defined in: src/lib/utils/csvParser.ts:336 Gets a specific row from the CSV file.

Parameters

index
number The zero-based index of the row to retrieve.

Returns

Promise<null | T extends ColumnStructure ? { [K in string | number | symbol]: string } : string[]> A promise that resolves to the row data, either as an object (if column structure is provided) or as an array of strings.

Throws

if the row index is out of bounds.

Example


getRowCount()

getRowCount(): Promise<number>
Defined in: src/lib/utils/csvParser.ts:249 Gets the total number of rows in the CSV file.

Returns

Promise<number> The total number of rows (excluding header if present)

Async

Example


map()

map<U>(mapper): Promise<U[]>
Defined in: src/lib/utils/csvParser.ts:437 Maps each row of the CSV file using a mapper function.

Type Parameters

U
U

Parameters

mapper
(row) => U A function that takes a row and returns a transformed value.

Returns

Promise<U[]> A promise that resolves to an array of mapped values.

restructure()

static restructure<U>(csvFile, newColumnStructure): Promise<CSVFile<U>>
Defined in: src/lib/utils/csvParser.ts:502 Restructures a CSVFile object with a new column structure.

Type Parameters

U
U extends ColumnStructure

Parameters

csvFile
CSVFile<undefined | ColumnStructure> The original CSVFile object.
newColumnStructure
U The new column structure to apply.

Returns

Promise<CSVFile<U>> A new CSVFile object with the updated column structure.

Throws

if the new column structure doesn’t match the CSV file headers.

writeToFile()

static writeToFile<T>(data, outputPath, columnStructure?, options?): Promise<void>
Defined in: src/lib/utils/csvParser.ts:542 Writes data to a CSV file.

Type Parameters

T
T extends undefined | ColumnStructure = undefined

Parameters

data
T extends ColumnStructure ? { [K in string | number | symbol]: string } : string[][] The data to write, either as an array of objects or an array of arrays.
outputPath
string The path where the CSV file should be written.
columnStructure?
T Optional column structure for typed data.
options?
CSVWriteOptions = {} Optional writing options.

Returns

Promise<void> A promise that resolves when the file has been written.

Example