@pujansrt/data-genie
Version:
High performant ETL engine written in TypeScript
32 lines (31 loc) • 933 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CSVReader = void 0;
const fs_1 = require("fs");
const csv_parse_1 = require("csv-parse");
class CSVReader {
constructor(filePath) {
this.hasFieldNamesInFirstRow = false;
this.fieldSeparator = ',';
this.filePath = filePath;
}
setFieldNamesInFirstRow(value) {
this.hasFieldNamesInFirstRow = value;
return this;
}
setFieldSeparator(separator) {
this.fieldSeparator = separator;
return this;
}
async *read() {
const parser = (0, fs_1.createReadStream)(this.filePath).pipe((0, csv_parse_1.parse)({
columns: this.hasFieldNamesInFirstRow,
delimiter: this.fieldSeparator,
skip_empty_lines: true
}));
for await (const record of parser) {
yield record;
}
}
}
exports.CSVReader = CSVReader;