79ddec7d4df3c01491d633546b19aa2670778c55
@techniker-me/csv
Usage
Parse from string
import { CsvParser } from "@techniker-me/csv";
const parser = new CsvParser();
const csv = "name,age\nAlice,30\nBob,40";
const result = await parser.parseFromString(csv, {
hasHeader: true,
output: "object", // "object" or "array"
});
// result.headers -> ["name", "age"]
// result.rows -> [{ name: "Alice", age: "30" }, { name: "Bob", age: "40" }]
Parse from file
import { CsvParser, FileChunkSource } from "@techniker-me/csv";
const parser = new CsvParser();
const source = new FileChunkSource("/path/to/file.csv");
const result = await parser.parseFromSource(source, {
hasHeader: true,
output: "array",
});
Dialect options
import { CsvParser, CsvDialect } from "@techniker-me/csv";
const parser = new CsvParser();
const result = await parser.parseFromString("a;b\n1;2", {
hasHeader: true,
dialect: { delimiter: ";", quote: '"', trimWhitespace: true },
});
API
CsvParser.parseFromString(input: string, options?: ParseOptions)CsvParser.parseFromSource(source: IChunkSource, options?: ParseOptions)FileChunkSource(path: string, chunkSize?: number)StringChunkSource(content: string, chunkSize?: number)
ParseOptions:
dialect(partial):{ delimiter, quote, trimWhitespace }hasHeader: treat first row as headers (default true for object mode, optional for array mode)output:"object" | "array"(default"array")validateRowLength: pads/truncates rows to uniform length (default true)skipEmptyLines: ignore empty lines (default true)
Description
Languages
TypeScript
100%