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<Defined in: src/lib/utils/csvParser.ts:141 Creates a new CSVFile instance.T>(filePath,columnStructure?,options?):CSVFile<T>
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 headersExamples
Methods
filter()
filter(Defined in: src/lib/utils/csvParser.ts:417 Filters rows of the CSV file based on a predicate function.predicate):Promise<TextendsColumnStructure? { [K in string | number | symbol]: string } :string[][]>
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():Defined in: src/lib/utils/csvParser.ts:270 Gets the number of columns in the CSV file.Promise<number>
Returns
Promise<number>
The number of columns
Async
Throws
When unable to read the header rowExample
getHeader()
getHeader():Defined in: src/lib/utils/csvParser.ts:291 Gets the header row of the CSV file.Promise<null|string[]>
Returns
Promise<null | string[]>
Array of header field names, or null if no header
Async
Throws
When unable to read the header rowExample
getRow()
getRow(Defined in: src/lib/utils/csvParser.ts:336 Gets a specific row from the CSV file.index):Promise<null|TextendsColumnStructure? { [K in string | number | symbol]: string } :string[]>
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():Defined in: src/lib/utils/csvParser.ts:249 Gets the total number of rows in the CSV file.Promise<number>
Returns
Promise<number>
The total number of rows (excluding header if present)
Async
Example
map()
map<Defined in: src/lib/utils/csvParser.ts:437 Maps each row of the CSV file using a mapper function.U>(mapper):Promise<U[]>
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()
Defined in: src/lib/utils/csvParser.ts:502 Restructures a CSVFile object with a new column structure.staticrestructure<U>(csvFile,newColumnStructure):Promise<CSVFile<U>>
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()
Defined in: src/lib/utils/csvParser.ts:542 Writes data to a CSV file.staticwriteToFile<T>(data,outputPath,columnStructure?,options?):Promise<void>
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.